Advertisement
vkimura

Excel VBA Match Current Forex Rate with Date v.02 (errors)

Mar 31st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub finddata()
  4.  
  5. '1. declare variables
  6. '2. clear old search results
  7. '3. find records that match our criteria and paste them
  8.  
  9. Dim curDateToMatchReceipts As String
  10. Dim curDateToMatchForex As String
  11. Dim nextDateToMatchForex As String
  12. Dim sCurRate As String
  13. Dim sPrevCurRate As String
  14. Dim finalrow As Integer
  15. Dim finalrowforex As Integer
  16. Dim i As Integer
  17. Dim iforex As Integer
  18. Dim iOffset As Integer
  19. Dim sTest As String
  20.  
  21.  
  22. finalrow = Sheets("Receipts").Range("A10000").End(xlUp).Row
  23. finalrowforex = Sheets("Forex15").Range("A3000").End(xlUp).Row
  24. iOffset = 0 'initialize default offset for retrieving rate value
  25.  
  26. For i = 2 To finalrow
  27.  
  28.     curDateToMatchReceipts = Sheets("Receipts").Cells(i, 1)
  29.    
  30.     For iforex = 13 To finalrowforex
  31.    
  32.         curDateToMatchForex = Sheets("Forex15").Cells(iforex, 1)
  33.         If curDateToMatchReceipts = curDateToMatchForex Then
  34.        
  35.             sCurRate = Sheets("Forex15").Cells(iforex, 2).Value
  36.            
  37.             'check if current rate is a 'bank holiday'
  38.            If IsNumeric(sCurRate) Then
  39.                 iOffset = 0
  40.                 GoTo NextIteration
  41.             Else
  42.                 sPrevCurRate = Sheets("Forex15").Cells(iforex, 2).Offset(1, 0).Value
  43.                
  44.                 'check if the previous current rate is a bank holiday. At most there are only two in a row
  45.                If IsNumeric(sPrevCurRate) Then
  46.                     iOffset = 1
  47.                     GoTo NextIteration
  48.                 Else
  49.                     'Sheets("Forex15").Cells(iforex, 2).Offset(2, 0).Value
  50.                    'Call copyPasteSelectedRate(iforex, i)
  51.                    iOffset = 2
  52.                     GoTo NextIteration
  53.                 End If
  54.                
  55.             End If
  56.            
  57.             'Sheets("Forex15").Range("B" & iforex).Copy
  58.            'Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
  59.            
  60.         End If
  61.        
  62.         'reset curDateToMatchForex
  63.        curDateToMatchForex = ""
  64.        
  65.     Next iforex
  66.    
  67.     'reset curDateToMatchReceipts
  68.    curDateToMatchReceipts = ""
  69.    
  70. NextIteration:
  71.     Call copyPasteSelectedRate(iforex, i, iOffset)
  72.    
  73.     'reset offest
  74.    iOffset = 0
  75.     'reset curDateToMatchForex
  76.    curDateToMatchForex = ""
  77.     'reset curDateToMatchReceipts
  78.    curDateToMatchReceipts = ""
  79. Next i
  80.  
  81. End Sub
  82.  
  83. 'function to copy selected rate and then paste into the Receipts sheet of the current row in the loop
  84. Sub copyPasteSelectedRate(iforex As Integer, i As Integer, iOffset As Integer)
  85.     'If Sheets("Receipts").Range("C" & i).Value <> "" Then
  86.        Sheets("Forex15").Cells(iforex, 2).Offset(iOffset, 0).Copy
  87.         'Sheets("Forex15").Range("B" & iforex).Copy
  88.        Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
  89.     'End If
  90.    
  91.     'when there is no date in the Forex sheet then go to next available previous row
  92.    'we check if there is no date by checking if there is not CRA Exchange Rate pasted in the cell
  93.    'If Sheets("Receipts").Cells(i, 3) = "" Then
  94.        'nextDateToMatchForex = Sheets("Forex15").Cells(iforex, 2).Value
  95.        'Sheets("Forex15").Cells(iforex, 1).Offset(1, 1).Copy
  96.        'Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
  97.    'End If
  98. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement