Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Sub finddata()
- '1. declare variables
- '2. clear old search results
- '3. find records that match our criteria and paste them
- Dim curDateToMatchReceipts As String
- Dim curDateToMatchForex As String
- Dim nextDateToMatchForex As String
- Dim sCurRate As String
- Dim sPrevCurRate As String
- Dim finalrow As Integer
- Dim finalrowforex As Integer
- Dim i As Integer
- Dim iforex As Integer
- Dim iOffset As Integer
- Dim sTest As String
- finalrow = Sheets("Receipts").Range("A10000").End(xlUp).Row
- finalrowforex = Sheets("Forex15").Range("A3000").End(xlUp).Row
- iOffset = 0 'initialize default offset for retrieving rate value
- For i = 2 To finalrow
- curDateToMatchReceipts = Sheets("Receipts").Cells(i, 1)
- For iforex = 13 To finalrowforex
- curDateToMatchForex = Sheets("Forex15").Cells(iforex, 1)
- If curDateToMatchReceipts = curDateToMatchForex Then
- sCurRate = Sheets("Forex15").Cells(iforex, 2).Value
- 'check if current rate is a 'bank holiday'
- If IsNumeric(sCurRate) Then
- iOffset = 0
- GoTo NextIteration
- Else
- sPrevCurRate = Sheets("Forex15").Cells(iforex, 2).Offset(1, 0).Value
- 'check if the previous current rate is a bank holiday. At most there are only two in a row
- If IsNumeric(sPrevCurRate) Then
- iOffset = 1
- GoTo NextIteration
- Else
- 'Sheets("Forex15").Cells(iforex, 2).Offset(2, 0).Value
- 'Call copyPasteSelectedRate(iforex, i)
- iOffset = 2
- GoTo NextIteration
- End If
- End If
- 'Sheets("Forex15").Range("B" & iforex).Copy
- 'Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
- End If
- 'reset curDateToMatchForex
- curDateToMatchForex = ""
- Next iforex
- 'reset curDateToMatchReceipts
- curDateToMatchReceipts = ""
- NextIteration:
- Call copyPasteSelectedRate(iforex, i, iOffset)
- 'reset offest
- iOffset = 0
- 'reset curDateToMatchForex
- curDateToMatchForex = ""
- 'reset curDateToMatchReceipts
- curDateToMatchReceipts = ""
- Next i
- End Sub
- 'function to copy selected rate and then paste into the Receipts sheet of the current row in the loop
- Sub copyPasteSelectedRate(iforex As Integer, i As Integer, iOffset As Integer)
- 'If Sheets("Receipts").Range("C" & i).Value <> "" Then
- Sheets("Forex15").Cells(iforex, 2).Offset(iOffset, 0).Copy
- 'Sheets("Forex15").Range("B" & iforex).Copy
- Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
- 'End If
- 'when there is no date in the Forex sheet then go to next available previous row
- 'we check if there is no date by checking if there is not CRA Exchange Rate pasted in the cell
- 'If Sheets("Receipts").Cells(i, 3) = "" Then
- 'nextDateToMatchForex = Sheets("Forex15").Cells(iforex, 2).Value
- 'Sheets("Forex15").Cells(iforex, 1).Offset(1, 1).Copy
- 'Sheets("Receipts").Range("C" & i).PasteSpecial xlPasteFormulasAndNumberFormats
- 'End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement