VBA help-Look up previous day data and return result

Natalia

Board Regular
Joined
Feb 20, 2009
Messages
72
Hi,

I need your help with a worktask, not a big task. I have reconciliation reports which gets updated each day, so some entries will clear and some will stay and there will be new entries appearing. My team enter their names for the recs they are responsible for as shown in 050511 wkb. The next day when the new report is updated they would have to re-input their names in A6:A to their recs.

I need a macro that will compare current data to previous day if match found then return their names in Col A6:A. This will enable them to see which breaks haven't cleared.

Now there are two tabs "Cash" and "Asset", the asset tab is easy to do a lookup as there is a case number in E6:E, but for cash the macro will need to concatenate in VBA memory a few columns to create a unique ref. So to concatenate cash use B&C&D&E&F&G&H&I&L&M&N, this should do it.

050511 Workbook

Excel Workbook
ABCDEFGHIJKLMNO
5NameGroupCategorySet IDValue DateEntry DateTypeAmountCCYAgeSourceRef1Ref2Ref3Reference
6NatCITIBANKCASH CITIBNPPSITESAPP2-Jun-082-Dec-09SCR4,417.80GBP1019SMP1FUNDS TFRNATL WESTMINSTER BK PLC LONDONSITE ROB GREGOR
7JamesCITIBANKCASH CITIBNPPSITESAPP3-Jun-082-Dec-09SCR2,208.90GBP1018SMP1FUNDS TFRCITIBANK NA LONDONJEFFERY NEIL 3 GOLDERSL
8JohnCITIBANKCASH CITIWTSIE1GBPBNPP20-Dec-1020-Dec-10SCR5,834.29GBP88SMP1NONREFSG1035400992OMTSAMP CAPITAL INVESTORSkjhjhjhjh
9LouiseCITIBANKCASH CITIWTPE2AUSDBNPP23-Dec-1023-Dec-10SCR6,250.00USD85SMP1634SG1035700026OMTSCETERA BROKERS NETWORK, LLC
10JamesCITIBANKCASH CITIWTGDPFEURBNPP24-Jan-1124-Jan-11SDR-2,200,000.00EUR53SMP1FXWTDPFP22SG1102400435OMTSALLEZ AND ASSOCIES SCP MARTIN
Cash


Excel Workbook
ABCDEFGHIJKLM
5NameRecAccountSourceCase No.Value DateSecurity CodeDescriptionCcy CodeLedg StmtUnitsExceptionAge Break
6NatBNPPINTLBNPP.WTPE1AAMP17735374-Apr-11SOLCAUSDSOLYNDRA INC CONVERTIBLE NOTE TRANCHE AUSDLedg1,003,682.00-1,003,682.0029
7JamesBNPPINTLBNPP.WTPE1BAMP17735384-Apr-11SOLCAUSDSOLYNDRA INC CONVERTIBLE NOTE TRANCHE AUSDLedg177,120.00-177,120.0029
8JohnSMPDIAT.DAIAMP27738758-Apr-11AU3CB0173953MEDL 2011-1 A2 6.5PCT 22NOV42Stmt1,000,000.001,000,000.0025
9LouiseSMPCITA.CBASAMP277423412-Apr-11AU000000EQN4EQUINOX MINERALS CDI NPV /ST/010Stmt59,782.0059,782.0021
10LouiseSMPCITA.CBASAMP277423512-Apr-11AU000000CGG3CITADEL RESOURCE GROUP LIMITED NPV /ST/010Stmt854,892.00-854,892.0021
11LouiseSMPNCSX.GESBEAMP277494415-Apr-11NCMNEWCREST MINING LIMITEDAUDLedg465,027.001,200.0018
12LouiseSMPNCSX.GESBEAMP277494415-Apr-11AU000000NCM7NEWCREST MINING LTDAUDStmt466,227.0018
13LouiseSMPJPMT.WHEISSAMP277509718-Apr-11B1FYW63GAGFAH SAEURLedg8,547.00-8,547.0015
14LouiseSMPNCSX.CSLAMP277523319-Apr-11WHGWHK GROUP LIMITEDAUDLedg80,578.00-80,578.0014
15LouiseSMPNCSX.GESSAMP277523219-Apr-11WHGWHK GROUP LIMITEDAUDLedg126,002.00-126,002.0014
Asset


