I'm quite new to VBA and very rusty from java and C++ years ago, so any help is appreciated. At an overview I am trying to make this program find a number and interpret the month into a sheet number on an external workbook, then return me a value based off of the first number. I have it doing this but I cannot get the loop to end at the first empty cell. It could be a problem with the range in lookFor
or with the loop statement; I'm not sure. Any thoughts? Here is the code
Sub Go()
Range("B6").Select
Do Until IsEmpty(ActiveCell)
Dim lookFor As Range
Dim srchRange As Range
Dim book1 As Workbook
Dim book2 As Workbook
Dim book2Name As String
book2Name = "Cash_Office_Long_Short_Log_FYE18.xlsx"
Dim book2NamePath As String
book2NamePath = ThisWorkbook.Path & "\" & book2Name
Dim refMonth As Integer
refMonth = Month(Cells(6, 5)) + 1
Debug.Print "refMonth="; refMonth
Set book1 = ThisWorkbook
If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)
Set book2 = Workbooks(book2Name)
Set lookFor = book1.Sheets(1).Range("B6:B800")
Set srchRange = book2.Sheets(refMonth).Range("A1:B800")
lookFor.Offset(0, -1).Value = Application.VLookup(lookFor, srchRange, 2, False)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Function IsOpen(strWkbNm As String) As Boolean
On Error Resume Next
Dim wBook As Workbook
Set wBook = Workbooks(strWkbNm)
If wBook Is Nothing Then
IsOpen = False
Set wBook = Nothing
On Error GoTo 0
Else
IsOpen = True
Set wBook = Nothing
On Error GoTo 0
End If
End Function