I can't get past the "subscript out of range" error arising from the code below. It throws the error when I first set the found variable. The sub is for each sheet in the workbook, to search for an employee from a known column and identify the row, then use that row to sum a variable. Then it fills a cell in the active sheet with that total. "col" is specified in the main sub.
nDate = Range("B3")
Dim startDate As Date
startDate = Range("B2")
Dim emp As String
emp = Range("B8")
Dim rw As String
n = 0
Do While True
Range("B99") = nDate
stringDate = Range("B99").Text
Set found = Worksheets(stringDate).Range("D38:D144").Find(emp, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If found = emp Then
rw = found.row
n = Worksheets(stringDate).Range(col + rw)
tot = tot + n
End If
If nDate = startDate Then
Exit Do
End If
nDate = nDate - 1
Loop
Range(col + "3") = tot
I have similar Subs in my code that compiled just fine. The only difference is that in the sub above, I'm searching a range. Below is the other sub that doesn't throw the error.
n = 0
Dim startDate As Date
Dim endDate As Date
startDate = Range("B2")
nDate = Range("B3")
Do While True
Range("B99") = nDate
stringDate = Range("B99").Text
n = Worksheets(stringDate).Range(col + rw)
tot = tot + n
If nDate = startDate Then
Exit Do
End If
nDate = nDate - 1
Loop
Range(col + "3") = tot
I know there are similar questions about the same error, but none deal with referencing external sheets. Any suggestions on how to debug?
Option Explicit
at the top of every module? If not, your code will compile but may throw errors at run-time. - Mark Fitzgerald