Hi folks
I have written a piece of code to populate cells from a source data file. There will be no more than 12 rows of data in the source file, one row per month. However, sometimes one or more months will not produce any data and the number of rows of data will be fewer. My code works fine when the source data contains 12 rows, but when it doesn't, I get a type mismatch at the point where the lookup value is not found in the source file. Any ideas why this is happening? Here's the code...
I have written a piece of code to populate cells from a source data file. There will be no more than 12 rows of data in the source file, one row per month. However, sometimes one or more months will not produce any data and the number of rows of data will be fewer. My code works fine when the source data contains 12 rows, but when it doesn't, I get a type mismatch at the point where the lookup value is not found in the source file. Any ideas why this is happening? Here's the code...
Code:
Dim intLookUpValue As Integer
Dim targetRow As Integer
Dim sourceRow As Integer
Dim rngLookUpRange As Range
Dim myVar As Variant
Dim wbMprCases As Workbook
Dim wsSource As String
targetRow = 7
sourceRow = 2
intLookUpValue = 0
Set wbMprCases = ThisWorkbook
Set wb1 = Workbooks.Open(wbMprCases.Worksheets("Sheet1").Range("AB16").Text)
wsSource = wbMprCases.Worksheets("Sheet1").Range("AB17")
Set rngLookUpRange = wb1.Worksheets(wsSource).Range("A2:d14")
Do While intLookUpValue < 13
If IsError(myVar = Application.VLookup(intLookUpValue, rngLookUpRange, 3, False)) Then
myVar = 0
Else: myVar = Application.VLookup(intLookUpValue, rngLookUpRange, 3, False)
End If
If myVar = 0 Then
wbMprCases.Worksheets("Sheet1").Cells(targetRow, 22).Value = 0
Else: wbMprCases.Worksheets("Sheet1").Cells(targetRow, 22).Value = myVar
End If
targetRow = targetRow + 2
sourceRow = sourceRow + 1
intLookUpValue = intLookUpValue + 1
Loop
wb1.Close