I have the below code. In a nutshell- I have two files a reader and a data. I need multiple users to be able to access these files, but they are stored on a Box account, so the file directory is different for each user. I have created the code that SHOULD do this. It runs perfectly when I step through the code by hand using F8, or even pressing F5 after the workbook is open. However I need this to run on workbook open. When I open it I get the following error: Run-time error '1004': Application-defined or orbject-defined error. This happens at the
Set directoryRange = ImSapMacroWorkbook.Worksheets("Users").Range(Cells(1, 2), Cells(lastUser, 2))
I have no idea why it happens at this point, but it is not consistent so I haven't been able to pin down why this happens. If it matters this is the first sub run in the auto_open sub. No variables are even declared before this runs.
Sub GrabData()
Dim DataWorkbook As Workbook
Dim ImSapMacroWorkbook As Workbook
Dim copyRange As Range
Dim pasteRange As Range
Dim directoryRange As Range
Dim LastRow As Integer
Dim lastUser As Integer
lastUser = ThisWorkbook.Worksheets("Users").Range("A1048576").End(xlUp).Row
Set directoryRange = ThisWorkbook.Worksheets("Users").Range(Cells(1, 2), Cells(lastUser, 2))
Set MacroWorkbook = ThisWorkbook
On Error GoTo nextUser
For Each c In directoryRange
Set DataWorkbook = Workbooks.Open(c.Value)
Exit For
nextUser:
Next c
LastRow = DataWorkbook.Worksheets("Sheet1").Range("A1048576").End(xlUp).Row
Set copyRange = DataWorkbook.Worksheets("Sheet1").Range(Cells(2, 1), Cells(LastRow, 36))
Set pasteRange = MacroWorkbook.Sheets("input").Cells(2, 1)
Call clearData
copyRange.Copy
MacroWorkbook.Activate
pasteRange.Select
ActiveSheet.Paste
End Sub
Aucun commentaire:
Enregistrer un commentaire