jimmisavage
Board Regular
- Joined
- Jun 28, 2017
- Messages
- 130
Good day,
I have a folder which has a bunch of spreadsheets in them - we're constantly adding and deleting but they're all from the same template. I don't want to put macro's in these files because it seems to scare people in my work!!
I have found a bit of code, which opens the first file but then gives me a subscript out of range warning before running my codes.
I figured that this might have been because the macro i'm calling isn't from the newly opened workbook - so i tried this, but get the same error:
So maybe there is something wrong with my code? I dont think so, because it's quite simple and it works using it normally:
Can anyone give me an idea of why this is going wrong? Maybe I have to select the newly opened spreadsheet? How would i do that?
Thanks in advance!
Jimmi
I have a folder which has a bunch of spreadsheets in them - we're constantly adding and deleting but they're all from the same template. I don't want to put macro's in these files because it seems to scare people in my work!!
I have found a bit of code, which opens the first file but then gives me a subscript out of range warning before running my codes.
Code:
Sub RunOnAllFilesInFolder()
folderName = "S:\Haemophilia\Reports\Study Updates\"
If Right(folderName, 1) <> Application.PathSeparator Then folderName = folderName & Application.PathSeparator
Fname = Dir(folderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(folderName & Fname)
' here comes the code for the operations on every file the code finds
Call Behindthescenes
Call MainRun
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub
I figured that this might have been because the macro i'm calling isn't from the newly opened workbook - so i tried this, but get the same error:
Code:
Sub RunOnAllFilesInFolder()
folderName = "S:\Haemophilia\Reports\Study Updates\"
If Right(folderName, 1) <> Application.PathSeparator Then folderName = folderName & Application.PathSeparator
Fname = Dir(folderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(folderName & Fname)
' here comes the code for the operations on every file the code finds
Application.Run "'Study Updates Runner.xlsm'!Behindthescenes"
Application.Run "'Study Updates Runner.xlsm'!Behindthescenes"
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub
So maybe there is something wrong with my code? I dont think so, because it's quite simple and it works using it normally:
Code:
Private Sub Behindthescenes()
Sheets("New").Visible = True
Sheets("Old").Visible = True
Sheets("Temp").Visible = True
Cells.Select
Selection.Copy
Sheets("Old").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Temp").Select
Range("A1").Select
Sheets("Summary").Select
Range("A1").Select
End Sub
Can anyone give me an idea of why this is going wrong? Maybe I have to select the newly opened spreadsheet? How would i do that?
Thanks in advance!
Jimmi