Excel VBA to stack columns in different sheets


Excel VBA to stack columns in different sheets



I have an Excel workbook with 2 sheets inside. Both sheets have tables that can change in length (row count). I want to be able to have VBA take table1 in sheet1 and align it within table2 on sheet2.



Here's where it gets tricky. The columns do not line up. For example, "Company" in sheet1 might be in column E. However, in sheet2 it's in column V. There are also blanks in the columns. Which is ok as far as data goes, but it's going to make getting to the end of the table's column harder.



I know I'm going to need to list out which columns go where for each column. In sheet1 there are 36 columns. In sheet 2 there are 49. This is perfectly acceptable.



Any guidance would be greatly appreciated. It takes an insane amount of time to do this manually and I'm just trying to speed it up. If it's easier to get everything to compile on sheet3, that's fine.



Here is what I have so far, but it's not liking the stacking part.


Sub Stacker()

'Select Full Report Sheet
Sheets("Full Report").Select

'Find Notes column and copy
Cells.find(What:="Notes", After:=Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows).EntireColumn.Copy

'Select Secondary Report sheet, column E and paste
Sheets("Secondary Report").Select
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub



Thank you





What do you mean exactly by 'tables' and 'Table1' and 'Table2'? Are these structured (i.e. ListObject) tables?
– Jeeped
Jun 29 at 20:25





Are all of the Table 1 columns present in Table 2? Are you copying all columns from Table 1?
– Tim Williams
Jun 29 at 23:27





What do mean by "stack" and "to have VBA take table1 in sheet1 and align it within table2 on sheet2"? Are you trying to get both tables to appear on the same page, with all shared columns lined up one above the next? Or, are you just trying to reorder the columns where they are and leave each table on its own sheet or ... something else?
– SeanW333
Jun 30 at 0:54





You may be able to use powerquery and append the tables. As the columns do not vary you can do the column re-ordering on one of the tables so the line up (not sure if powerquery even required you to do this if they have matching headers - so try without first) You may need to add dummy columns to the shorter width table. Again, powerquery may handle this for you or you can use its column add feature.
– QHarr
Jun 30 at 3:35





2 Answers
2



This looks like a job for a Super Dictionary!!!


Function DictionaryStacker()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim dic As Object
Dim cell As Range
Set dic = CreateObject("Scripting.Dictionary")

With Sheets("Secondary Report")
For Each cell In .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))
'Add a reference to the first empty cell in the column
Set dic(cell.Value) = cell(.Rows.Count).End(xlUp).Offset(1)
Next
End With

With Sheets("Full Report")
For Each cell In .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))
If dic.Exists(cell.Value) Then
'Reference data in column starting at Row 2
With .Range(cell.Offset(1), cell(.Rows.Count).End(xlUp))
'Append the referenced Range on Full Reports to the Range stored in the Dictionary
dic(cell.Value).Resize(.Rows.Count).Value = .Value
End With
End If
Next
End With

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Function



I've previously written code that lets you mash all your data from seperate tables into one table, no matter the order of the columns. See https://stackoverflow.com/a/47279374/2507160



I'd also suggest trying out PowerQuery to do the same. See https://stackoverflow.com/a/47170312/2507160






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux