Hi All
I'm completely stumped here. I've written the following macro for a Scorecard workbook I'm creating. The basics of what it's meant to do are:
* have one workbook to collate data from various other workbooks
* main workbook (called Launchpad), has a "Data" tab, which lists all of the file names from which to pull data
* on click of a button, the macro starts
* the first thing it does is go to the "Master" tab, and clear any info from the table (so that it gets refreshed)
* it then opens the first workbook from cell A1 in the Data tab
* in the now open workbook it goes to a designated cell, filters out the blanks, selects all data in the table and copies it
* it then goes to the "Master" tab in the Launchpad workbook and pastes the data
* it then returns to the other workbook, unfilters the table (putting the blanks back in), saves and closes that workbook
* lastly it returns again to Launchpad to the Data tab and moves down one cell to the next file name
* a loop then starts where the workbook opens the next file, filters, copies, returns, pastes etc.
* this is meant to occur in a loop until a blank cell is reached on the Data tab
Now, the issue I'm having is:
* if I run the macro from VBA, it works one time, going through the loop as intended. The next time I run it, it seems to only go once, stopping after the first run of code (before the loop starts). If I run again, it works. Every second time ...
* if I run the macro from the button click, as intended, it also runs through once, stopping before the loop (i.e. only one workbook's data is grabbed). It never loops from the button.
The code is:
Private Sub Picture9_Click()
Dim x As Integer
Dim LastDataRow As String
LastRow = Range("A" & Rows.Count).End(xlUp).Row - 1
Worksheets("Master").Activate
Range("B3:R500").ClearContents
Worksheets("Data").Activate
Range("A1").Select
Workbooks.Open Filename:=ActiveCell, UpdateLinks:=True
ActiveSheet.Range("$B$7:$S$27").AutoFilter Field:=1, Criteria1:="<>"
Sheets("Activity Summary").Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("F13 Launchpad Scorecard").Activate
Worksheets("Master").Activate
Sheets("Master").Range("B1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.Workbooks(2).Activate
ActiveSheet.Range("$B$7:$S$27").AutoFilter Field:=1
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("F13 Launchpad Scorecard").Activate
Worksheets("Data").Activate
ActiveCell.Offset(1, 0).Select
For x = 1 To LastRow
Workbooks.Open Filename:=ActiveCell, UpdateLinks:=True
ActiveSheet.Range("$B$7:$S$27").AutoFilter Field:=1, Criteria1:="<>"
Sheets("Activity Summary").Range("A8").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("F13 Launchpad Scorecard").Activate
Worksheets("Master").Activate
Sheets("Master").Range("B2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.Workbooks(2).Activate
ActiveSheet.Range("$B$7:$S$27").AutoFilter Field:=1
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("F13 Launchpad Scorecard").Activate
Worksheets("Data").Activate
ActiveCell.Offset(1, 0).Select
Next
End Sub
Any help would be much appreciated. Thanks guys!!!
Bookmarks