Rowland Hamilton
Active Member
- Joined
- Nov 13, 2009
- Messages
- 250
Folks:
If I get the file browser prompt and decide not to open a file, how can I get the macro to not perform actions on the current workbook? (Sometimes excel forces the actions on current workbook when you do not open a file as instructed by the macro). How do you stop that macro from running? Here is the code:
If I get the file browser prompt and decide not to open a file, how can I get the macro to not perform actions on the current workbook? (Sometimes excel forces the actions on current workbook when you do not open a file as instructed by the macro). How do you stop that macro from running? Here is the code:
Code:
Sub Pull_From_Source()
'Uses file browser for source workbook, array for tab choices, find row options
'Pulls data from source workbook
Dim MasterWB As Workbook
Dim SourceWB As Workbook
Dim Incoming As Worksheet
Dim rngSrc As Range
Dim rngDst As Range
Dim ccDst As Range
Dim ws As Worksheet
Dim varFileName As Variant
Dim LastRow As Long
Dim FirstRow As Long
Set MasterWB = ThisWorkbook
Set Incoming = Worksheets("Incoming")
''''''''''Clear MasterWB'''''''''''''''''''''''''''''''''''''''''''''''''''''
MasterWB.Activate
Incoming.Activate
Cells.Select
Selection.Clear
Range("a1").Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
varFileName = Application.GetOpenFilename(, , "Please select source workbook:")
If TypeName(varFileName) = "String" Then
Set SourceWB = Workbooks.Open(Filename:=varFileName, UpdateLinks:=0)
For Each ws In SourceWB.Sheets(Array("CC10001", "35ROAR2222", "555Stomp86", "CC5353"))
If ws.Visible <> xlSheetHidden Then
'Expand Column groups, Collapse Row groups - for other report
'ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=2
'copy
LastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
FirstRow = ws.Range("A5").End(xlDown).Row
Set rngSrc = ws.Range("A" & FirstRow).CurrentRegion.Offset(0, 0) 'this works
'Set rngSrc = ws.Range("A21").CurrentRegion.Offset(0, 0)'this works
'Set rngSrc = ws.Range("A" & FirstRow & ":M" & LastRow) 'this works
'Set rngSrc = ws.Range("A21:M" & LastRow) 'this works
'MsgBox FirstRow 'Test if it works
'paste
Set rngDst = Incoming.Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
rngSrc.SpecialCells(xlCellTypeVisible).Copy
rngDst.PasteSpecial Paste:=xlPasteValues
rngDst.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Set ccDst = Incoming.Range("A" & Rows.Count).End(xlUp).Offset(0)
ccDst.Formula = ws.Name
End If
Next ws
SourceWB.Close False
'MsgBox "Copied all data from source workbook"
Else
'MsgBox "No file selected"
End If
Application.Goto Incoming.Range("A1"), True
Selection.EntireRow.Delete
End Sub