I have a folder named "My System" with 31 identical workbooks (except the name). Since I use these workbooks on my PC, everything work good but I started to have a problem when I copied the folder from my PC to my PC at work.
Lets work on workbook named "2".
I have a command button to run this code:
Code: Select all
Sub SO()
Dim strPath As String
strPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
dateStamp = Date
If Not Range("Q1").Value = dateStamp Then
Range("Q1").Value = dateStamp
'// replace with your code below-------------
Workbooks("2").Sheets("Chit1&2").Range("E20:H21").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit1&2'!E20:H21"
Workbooks("2").Sheets("Chit3&4").Range("E20:H21").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit3&4'!E20:H21"
Workbooks("2").Sheets("Chit5&6").Range("E20:H21").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit5&6'!E20:H21"
Workbooks("2").Sheets("Chit1&2").Cells(8, "J").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit1&2'!J8"
Workbooks("2").Sheets("Chit3&4").Cells(8, "J").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit3&4'!J8"
Workbooks("2").Sheets("Chit5&6").Cells(8, "J").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit5&6'!J8"
MsgBox "Code run for first time today!"
Else
MsgBox "Code has already been run today!"
End If
End Sub
Run-Time Error '9'
Subscript out of range
After I debug, the line ( Workbooks("2").Sheets("Chit1&2").Range("E20:H21").Value = "=" & "'" & strPath & "\My System\1. January 2019\[1.xlsm]Chit1&2'!E20:H21") becomes highlighted in yellow. From my very little knowledge in VBA, I am suspected that the problem is (strPath). What you think?
Thanks in advance