Macro Combine Worksheets and Tab Names

JesC929

New Member
Joined
Jul 31, 2017
Messages
2
Hi,

I'm trying to combine data that I have on 62 worksheets in my workbook. I've got the Macro working that pulls in my data from cells A4:P:66, and only puss in the info if they have data, but I can't figure out how to add the worksheet name to the end of the data on my "Import" worksheet, so in column Q, that way it has an indicator for me, so I know what worksheet the data actually came from. Any help would be much appreciated. Here is the macro I currently have:

Public Sub LaborFTE()

Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long

Set wksDst = ThisWorkbook.Worksheets("Import")
lngDstLastRow = LastOccupiedRowNum(wksDst) '
lngLastCol = LastOccupiedColNum(wksDst) '

Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

For Each wksSrc In ThisWorkbook.Worksheets

If wksSrc.Name <> "Import" Then

lngSrcLastRow = LastOccupiedRowNum(wksSrc)

With wksSrc
Set rngSrc = .Range(.Cells(4, 1), .Cells(166, 16))
rngSrc.Copy Destination:=rngDst
End With

lngDstLastRow = LastOccupiedRowNum(wksDst)
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

End If

Next wksSrc

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Add the line of code below that starts with Range between

Code:
lngDstLastRow = LastOccupiedRowNum(wksDst)
Range(.Cells(rngDst, 1), .Cells(lngDstLastRow, 16)).Value = wksSrc.Name
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)

Also it may have just been a typo but you mention copying Rows 4 through 66 but your code copies 4 through 166.
 
Upvote 0
Hi Frank,

Yes it was a typo, should've been 166. As for your solution, I'm not 100% sure where I should be adding that line of code. I've tried it a few different places and it rejects the macro.
 
Upvote 0
JesC,

Okay, I made a couple of modifications and here is the complete code:
In my test workbook it did put the worksheet name in Column Q on the Import worksheet.

I didn't have your lastoccupiedrownum and colnum Function so I modified that to find the last row/col. See Rows of code that are Underlined. You can change that back to use your Function.


Code:
Public Sub LaborFTE()


Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long


Set wksDst = ThisWorkbook.Worksheets("Import")
[U]lngDstLastRow = wksDst.Cells(Rows.Count, "B").End(xlUp).Row[/U]
[U]lngLastCol = wksDst.Cells(18, Columns.Count).End(xlToLeft).Column[/U]


Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)


For Each wksSrc In ThisWorkbook.Worksheets


If wksSrc.Name <> "Import" Then


[U]lngSrcLastRow = wksSrc.Cells(Rows.Count, "B").End(xlUp).Row[/U]


With wksSrc
Set rngSrc = .Range(.Cells(4, 1), .Cells(166, 16))
rngSrc.Copy Destination:=rngDst
End With


lngDstLastRow = wksDst.Cells(Rows.Count, "B").End(xlUp).Row
[FONT=arial black]With wksDst[/FONT]
[FONT=arial black]    Range(Cells(lngDstLastRow + 1, 17), Cells(lngDstLastRow + 163, 17)).Value = wksSrc.Name[/FONT]
[FONT=arial black]End With[/FONT]
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)


End If


Next wksSrc


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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