060511 Workbook
Excel Workbook
ABCDEFGHIJKLMNO
5NameGroupCategorySet IDValue DateEntry DateTypeAmountCCYAgeSourceRef1Ref2Ref3Reference
6CITIBANKCASH CITIBNPPSITESAPP2-Jun-082-Dec-09SCR4,417.80GBP1019SMP1FUNDS TFRNATL WESTMINSTER BK PLC LONDONSITE ROB GREGOR
7JP MORGANCASH JPYTYTTYTT12-Aug-107-Nov-10SCR23,434.36BNM345WERYTYTY244544
8CITIBANKCASH CITIBNPPSITESAPP3-Jun-082-Dec-09SCR2,208.90GBP1018SMP1FUNDS TFRCITIBANK NA LONDONJEFFERY NEIL 3 GOLDERSL
9CITIBANKCASH CITIWTSIE1GBPBNPP20-Dec-1020-Dec-10SCR5,834.29GBP88SMP1NONREFSG1035400992OMTSAMP CAPITAL INVESTORSkjhjhjhjh
10CITIBANKCASH CITIWTPE2AUSDBNPP23-Dec-1023-Dec-10SCR6,250.00USD85SMP1634SG1035700026OMTSCETERA BROKERS NETWORK, LLC
11CITIBANKCASH CITIWTGDPFEURBNPP24-Jan-1124-Jan-11SDR-2,200,000.00EUR53SMP1FXWTDPFP22SG1102400435OMTSALLEZ AND ASSOCIES SCP MARTIN
12
Cash


Excel Workbook
ABCDEFGHIJKLM
5NameRecAccountSourceCase No.Value DateSecurity CodeDescriptionCcy CodeLedg StmtUnitsExceptionAge Break
6JP MORGANCITA.CBASCIT12342338-Apr-11EWREWTEWTestNORLedg3,235,325.00-12,155.0021
7BNPPINTLBNPP.WTPE1AJP17735374-Apr-11SOLCAUSDSOLYNDRA INC CONVERTIBLE NOTE TRANCHE AUSDLedg1,003,682.00-1,003,682.0029
8BNPPINTLBNPP.WTPE1BJP17735384-Apr-11SOLCAUSDSOLYNDRA INC CONVERTIBLE NOTE TRANCHE AUSDLedg177,120.00-177,120.0029
9SMPDIAT.DAIJP17738758-Apr-11AU3CB0173953MEDL 2011-1 A2 6.5PCT 22NOV42Stmt1,000,000.001,000,000.0025
10SMPCITA.CBASJP177423412-Apr-11AU000000EQN4EQUINOX MINERALS CDI NPV /ST/010Stmt59,782.0059,782.0021
11SMPCITA.CBASJP177423512-Apr-11AU000000CGG3CITADEL RESOURCE GROUP LIMITED NPV /ST/010Stmt854,892.00-854,892.0021
12SMPNCSX.GESBEJP177494415-Apr-11NCMNEWCREST MINING LIMITEDAUDLedg465,027.001,200.0018
13SMPNCSX.GESBEJP177494415-Apr-11AU000000NCM7NEWCREST MINING LTDAUDStmt466,227.0018
14SMPJPMT.WHEISSJP177509718-Apr-11B1FYW63GAGFAH SAEURLedg8,547.00-8,547.0015
15SMPNCSX.CSLJP177523319-Apr-11WHGWHK GROUP LIMITEDAUDLedg80,578.00-80,578.0014
16SMPNCSX.GESSJP177523219-Apr-11WHGWHK GROUP LIMITEDAUDLedg126,002.00-126,002.0014
17SMPNCSX.GESSJP115151119-Apr-11WHGWHK GROUP LIMITEDAUDLedg126,002.00-54,545.3214
Asset
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi Nat, thanks for the pm.

I would place this code in a module. This can be exported to your hard drive and imported into each new workbook when needed.

I have used Application.GetOpenFilename to allow the user to select the file for comparison.

Here are the range variables I have used:


