1

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?

Parseltongue
  • 11,157
  • 30
  • 95
  • 160
  • Try recording a macro and see what code do you come up with. You can then modify that code to then achieve what you want. Give it a try and if you are stuck simply post what you tried and we will take it from there? – Siddharth Rout Aug 03 '12 at 00:44
  • The deed is done-- thanks for the advice. Let me know what else I can do to make the code work. – Parseltongue Aug 03 '12 at 03:49
  • Gr8. Now all you have to do is to simply loop through the worksheets and copy the relevant "Range" and copy it to the `Sheets("Table 1")`. Wanna give that a try too ;)? – Siddharth Rout Aug 03 '12 at 03:51
  • You're some kind of wizard. I did it. Thanks! – Parseltongue Aug 03 '12 at 04:10
  • 1
    `I did it. Thanks!` I know this feeling ;) Ok Now that you have got the logic and the code working... Few improvements :) Instead of `.Select` and `.Activate` directly interact with the range. You may want to see this link. http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select – Siddharth Rout Aug 03 '12 at 04:16
  • Once you manage to incorporate the above suggestion I will then suggest 2 new improvements :) – Siddharth Rout Aug 03 '12 at 04:17
  • Hmm. Alright, I think I've done it, but I'm having trouble avoiding selections on the second half of the code (when I need to use the 'End' key to get to the last cell with content). Any ideas? – Parseltongue Aug 03 '12 at 04:32
  • Gimme 15 mins... framing an answer... – Siddharth Rout Aug 03 '12 at 04:39
  • `Well, I hate to admit I'm glad you didn't just spoonfeed me the answer. Good on you, sir. ` + 1 For your Efforts and willingness to learn :) – Siddharth Rout Aug 03 '12 at 04:55

1 Answers1

2

See this code.

  1. I modified your code so that it doesn't use .Select or .Activate at all.
  2. I have commented the code so you shouldn't have a problem understanding it. :)
  3. The code doesn't use On Error Resume Next. You should always avoid that unless it is necessary. Use proper error handling instead. Consider On Error Resume Next as telling your application to simply SHUT UP. :)

Here is an example of basic error handling

Sub Sample()
    On Error GoTo Whoa

    '
    '~~> Rest of Code
    '

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub

So this is how your final code will look like. It avoids the use of .Select or .Activate. It also avoids the use of Selection 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.

Option Explicit

Sub Sample()
    Dim wsInput As Worksheet, wsOutput As Worksheet
    Dim rng As Range
    Dim LRowO As Long, LRowI As Long

    On Error GoTo Whoa

    '~~> Set your Output Sheet
    Set wsOutput = ThisWorkbook.Sheets("Table 1")

    '~~> Loop through all sheets
    For Each wsInput In ThisWorkbook.Worksheets
        '~~> Ensure that we ignore the output sheet
        If wsInput.Name <> wsOutput.Name Then
            '~~> Working with the input sheet
            With wsInput
                '~~> Get the last row of input sheet
                LRowI = .Range("A" & .Rows.Count).End(xlUp).Row
                '~~> Set your range for copying
                Set rng = .Range("A1:B" & LRowI)
                '~~> Copy your range
                rng.Copy
                '~~> Pasting data in the output sheet
                With wsOutput
                    '~~> Get the next available row in output sheet for pasting
                    LRowO = .Range("A" & .Rows.Count).End(xlUp).Row + 1

                    '~~> Finally paste
                    .Range("A" & LRowO).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, _
                    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                End With
            End With
        End If
    Next wsInput

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • This is brilliant. I never knew you could use range with the & sign like you did. Great! It means a lot that you would take the time to teach me all this. Thanks again, and take care! – Parseltongue Aug 03 '12 at 04:56
  • I am just passing on the favor ;) Many many many years ago there were many experts on forums like this who patiently explained every thing to me :) – Siddharth Rout Aug 03 '12 at 04:58