0

Not sure why I'm getting subscript out of range error on this code but as per my understandings the code is correct. This code was working fine few hours back but now getting this error. Can anyone help on this. Restarted my system several times but getting this error again and again.

Dim workbookNames As Variant
workbookNames = Array("Book1.xlsm", "Book2.xlsm", "Book3.xlsm", "Book4.xlsm")

Dim i As Long
For i = LBound(workbookNames) To UBound(workbookNames)
    
    Dim wb As Workbook
    Set wb = Workbooks(workbookNames(i)) -----**SUBSCRIPT OUT OF RANGE**
Lal Sahab
  • 17
  • 1
  • 4
  • Asking the dumb question: do you have those four workbooks open? – BigBen Mar 29 '22 at 21:15
  • Yes, all 4 workbooks are opened and then trying to execute this code. It was working fine few hours back but now getting error – Lal Sahab Mar 29 '22 at 21:16
  • Add a new subroutine: `Sub foo()`, `Dim wb As Workbook`, `For Each wb in Workbooks`, `Debug.Print wb.Name`, `Next`, `End Sub`. Run. What is the output in the Immediate Window? – BigBen Mar 29 '22 at 21:17
  • To change pivot fields in all 4 workbooks and then copy data and paste in separate workbook. I've completed the code but getting error on this line, not sure why when it was working fine few hours back. But now its giving error – Lal Sahab Mar 29 '22 at 21:21
  • Did you ever figure this out? – SixSigmaGuy Aug 02 '23 at 06:38

0 Answers0