Subscript Out of range VBA Error

trinidad2011

New Member
Joined
May 8, 2011
Messages
13
Hi all,
If it is possible, i would like to know what the 'Subscript out of range error means' when trying to compile a VBA code?
I changed the code that i pasted below from what i found on the internet, according to my needs.

Does anyone know what this means? When i hit the debug button it highlights this line:

Set wsMaster = ThisWorkbook.Sheets("Master") 'sheet report is built into


Here is my code
Sub Consolidate()

'Summary: Prompt user to open folder
'Open file, delete unwanted data, filter latitudes
'Longitudes to fit South Australian region

Dim fName As String, fPath As String, fPathDone As String
Dim LR As Long, NR As Long
Dim wbData As Workbook, wsMaster As Worksheet

'Setup
Application.ScreenUpdating = False 'speed up macro execution
Application.EnableEvents = False 'turn off other macros for now
Application.DisplayAlerts = False 'turn off system messages for now

Set wsMaster = ThisWorkbook.Sheets("Master") 'sheet report is built into

With wsMaster
If MsgBox("Clear the o data first?", vbYesNo) = vbYes Then
.Cells.Clear
NR = 1
Else
NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'appends data to existing data
End If

'Path and filename
'Prompting user to choose the required folder

MsgBox "Please select a folder with files to consolidate"
Do
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
fPath = .SelectedItems(1) & "\"
Exit Do
Else
If MsgBox("No folder chosen, do you wish to exit Macro?", _
vbYesNo) = vbYes Then Exit Sub
End If
End With
Loop

fPathDone = fPath & "Imported\" 'remember final \ in this string
On Error Resume Next
MkDir fPathDone 'creates the completed folder if missing
On Error GoTo 0
fName = Dir(fPath & "*.csv*") 'listing of desired files


'Import a sheet from found files
Do While Len(fName) > 0
If fName <> ThisWorkbook.Name Then 'don't reopen this file accidentally
Set wbData = Workbooks.Open(fPath & fName) 'Open file

'This is the section to customize, replace with your own action code as needed

Columns("F:I").Select
Selection.ClearContents
Columns("A:E").Select
Selection.AutoFilter
Columns("C:C").Select 'Filter Latitude
ActiveSheet.Range("$A$1:$E$31001").AutoFilter Field:=3, Criteria1:= _
">=-37.9382", Operator:=xlAnd, Criteria2:="<=-32.004"
Columns("D:D").Select 'Filter Longitude
ActiveSheet.Range("$A$1:$E$31001").AutoFilter Field:=4, Criteria1:= _
">=136.7754", Operator:=xlAnd, Criteria2:="<=141.0698"
Cells.Select
Selection.Copy
Workbooks.Add

wbData.Close False 'close file
NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'Next row
Name fPath & fName As fPathDone & fName 'move file to IMPORTED folder
fName = Dir 'ready next filename
End If
Loop
End With

ErrorExit: 'Cleanup
ActiveSheet.Columns.AutoFit
Application.DisplayAlerts = True 'turn system alerts back on
Application.EnableEvents = True 'turn other macros back on
Application.ScreenUpdating = True 'refreshes the screen
End Sub
I will be delighted if someone can find the error or kindly explain what the error means
 
Hi!
the problem has been resolved. thank you so much for your help :)
the macro stopped running everytime the filter returned empty cells under the specified conditions, so i just added a loop to overcome that problem.
this forum has been so helpful i can not describe how grateful i am for having found it,
Thanks again
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,
Can anybody tell me what is wrong with my program below that I get the " Subscript Out of range" Error?

When I debug the program it highlights this line:
oExcel.Workbooks("ActiveFactoryWorkbook.xlt").RunAutoMacros1



This is my code: I am new to Macro so any help is appreciated.

Sub Macro1()

Dim oExcel
Dim oBook
Dim savepath
Dim savefolder
Dim savefolder2
Dim CurrentMonth
Dim CurrentDay
Dim ReportMonth
Dim RMName
Dim ReportYear
Dim objFS


CurrentDay = Day(Date)
CurrentMonth = Month(Date)




If CurrentDay = 1 Then
ReportMonth = Month(Date) - 1
If CurrentMonth = 1 Then
ReportMonth = 12
ReportYear = Year(Date) - 1
Else
ReportYear = Year(Date)
End If
Else
ReportMonth = Month(Date)
ReportYear = Year(Date)
End If


RMName = MonthName(ReportMonth, 0)






'--Start Excell and set visibility


Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False




'--Path to Excel workbook and open workbook


sSourceFile = "E:\Reports\Report Templates\Daily Report.xls"


st = "C:\Program Files\Common Files\ArchestrA\ActiveFactoryWorkbook.xlt"
Set oBook = oExcel.Workbooks.Open(st)
oExcel.Workbooks("ActiveFactoryWorkbook.xlt").RunAutoMacros1


oExcel.Workbooks.Add sSourceFile




x = Timer()
Do While x + 2 > Timer()
Loop




'--Runs SQLQuery script in workbook


oExcel.Run ("ThisWorkbook.SQLQuery")


'--Path for folder and sub-folder


savefolder = "E:\Reports\Daily\" & ReportYear
savefolder2 = "E:\Reports\Daily\" & ReportYear & "\" & RMName




'--Path to save to


savepath = "E:\Reports\Daily\" & ReportYear & "\" & RMName & "\" & "Daily Report " & Day(Date) & " " & Hour(Time()) & " " & Minute(Time()) & ".xls"




'--Checks to see if the folder and sub-folder exist, if not it creates them


Set objFS = CreateObject("Scripting.filesystemobject")


If Not objFS.folderexists(savefolder) Then
objFS.CreateFolder (savefolder)
End If


If Not objFS.folderexists(savefolder2) Then
objFS.CreateFolder (savefolder2)
End If




'--Runs Cleanup script in workbook


oExcel.Run ("ThisWorkbook.Cleanup")




'--Saves workbook to save path


oExcel.Workbooks(1).SaveAs savepath


'--Closes workbook and Excel


oExcel.Workbooks(1).Close
oExcel.Quit
Set oExcel = Nothing
Set oBook = Nothing
End Sub
 
Upvote 0
I know this thread is dated, but I'm trying to use the same code as above and I'm receiving the same error message with the same line highlighted. I'm running the macro on a blank file with a single sheet titled "Master" (without quotations). I'm on Excel 2010. Any help is greatly appreciatied!
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,427
Members
452,914
Latest member
echoix

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