I have an excel spreadsheet with 75 tabs-- each tab is formatted in the same way with two columns of words. I want all of this data to be on just a single page, but I don't know how to programmatically extract tables from each tab and paste it on a single tab.
Is there a way to do this in Excel?
Alright, here's the code that I've tried:
Sub Macro5()
Range("A1:B30").Select
Selection.Copy
Sheets("Table 1").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
End Sub
All tabs are formatted in the same way, with data in all cells from A1:B30. I'm thinking that the Selection.End command would go to the next available open cell and paste data from the subsequent tabs in that.
As of current, I would need to go to each tab and individually run this macro, except that it doesn't work because it says the pasted data is not of the same type/format of the existing data.
Any ideas?
Coding attempt #2- SUCCESS!!!
Sub Macro5()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.activate
Range("A1:B30").Select
Selection.Copy
Sheets("Table 1").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
On Error Resume Next 'Will continue if an error results
Next ws
End Sub
Well, I hate to admit I'm glad you didn't just spoonfeed me the answer. Good on you, sir.
Coding Attempt #3- Avoid Selections
Sub Macro5()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Set Rng = ws.Range("A1:B30")
Rng.Copy
Dim ws1 As Worksheet
Set ws1 = Worksheets("Table 1")
ws1.Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
On Error Resume Next 'Will continue if an error results
Next ws
End Sub
Not quite right-- it still works, but I'm not sure how to avoid using "Selection" when I get to the first workbook. Is there a way to reference the most proximate cell without content? I know the 'End' key can do this, but is there a non-selection based way?
See this code.
.Select
or.Activate
at all.On Error Resume Next
. You should always avoid that unless it is necessary. Use proper error handling instead. ConsiderOn Error Resume Next
as telling your application to simply SHUT UP. :)Here is an example of basic error handling
So this is how your final code will look like. It avoids the use of
.Select
or.Activate
. It also avoids the use ofSelection
and finds the exact range that needs to be copied and exact range where it needs to be copied. Also it does proper error handling.