Copy and paste from numerous tabs into a single column

junkforhr

Board Regular
Joined
Dec 16, 2009
Messages
115
Office Version
  1. 365
Platform
  1. Windows
I have an excel spreadsheet with tabs called, DSC,FAO, YAS, EMS, RET. I want to reference the sheets that need to be copied. So on the “Lookup” sheet in cell A1 the value will be “EMS”, in cell A2 the values will be “FAO”. I want the code to cater for new sheets to copy without it be hard coded in the VBA.



So the first sheet to be copied will be EMS, which will be in cell A1 on the “lookup” sheet, the code should start looking at the values from G2 and go look at all the any cell that has a value and copy this. This then should be copied onto the “Lookup” sheet and paste into cell B2. The code should then move onto the next sheet which will be refenced in cell A2 on the “Lookup” sheet. It will then copy all cells with values start from cell G2. The code then should append this copied data below the previously copied data. Then it should then repeat the process for sheets that are reference in A3 etc.



Example of EMS tab

Header1Header2Header3Header4Header5Header6Header7Header8
Text1Text8Text15Text22Text29Text36Text43Text50
Text2Text9Text16Text23Text30Text37Text44Text51
Text3Text10Text17Text24Text31Text38Text45Text52
Text4Text11Text18Text25Text32Text39Text46Text53
Text5Text12Text19Text26Text33Text40Text47Text54
Text6Text13Text20Text27Text34Text41Text48Text55
Text7Text14Text21Text28Text35Text42Text49Text56


Example of FAO tab



Header1Header2Header3Header4Header5Header6Header7Header8
Tex57ABC1FGD1HJU1PPP3HYT89JHY321HAHSAHS5
Tex58ABC2FGD2HJU2PPP4HYT90JHY322HAHSAHS6
Tex59ABC3FGD3HJU3PPP5HYT91JHY323HAHSAHS7
Tex60ABC4FGD4HJU4PPP6HYT92JHY324HAHSAHS8
Tex61ABC5FGD5HJU5PPP7HYT93JHY325HAHSAHS9
Tex62ABC6FGD6HJU6PPP8HYT94JHY326HAHSAHS10
Tex63ABC7FGD7HJU7PPP9HYT95JHY327HAHSAHS11


Desired outcome for the examples provided on the tab called Lookup in starting from B2

Lookup
Tex57
Tex58
Tex59
Tex60
Tex61
Tex62
Tex63
ABC1
ABC2
ABC3
ABC4
ABC5
ABC6
ABC7
FGD1
FGD2
FGD3
FGD4
FGD5
FGD6
FGD7
HJU1
HJU2
HJU3
HJU4
HJU5
HJU6
HJU7
PPP3
PPP4
PPP5
PPP6
PPP7
PPP8
PPP9
HYT89
HYT90
HYT91
HYT92
HYT93
HYT94
HYT95
JHY321
JHY322
JHY323
JHY324
JHY325
JHY326
JHY327
HAHSAHS5
HAHSAHS6
HAHSAHS7
HAHSAHS8
HAHSAHS9
HAHSAHS10
HAHSAHS11
Text1
Text2
Text3
Text4
Text5
Text6
Text7
Text8
Text9
Text10
Text11
Text12
Text13
Text14
Text15
Text16
Text17
Text18
Text19
Text20
Text21
Text22
Text23
Text24
Text25
Text26
Text27
Text28
Text29
Text30
Text31
Text32
Text33
Text34
Text35
Text36
Text37
Text38
Text39
Text40
Text41
Text42
Text43
Text44
Text45
Text46
Text47
Text48
Text49
Text50
Text51
Text52
Text53
Text54
Text55
Text56
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

Below is VBA that will answer you problem.

Note no frills vba. It does not check if worksheet exists or if sheet has no data.

Was not sure if I understand your question. If I've got it wrong let me know where i went wrong and will correct it.

VBA Code:
Sub getData()
    Dim lastrowA As Long
    Dim wsLU As Worksheet
    Dim i As Long
    Dim b As Long
    Dim wsDataSheet As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long
    Dim dr As Long
    Dim dc As Long
    Set wsLU = ThisWorkbook.Worksheets("Lookup")
   
        'delete Lookup Row "B"
   
    wsLU.Columns(2).Delete

    b = 2
    lastrowA = wsLU.Cells(wsLU.Rows.Count, "A").End(xlUp).Row
   
    'loop through each worksheet in column 'A'
   
   
   
   
    For i = 1 To lastrowA
       
        Set wsDataSheet = ThisWorkbook.Worksheets(wsLU.Cells(i, 1).Value)
   
        lastRow = LastRowColumn(wsDataSheet, "R")
        lastCol = LastRowColumn(wsDataSheet, "C")
       
        For dc = 1 To lastCol
            For dr = 2 To lastRow
                wsLU.Cells(b, 2) = wsDataSheet.Cells(dr, dc).Value
                b = b + 1
            Next dr
        Next dc
   
   
   
   
    Next i
   
   

End Sub
Function LastRowColumn(sht As Worksheet, RowColumn As String) As Long
'PURPOSE: Function To Return the Last Row Or Column Number In the Active Spreadsheet
'INPUT: "R" or "C" to determine which direction to search

    Select Case LCase(Left(RowColumn, 1)) 'If they put in 'row' or column instead of 'r' or 'c'.
      Case "c"
        LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious).Column
      Case "r"
        LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious).Row
      Case Else
        LastRowColumn = 1
    End Select

End Function
 
Last edited:
Upvote 0
You can use Power Query to append each of the sheets and then unpivot the data.
Here is the Mcode once each table has been loaded.

Power Query:
let
    Source = Table.Combine({Table1, Table2}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})
in
    #"Removed Columns"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top