Subscript out of bounds help

bftbeckett

New Member
Joined
Apr 20, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am attempting to create an array to create a mapping file but I keep getting a Subscript out of bounds error on the tempArray function. I can't figure out what I need to do.

Sub SplitEachWorksheet()
Dim FPath, FileExtStr, DateString, xFile As String
Dim FileFormatNum As Long
Dim itemCount As Integer
Dim tempArray() As Variant
Dim result As Variant
Dim Filename As Variant
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False


For Each ws In ThisWorkbook.Sheets
ws.Copy
Application.ActiveWorkbook.SaveAs Filename:=FPath & "/" & ws.Name & " " & Split((DateDiff("s", "01/01/1970", Date) + Timer) * 1000, ".")(0) & Int((99999 - 1 + 1) * Rnd + 1) & ".csv"
Next
itemCount = 0
result = Filename
tempArray(Filename) = result
result = ""
itemCount = itemCount + 1
Application.ActiveWorkbook.Close False
generateMappingFile (tempArray)
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You get subscript out of bounds because your Variable "Filename" is empty. this is because you have never loaded anything into it. Your variable which you have called Filename is a completely different entity to the "filename" which is a parameter which is passed to the workbooksaveas method. It is NOT good practice to use variable names for any of the VBA or EXCEL functions of parameters
Try this :
VBA Code:
Sub SplitEachWorksheet()
Dim FPath, FileExtStr, DateString, xFile As String
Dim FileFormatNum As Long
Dim itemCount As Integer
Dim tempArray() As Variant
Dim result As Variant
Dim Fname As Variant
Dim wscnt As Integer
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
wscnt = Worksheets.Count
ReDim tempArray(1 To wscnt)
For Each ws In ThisWorkbook.Sheets
ws.Copy
Fname = FPath & "/" & ws.Name & " " & Split((DateDiff("s", "01/01/1970", Date) + Timer) * 1000, ".")(0) & Int((99999 - 1 + 1) * Rnd + 1) & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=Fname
itemCount = 1
'result = Fname
tempArray(itemCount) = Fname
'result = ""
itemCount = itemCount + 1
Next

Application.ActiveWorkbook.Close False
generateMappingFile (tempArray)
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Note I presume you have generatemappingfile subroutine that takes an array.
 
Upvote 0
You get subscript out of bounds because your Variable "Filename" is empty. this is because you have never loaded anything into it. Your variable which you have called Filename is a completely different entity to the "filename" which is a parameter which is passed to the workbooksaveas method. It is NOT good practice to use variable names for any of the VBA or EXCEL functions of parameters
Try this :
VBA Code:
Sub SplitEachWorksheet()
Dim FPath, FileExtStr, DateString, xFile As String
Dim FileFormatNum As Long
Dim itemCount As Integer
Dim tempArray() As Variant
Dim result As Variant
Dim Fname As Variant
Dim wscnt As Integer
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
wscnt = Worksheets.Count
ReDim tempArray(1 To wscnt)
For Each ws In ThisWorkbook.Sheets
ws.Copy
Fname = FPath & "/" & ws.Name & " " & Split((DateDiff("s", "01/01/1970", Date) + Timer) * 1000, ".")(0) & Int((99999 - 1 + 1) * Rnd + 1) & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=Fname
itemCount = 1
'result = Fname
tempArray(itemCount) = Fname
'result = ""
itemCount = itemCount + 1
Next

Application.ActiveWorkbook.Close False
generateMappingFile (tempArray)
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Note I presume you have generatemappingfile subroutine that takes an array.
Thank you so much for this.

This is my first time actually building a mapping file so I could use some help. What is your recommendation for the generatemappingfile subroutine to take an array and actual get the file?
 
Upvote 0
I think you probably need to raise another question about that because it is completely unrelated to the title of this thread.
By the way I spotted an error in my code. I have put the initialisation of itemcount in th wrong place try this:
VBA Code:
Sub SplitEachWorksheet()
Dim FPath, FileExtStr, DateString, xFile As String
Dim FileFormatNum As Long
Dim itemCount As Integer
Dim tempArray() As Variant
Dim result As Variant
Dim Fname As Variant
Dim wscnt As Integer
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
wscnt = Worksheets.Count
ReDim tempArray(1 To wscnt)
itemCount = 1                       ' move this up to here
For Each ws In ThisWorkbook.Sheets
ws.Copy
Fname = FPath & "/" & ws.Name & " " & Split((DateDiff("s", "01/01/1970", Date) + Timer) * 1000, ".")(0) & Int((99999 - 1 + 1) * Rnd + 1) & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=Fname
'result = Fname
tempArray(itemCount) = Fname
'result = ""
itemCount = itemCount + 1
Next

Application.ActiveWorkbook.Close False
generateMappingFile (tempArray)
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top