Hello All!
I am setting up a macro to take raw data from one workbook and move it to another workbook, while putting the columns in a specific, but different order, using a configuration table. Here is a basic example of what I am trying to accomplish:
Raw Data - Workbook "wa", Sheet "Data" - starts in Range A1
Config Sheet - Workbook "WB", Sheet "Config" - text starts in Range A6
Output Sheet - Workbook "WB", Sheet "New Data" - starts in Range A1
Here is the code I currently have:
The code runs fine up until the line:
WB.Sheets("New Data").Range(Cells(1, Col), Cells(last_row, Col)).Value = wa.Sheets("Data").Range(Cells(1, rngFound.Column), Cells(last_row, rngFound.Column)).Value
Then I get a Run-time error 1004: Application-defined or object-defined error.
Hoping someone here can help straighten me out!!
Thanks in advance!
I am setting up a macro to take raw data from one workbook and move it to another workbook, while putting the columns in a specific, but different order, using a configuration table. Here is a basic example of what I am trying to accomplish:
Raw Data - Workbook "wa", Sheet "Data" - starts in Range A1
Apples | Grapes | Strawberries | Blueberries | Peaches |
1 | 4 | 3 | 6 | 7 |
2 | 5 | 2 | 8 | 5 |
3 | 6 | 1 | 10 | 3 |
4 | 7 | 0 | 12 | 1 |
5 | 8 | -1 | 14 | -1 |
6 | 9 | -2 | 16 | -3 |
7 | 10 | -3 | 18 | -5 |
8 | 11 | -4 | 20 | -7 |
9 | 12 | -5 | 22 | -9 |
10 | 13 | -6 | 24 | -11 |
Config Sheet - Workbook "WB", Sheet "Config" - text starts in Range A6
Strawberries | |||
Grapes | |||
Blueberries | |||
Apples | |||
Peaches | |||
Output Sheet - Workbook "WB", Sheet "New Data" - starts in Range A1
Strawberries | Grapes | Blueberries | Apples | Peaches |
3 | 4 | 6 | 1 | 7 |
2 | 5 | 8 | 2 | 5 |
1 | 6 | 10 | 3 | 3 |
0 | 7 | 12 | 4 | 1 |
-1 | 8 | 14 | 5 | -1 |
-2 | 9 | 16 | 6 | -3 |
-3 | 10 | 18 | 7 | -5 |
-4 | 11 | 20 | 8 | -7 |
-5 | 12 | 22 | 9 | -9 |
-6 | 13 | 24 | 10 | -11 |
Here is the code I currently have:
VBA Code:
Dim last_row, last_col As Long
Dim includedCol As Integer
Dim rngFound As Range
Dim Col As Integer
wa.Sheets("Data").Select
last_row = Cells(Rows.Count, 1).End(xlUp).Row
last_col = Cells(1, Columns.Count).End(xlToLeft).Column
includedCol = 6
colString = ""
Col = 1
Do While WB.Sheets("Config").Cells(includedCol, 1) <> ""
'If WB.Worksheets("Config").Cells(includedCol, 1) = "" Then
Set rngFound = wa.Sheets("Data").Range("1:1").Find(What:=WB.Sheets("Config").Cells(includedCol, 1), LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=True)
'End If
If Not rngFound Is Nothing Then
WB.Sheets("New Data").Range(Cells(1, Col), Cells(last_row, Col)).Value = wa.Sheets("Data").Range(Cells(1, rngFound.Column), Cells(last_row, rngFound.Column)).Value
End If
Col = Col + 1
includedCol = includedCol + 1
Loop
The code runs fine up until the line:
WB.Sheets("New Data").Range(Cells(1, Col), Cells(last_row, Col)).Value = wa.Sheets("Data").Range(Cells(1, rngFound.Column), Cells(last_row, rngFound.Column)).Value
Then I get a Run-time error 1004: Application-defined or object-defined error.
Hoping someone here can help straighten me out!!
Thanks in advance!