ParaSitius
New Member
- Joined
- Nov 11, 2013
- Messages
- 11
Hello,
I need to update the following code that I haphazardly mashed together from several sources on the Internet to be a bit more flexible if rows from the 12M file are added or removed. I also require the code to be able to find the first empty row in the Report file as I have eight of these 12M files in total that need to be stacked going down the Report file.
To put some of the code in context.
1. I found that the Unmerge command is required as Columns A & B contain merged cells that cover several rows for each location, the filter command only works if they are unmerged first.
2. AutoFilter - Filters column C, allows me to than copy the data I require in other columns
3. SpecialCells - I now need this to copy from the first filtered row of data below the AutoFilter (row 9) to the last row containing data, minus 1 row (due to last row containing the overall total which isn't required here).
4. I need the first set of data to paste into Row 7 downwards, but for the remaining seven 12M files, I need them to find the first empty row. Example - First 12M file pastes into Row 7 to 95, the second 12M file will need to paste into Row 96 and so on.
If number 4 has to be spread across several Subs is fine as I have assigned eight macros to do this already with the current setup.
I know my coding is a bit of a mess but I'm a novice at VBA who is self-teaching himself so any help would be greatly appreciated.
I need to update the following code that I haphazardly mashed together from several sources on the Internet to be a bit more flexible if rows from the 12M file are added or removed. I also require the code to be able to find the first empty row in the Report file as I have eight of these 12M files in total that need to be stacked going down the Report file.
Code:
Sub LocationData()
Dim x As Workbook
Dim y As Workbook
'## Open 12M Data (x) and Report (y) workbooks first:
Set y = Workbooks.Open("File location\Report.xlsm")
Set x = Workbooks.Open("File location\12M Data.xls")
'##12M data copied first
'un-merge all the cells in the file to allow filtering
Range("a1:AA600").UnMerge
'Choses all rows that contain the word Denominator
Range("A9").AutoFilter Field:=3, Criteria1:="Denominator"
'Copies only the visible cells
Range("A10:A394").SpecialCells(xlCellTypeVisible).Copy
'Opens the Report before pasting
Set y = Workbooks.Open("File location\Report.xlsm")
'Pastes the values starting from cell D7
Workbooks("Report.xlsm").Worksheets("Location").Range("D7").PasteSpecial Paste:=xlPasteValues
Set x = Workbooks.Open("File location\12M Data.xls")
Range("A9").AutoFilter Field:=3, Criteria1:="Denominator"
Range("B10:B394").SpecialCells(xlCellTypeVisible).Copy
Set y = Workbooks.Open("File location\\Report.xlsm")
Workbooks("Report.xlsm").Worksheets("Location").Range("C7").PasteSpecial Paste:=xlPasteValues
Set x = Workbooks.Open("File location\12M Data.xls")
Range("A9").AutoFilter Field:=3, Criteria1:="Denominator"
Range("D10:D394").SpecialCells(xlCellTypeVisible).Copy
Set y = Workbooks.Open("File location\\Report.xlsm")
Workbooks("Report.xlsm").Worksheets("Location").Range("F7").PasteSpecial Paste:=xlPasteValues
Set x = Workbooks.Open("File location\12M Data.xls")
Range("A9").AutoFilter Field:=3, Criteria1:="Item 1"
Range("D11:D395").SpecialCells(xlCellTypeVisible).Copy
Set y = Workbooks.Open("File location\\Report.xlsm")
Workbooks("Report.xlsm").Worksheets("Location").Range("G7").PasteSpecial Paste:=xlPasteValues
Set x = Workbooks.Open("File location\12M Data.xls")
Range("A9").AutoFilter Field:=3, Criteria1:="Item 2"
Range("D13:D397").SpecialCells(xlCellTypeVisible).Copy
Set y = Workbooks.Open("File location\\Report.xlsm")
Workbooks("Report.xlsm").Worksheets("Location").Range("I7").PasteSpecial Paste:=xlPasteValues
Set x = Workbooks.Open("File location\12M Data.xls")
Range("A9").AutoFilter Field:=3, Criteria1:="Item 3"
Range("D14:D398").SpecialCells(xlCellTypeVisible).Copy
Set y = Workbooks.Open("File location\\Report.xlsm")
Workbooks("Report.xlsm").Worksheets("Location").Range("K7").PasteSpecial Paste:=xlPasteValues
'Close x - 12M workbook:
'x = EmptyClipboard()
Application.CutCopyMode = False
x.Close SaveChanges:=False
To put some of the code in context.
1. I found that the Unmerge command is required as Columns A & B contain merged cells that cover several rows for each location, the filter command only works if they are unmerged first.
2. AutoFilter - Filters column C, allows me to than copy the data I require in other columns
3. SpecialCells - I now need this to copy from the first filtered row of data below the AutoFilter (row 9) to the last row containing data, minus 1 row (due to last row containing the overall total which isn't required here).
4. I need the first set of data to paste into Row 7 downwards, but for the remaining seven 12M files, I need them to find the first empty row. Example - First 12M file pastes into Row 7 to 95, the second 12M file will need to paste into Row 96 and so on.
If number 4 has to be spread across several Subs is fine as I have assigned eight macros to do this already with the current setup.
I know my coding is a bit of a mess but I'm a novice at VBA who is self-teaching himself so any help would be greatly appreciated.