This is driving me absolutely insane. I'm new to VBA and I compiled code line by line, adding more and more verifying it all worked within the same workbook using F8. The last bit I have to add is just opening a separate workbook, and now it's giving me errors each time. Here's my code:
Sub MasterXfer()
Dim mystring As String, wbName As String, dt As String, sdt As String, ldt As String
Dim wb1 As Workbook, wb2 As Workbook, mypath As String
wbNam = "Productivity "
dt = Sheet1.Range("B1").Value
sdt = Format(CStr(dt), "m.d.yy") & ".xlsx"
ldt = Format(CStr(dt), "yyyy") & "\" & Format(CStr(dt), "mm") & "_" & MonthName(Month(dt)) & "_" & Year(dt)
mypath = "S:\" & ldt & "\" & wbNam & sdt
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open(mypath) 'HERE'S WHERE IT ERRORS OUT
With wb1
lastrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
For x = 2 To lastrow Step 16
mystring = .Range("A" & x)
Stepping through this, it works fine. Then I get to the Set wb2 = Workbooks.Open line, and it successfully opens the target workbook, however immediately upon opening it the code stops and the error in question comes up.
If anyone at all can tell me what mistake I'm making I will name my firstborn after you.
debug.print mypathshow at that point (look in the Immediate window with [ctrl]+G)? Why use a 'helper' function likeMonthNamewhen you can just useFormat(date, "mmm")orFormat(date, "mmmm")? I seem to remember problems with workbook names containing periods; can you run a test without them?lastrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Rowhere theWorksheets(1)is not tied to wb1 since you're missing the leading period. Unless you meant it that way.Set wb2 = ...line, enterDebug.print mypath. Copy the line from the immediate window and paste it into the address bar of a Windows Explorer window. This will validate is the path you are building is to a file that exists. IF it fails to open copy the two paths (yours and the actual one) into notepad to see what might be missing.