Code:
[color=green]'============================================[/color]
   [color=green]'set up the worksheet ranges to loop through[/color]
   [color=green]'============================================[/color]
   [color=darkblue]Set[/color] rngAssetNew = Sheets("Asset").Range("B6")
   [color=darkblue]Set[/color] rngCashNew = Sheets("Cash").Range("B6")
   
   FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
   [color=darkblue]If[/color] FileToOpen <> [color=darkblue]False[/color] [color=darkblue]Then[/color]
      [color=darkblue]Set[/color] wbOld = Workbooks.Open(FileToOpen)
      [color=darkblue]Set[/color] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      [color=darkblue]Set[/color] rngCashOld = wbOld.Sheets("Cash").Range("B6")
   [color=darkblue]Else[/color]
      MsgBox "No file Selected"
      [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
   [color=darkblue]End[/color] [color=darkblue]If[/color]

The code then loops through both ranges, builds up the string for comparison and updates the name when a match is found.

Code:
   [color=green]'===============================[/color]
   [color=green]'asset worksheet[/color]
   [color=green]'==============================[/color]
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetNew = ""
      [color=green]'build up the string[/color]
      [color=darkblue]For[/color] i = 2 [color=darkblue]To[/color] 13
         MatchNew = MatchNew & Trim(rngAssetNew.Offset(0, i - 2))
      [color=darkblue]Next[/color] i
      
      [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetOld = ""
         [color=green]'build up the string[/color]
         [color=darkblue]For[/color] j = 2 [color=darkblue]To[/color] 13
            MatchOld = MatchOld & Trim(rngAssetOld.Offset(0, j - 2))
         [color=darkblue]Next[/color] j
      
         [color=darkblue]If[/color] MatchOld = MatchNew [color=darkblue]Then[/color]
            rngAssetNew.Offset(0, -1) = rngAssetOld.Offset(0, -1)
            [color=darkblue]Exit[/color] [color=darkblue]Do[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]

The code to handle the cash sheet is pretty much the same.

Here is the full code:

Code:
[color=darkblue]Sub[/color] CompareStrings()
   [color=darkblue]Dim[/color] wbOld [color=darkblue]As[/color] Workbook
   [color=darkblue]Dim[/color] wbNew [color=darkblue]As[/color] Workbook
   [color=darkblue]Dim[/color] rngAssetOld As Range
   [color=darkblue]Dim[/color] rng[color=darkblue]As[/color]setNew [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] rngCashOld [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] rngCashNew [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] FileToOpen
   [color=darkblue]Dim[/color] MatchOld [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] MatchNew [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] i As [color=darkblue]Integer[/color], j As [color=darkblue]Integer[/color]   [color=green]'loop index'[/color]
   
   [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] errCompareStrings
   
   [color=green]'============================================[/color]
   [color=green]'set up the worksheet ranges to loop through[/color]
   [color=green]'============================================[/color]
   [color=darkblue]Set[/color] rngAssetNew = Sheets("Asset").Range("B6")
   [color=darkblue]Set[/color] rngCashNew = Sheets("Cash").Range("B6")
   
   FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
   [color=darkblue]If[/color] FileToOpen <> [color=darkblue]False[/color] [color=darkblue]Then[/color]
      [color=darkblue]Set[/color] wbOld = Workbooks.Open(FileToOpen)
      [color=darkblue]Set[/color] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      [color=darkblue]Set[/color] rngCashOld = wbOld.Sheets("Cash").Range("B6")
   [color=darkblue]Else[/color]
      MsgBox "No file Selected"
      [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
   [color=darkblue]End[/color] [color=darkblue]If[/color]
   
   [color=green]'===============================[/color]
   [color=green]'asset worksheet[/color]
   [color=green]'==============================[/color]
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetNew = ""
      [color=green]'build up the string[/color]
      [color=darkblue]For[/color] i = 2 [color=darkblue]To[/color] 13
         MatchNew = MatchNew & Trim(rngAssetNew.Offset(0, i - 2))
      [color=darkblue]Next[/color] i
      
      [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetOld = ""
         [color=green]'build up the string[/color]
         [color=darkblue]For[/color] j = 2 [color=darkblue]To[/color] 13
            MatchOld = MatchOld & Trim(rngAssetOld.Offset(0, j - 2))
         [color=darkblue]Next[/color] j
      
         [color=darkblue]If[/color] MatchOld = MatchNew [color=darkblue]Then[/color]
            rngAssetNew.Offset(0, -1) = rngAssetOld.Offset(0, -1)
            [color=darkblue]Exit[/color] [color=darkblue]Do[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]
         
         MatchOld = ""
         [color=darkblue]Set[/color] rngAssetOld = rngAssetOld.Offset(1, 0)
      [color=darkblue]Loop[/color]
      
      [color=darkblue]Set[/color] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      MatchNew = ""
      [color=darkblue]Set[/color] rngAssetNew = rngAssetNew.Offset(1, 0)
   [color=darkblue]Loop[/color]
   
   [color=green]'===============================[/color]
   [color=green]'cash worksheet[/color]
   [color=green]'==============================[/color]
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngCashNew = ""
      [color=green]'build up the string[/color]
      [color=darkblue]For[/color] i = 2 [color=darkblue]To[/color] 13
         MatchNew = MatchNew & Trim(rngCashNew.Offset(0, i - 2))
      [color=darkblue]Next[/color] i
      
      [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngCashOld = ""
         [color=green]'build up the string[/color]
         [color=darkblue]For[/color] j = 2 [color=darkblue]To[/color] 13
            MatchOld = MatchOld & Trim(rngCashOld.Offset(0, j - 2))
         [color=darkblue]Next[/color] j
      
         [color=darkblue]If[/color] MatchOld = MatchNew [color=darkblue]Then[/color]
            rngCashNew.Offset(0, -1) = rngCashOld.Offset(0, -1)
            [color=darkblue]Exit[/color] [color=darkblue]Do[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]
         
         MatchOld = ""
         [color=darkblue]Set[/color] rngCashOld = rngCashOld.Offset(1, 0)
      [color=darkblue]Loop[/color]
      
      [color=darkblue]Set[/color] rngCashOld = wbOld.Sheets("Cash").Range("B6")
      MatchNew = ""
      [color=darkblue]Set[/color] rngCashNew = rngCashNew.Offset(1, 0)
   [color=darkblue]Loop[/color]
   
   
exitCompareStrings:
   [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
   
  [color=green]' wbOld.Close SaveChanges:=False[/color]
   [color=darkblue]Set[/color] wbOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngAssetNew = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngCashNew = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngAssetOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngCashOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
errCompareStrings:
   MsgBox "Error: " & Err.Number & vbCrLf & _
         "Description: " & Err.Description
   [color=darkblue]Resume[/color] exitCompareStrings:
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

Edit: ps I have commented out a line to close the workbook which was opened.
Bertie
 
Last edited:
Upvote 0
Hi Bertie, i did a test and found the code does not return matched data for asset. For Asset the code only needs to lookup E6:E since the case number is sufficient to perform a lookup.

For cash i dont want the code to look at Age (Col J) and Source (Col K) as these variable will be changing constantly from day to day, whereas the other variables are fixed.



Hi Nat, thanks for the pm.

I would place this code in a module. This can be exported to your hard drive and imported into each new workbook when needed.

I have used Application.GetOpenFilename to allow the user to select the file for comparison.

Here are the range variables I have used:


Code:
[COLOR=green]'============================================[/COLOR]
   [COLOR=green]'set up the worksheet ranges to loop through[/COLOR]
   [COLOR=green]'============================================[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngAssetNew = Sheets("Asset").Range("B6")
   [COLOR=darkblue]Set[/COLOR] rngCashNew = Sheets("Cash").Range("B6")
   
   FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
   [COLOR=darkblue]If[/COLOR] FileToOpen <> [COLOR=darkblue]False[/COLOR] [COLOR=darkblue]Then[/COLOR]
      [COLOR=darkblue]Set[/COLOR] wbOld = Workbooks.Open(FileToOpen)
      [COLOR=darkblue]Set[/COLOR] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      [COLOR=darkblue]Set[/COLOR] rngCashOld = wbOld.Sheets("Cash").Range("B6")
   [COLOR=darkblue]Else[/COLOR]
      MsgBox "No file Selected"
      [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
The code then loops through both ranges, builds up the string for comparison and updates the name when a match is found.

Code:
   [COLOR=green]'===============================[/COLOR]
   [COLOR=green]'asset worksheet[/COLOR]
   [COLOR=green]'==============================[/COLOR]
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngAssetNew = ""
      [COLOR=green]'build up the string[/COLOR]
      [COLOR=darkblue]For[/COLOR] i = 2 [COLOR=darkblue]To[/COLOR] 13
         MatchNew = MatchNew & Trim(rngAssetNew.Offset(0, i - 2))
      [COLOR=darkblue]Next[/COLOR] i
      
      [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngAssetOld = ""
         [COLOR=green]'build up the string[/COLOR]
         [COLOR=darkblue]For[/COLOR] j = 2 [COLOR=darkblue]To[/COLOR] 13
            MatchOld = MatchOld & Trim(rngAssetOld.Offset(0, j - 2))
         [COLOR=darkblue]Next[/COLOR] j
      
         [COLOR=darkblue]If[/COLOR] MatchOld = MatchNew [COLOR=darkblue]Then[/COLOR]
            rngAssetNew.Offset(0, -1) = rngAssetOld.Offset(0, -1)
            [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Do[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
The code to handle the cash sheet is pretty much the same.
 
Last edited:
Upvote 0
Hi Nat,
I have made the highlighted changes to the code. Try this.
Code:
[color=darkblue]Sub[/color] CompareStrings()
   [color=darkblue]Dim[/color] wbOld [color=darkblue]As[/color] Workbook
   [color=darkblue]Dim[/color] wbNew [color=darkblue]As[/color] Workbook
   [color=darkblue]Dim[/color] rngAssetOld As Range
   [color=darkblue]Dim[/color] rng[color=darkblue]As[/color]setNew [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] rngCashOld [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] rngCashNew [color=darkblue]As[/color] Range
   [color=darkblue]Dim[/color] FileToOpen
   [color=darkblue]Dim[/color] MatchOld [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] MatchNew [color=darkblue]As[/color] [color=darkblue]String[/color]
   [color=darkblue]Dim[/color] i As [color=darkblue]Integer[/color], j As [color=darkblue]Integer[/color]   [color=green]'loop index'[/color]
   
   [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] errCompareStrings
   
   [color=green]'============================================[/color]
   [color=green]'set up the worksheet ranges to loop through[/color]
   [color=green]'============================================[/color]
   [color=darkblue]Set[/color] rngAssetNew = Sheets("Asset").Range("B6")
   [color=darkblue]Set[/color] rngCashNew = Sheets("Cash").Range("B6")
   
   FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
   [color=darkblue]If[/color] FileToOpen <> [color=darkblue]False[/color] [color=darkblue]Then[/color]
      [color=darkblue]Set[/color] wbOld = Workbooks.Open(FileToOpen)
      [color=darkblue]Set[/color] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      [color=darkblue]Set[/color] rngCashOld = wbOld.Sheets("Cash").Range("B6")
   [color=darkblue]Else[/color]
      MsgBox "No file Selected"
      [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
   [color=darkblue]End[/color] [color=darkblue]If[/color]
   
   [color=green]'================[/color]
   [color=green]'asset worksheet[/color]
   [color=green]'================[/color]
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetNew = ""
      [COLOR="Red"]MatchNew = rngAssetNew.Offset(0, 3).Value[/COLOR]
      
      [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngAssetOld = ""
         [COLOR="Red"]MatchOld = rngAssetOld.Offset(0, 3).Value[/COLOR]
         
         [color=darkblue]If[/color] MatchOld = MatchNew [color=darkblue]Then[/color]
            rngAssetNew.Offset(0, -1) = rngAssetOld.Offset(0, -1)
            [color=darkblue]Exit[/color] [color=darkblue]Do[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]
         
         MatchOld = ""
         [color=darkblue]Set[/color] rngAssetOld = rngAssetOld.Offset(1, 0)
      [color=darkblue]Loop[/color]
      
      [color=darkblue]Set[/color] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      MatchNew = ""
      [color=darkblue]Set[/color] rngAssetNew = rngAssetNew.Offset(1, 0)
   [color=darkblue]Loop[/color]
Stop
   [color=green]'===============================[/color]
   [color=green]'cash worksheet[/color]
   [color=green]'==============================[/color]
   [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngCashNew = ""
      [color=green]'build up the string[/color]
[COLOR="Red"]      MatchNew = rngCashNew.Value & _
               rngCashNew.Offset(, 1).Value & _
               rngCashNew.Offset(, 2).Value & _
               rngCashNew.Offset(, 3).Value & _
               rngCashNew.Offset(, 4).Value & _
               rngCashNew.Offset(, 5).Value & _
               rngCashNew.Offset(, 6).Value & _
               rngCashNew.Offset(, 7).Value & _
               rngCashNew.Offset(, 10).Value & _
               rngCashNew.Offset(, 11).Value & _
               rngCashNew.Offset(, 12).Value[/COLOR]
     
      [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngCashOld = ""
         [color=green]'build up the string[/color]
   [COLOR="red"]      MatchOld = rngCashOld.Value & _
                  rngCashOld.Offset(, 1).Value & _
                  rngCashOld.Offset(, 2).Value & _
                  rngCashOld.Offset(, 3).Value & _
                  rngCashOld.Offset(, 4).Value & _
                  rngCashOld.Offset(, 5).Value & _
                  rngCashOld.Offset(, 6).Value & _
                  rngCashOld.Offset(, 7).Value & _
                  rngCashOld.Offset(, 10).Value & _
                  rngCashOld.Offset(, 11).Value & _
                  rngCashOld.Offset(, 12).Value[/COLOR]

         [color=darkblue]If[/color] MatchOld = MatchNew [color=darkblue]Then[/color]
            rngCashNew.Offset(0, -1) = rngCashOld.Offset(0, -1)
            [color=darkblue]Exit[/color] [color=darkblue]Do[/color]
         [color=darkblue]End[/color] [color=darkblue]If[/color]
         
         MatchOld = ""
         [color=darkblue]Set[/color] rngCashOld = rngCashOld.Offset(1, 0)
      [color=darkblue]Loop[/color]
      
      [color=darkblue]Set[/color] rngCashOld = wbOld.Sheets("Cash").Range("B6")
      MatchNew = ""
      [color=darkblue]Set[/color] rngCashNew = rngCashNew.Offset(1, 0)
   [color=darkblue]Loop[/color]
   
   
exitCompareStrings:
   [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
   
  [color=green]' wbOld.Close SaveChanges:=False[/color]
   [color=darkblue]Set[/color] wbOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngAssetNew = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngCashNew = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngAssetOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Set[/color] rngCashOld = [color=darkblue]Nothing[/color]
   [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
errCompareStrings:
   MsgBox "Error: " & Err.Number & vbCrLf & _
         "Description: " & Err.Description
   [color=darkblue]Resume[/color] exitCompareStrings:
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thanks Bertie, that worked on the test data, but could you explain what this does? On my real data the code debugs on this line.

Do Until rngAssetNew = ""


Hi Nat,
I have made the highlighted changes to the code. Try this.
Code:
[COLOR=darkblue]Sub[/COLOR] CompareStrings()
   [COLOR=darkblue]Dim[/COLOR] wbOld [COLOR=darkblue]As[/COLOR] Workbook
   [COLOR=darkblue]Dim[/COLOR] wbNew [COLOR=darkblue]As[/COLOR] Workbook
   [COLOR=darkblue]Dim[/COLOR] rngAssetOld As Range
   [COLOR=darkblue]Dim[/COLOR] rng[COLOR=darkblue]As[/COLOR]setNew [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] rngCashOld [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] rngCashNew [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] FileToOpen
   [COLOR=darkblue]Dim[/COLOR] MatchOld [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] MatchNew [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] i As [COLOR=darkblue]Integer[/COLOR], j As [COLOR=darkblue]Integer[/COLOR]   [COLOR=green]'loop index'[/COLOR]
   
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] errCompareStrings
   
   [COLOR=green]'============================================[/COLOR]
   [COLOR=green]'set up the worksheet ranges to loop through[/COLOR]
   [COLOR=green]'============================================[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngAssetNew = Sheets("Asset").Range("B6")
   [COLOR=darkblue]Set[/COLOR] rngCashNew = Sheets("Cash").Range("B6")
   
   FileToOpen = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
   [COLOR=darkblue]If[/COLOR] FileToOpen <> [COLOR=darkblue]False[/COLOR] [COLOR=darkblue]Then[/COLOR]
      [COLOR=darkblue]Set[/COLOR] wbOld = Workbooks.Open(FileToOpen)
      [COLOR=darkblue]Set[/COLOR] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      [COLOR=darkblue]Set[/COLOR] rngCashOld = wbOld.Sheets("Cash").Range("B6")
   [COLOR=darkblue]Else[/COLOR]
      MsgBox "No file Selected"
      [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
   
   [COLOR=green]'================[/COLOR]
   [COLOR=green]'asset worksheet[/COLOR]
   [COLOR=green]'================[/COLOR]
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngAssetNew = ""
      [COLOR=Red]MatchNew = rngAssetNew.Offset(0, 3).Value[/COLOR]
      
      [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngAssetOld = ""
         [COLOR=Red]MatchOld = rngAssetOld.Offset(0, 3).Value[/COLOR]
         
         [COLOR=darkblue]If[/COLOR] MatchOld = MatchNew [COLOR=darkblue]Then[/COLOR]
            rngAssetNew.Offset(0, -1) = rngAssetOld.Offset(0, -1)
            [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Do[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
         
         MatchOld = ""
         [COLOR=darkblue]Set[/COLOR] rngAssetOld = rngAssetOld.Offset(1, 0)
      [COLOR=darkblue]Loop[/COLOR]
      
      [COLOR=darkblue]Set[/COLOR] rngAssetOld = wbOld.Sheets("Asset").Range("B6")
      MatchNew = ""
      [COLOR=darkblue]Set[/COLOR] rngAssetNew = rngAssetNew.Offset(1, 0)
   [COLOR=darkblue]Loop[/COLOR]
Stop
   [COLOR=green]'===============================[/COLOR]
   [COLOR=green]'cash worksheet[/COLOR]
   [COLOR=green]'==============================[/COLOR]
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngCashNew = ""
      [COLOR=green]'build up the string[/COLOR]
[COLOR=Red]      MatchNew = rngCashNew.Value & _
               rngCashNew.Offset(, 1).Value & _
               rngCashNew.Offset(, 2).Value & _
               rngCashNew.Offset(, 3).Value & _
               rngCashNew.Offset(, 4).Value & _
               rngCashNew.Offset(, 5).Value & _
               rngCashNew.Offset(, 6).Value & _
               rngCashNew.Offset(, 7).Value & _
               rngCashNew.Offset(, 10).Value & _
               rngCashNew.Offset(, 11).Value & _
               rngCashNew.Offset(, 12).Value[/COLOR]
     
      [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngCashOld = ""
         [COLOR=green]'build up the string[/COLOR]
   [COLOR=red]      MatchOld = rngCashOld.Value & _
                  rngCashOld.Offset(, 1).Value & _
                  rngCashOld.Offset(, 2).Value & _
                  rngCashOld.Offset(, 3).Value & _
                  rngCashOld.Offset(, 4).Value & _
                  rngCashOld.Offset(, 5).Value & _
                  rngCashOld.Offset(, 6).Value & _
                  rngCashOld.Offset(, 7).Value & _
                  rngCashOld.Offset(, 10).Value & _
                  rngCashOld.Offset(, 11).Value & _
                  rngCashOld.Offset(, 12).Value[/COLOR]

         [COLOR=darkblue]If[/COLOR] MatchOld = MatchNew [COLOR=darkblue]Then[/COLOR]
            rngCashNew.Offset(0, -1) = rngCashOld.Offset(0, -1)
            [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Do[/COLOR]
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
         
         MatchOld = ""
         [COLOR=darkblue]Set[/COLOR] rngCashOld = rngCashOld.Offset(1, 0)
      [COLOR=darkblue]Loop[/COLOR]
      
      [COLOR=darkblue]Set[/COLOR] rngCashOld = wbOld.Sheets("Cash").Range("B6")
      MatchNew = ""
      [COLOR=darkblue]Set[/COLOR] rngCashNew = rngCashNew.Offset(1, 0)
   [COLOR=darkblue]Loop[/COLOR]
   
   
exitCompareStrings:
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
   
  [COLOR=green]' wbOld.Close SaveChanges:=False[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wbOld = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngAssetNew = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngCashNew = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngAssetOld = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] rngCashOld = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
errCompareStrings:
   MsgBox "Error: " & Err.Number & vbCrLf & _
         "Description: " & Err.Description
   [COLOR=darkblue]Resume[/COLOR] exitCompareStrings:
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top