How to Copy Sheet Names from a Source Workbook

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hi. I have here a code that I need to do some tweaking. My problem is what additional code is appropriate wherein I could copy the Sheet Names where the data came from (srcWB) into the destination file (desWS).

Here's a sample of the desired output, also see below code for reference. Thanks!

1599484149027.png


VBA Code:
Private Sub CopyEachPrcs()
    Application.ScreenUpdating = False
    Dim lastRow As Long, srcWB As Workbook, desWS As Worksheet
    Set srcWB = ThisWorkbook
    Set desWS = Workbooks("final data.xlsm").Sheets(1)
    With srcWB
        With .Sheets(1)
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("A2:C" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
        With .Sheets(2)
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("B2:B" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("A2:A" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("C2:C" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "C").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hello

You don't say where in the destination you'd like it copying to. But the sheet name of srcWB would be
VBA Code:
srcWb.Sheets(1).Name
 
Upvote 0
Hello

You don't say where in the destination you'd like it copying to. But the sheet name of srcWB would be
VBA Code:
srcWb.Sheets(1).Name


Im sorry I haven't added them, the ones with apostrophe are the trial code I am tweaking. I need them to be copied on column D of destination file.

VBA Code:
Private Sub CopyEachPrcs()
    Application.ScreenUpdating = False
    Dim lastRow As Long, srcWB As Workbook, desWS As Worksheet
    Set srcWB = ThisWorkbook
    Set desWS = Workbooks("final data.xlsm").Sheets(1)
    With srcWB
        With .Sheets(1)
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("A2:C" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            'desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).Value = srcWB.Sheets(1).Name
        End With
        With .Sheets(2)
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("B2:B" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("A2:A" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("C2:C" & lastRow).Copy
            desWS.Cells(desWS.Rows.Count, "C").End(xlUp).Offset(1).PasteSpecial xlPasteValues
           'desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).Value = srcWB.Sheets(2).Name
        End With
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi anyone who could assist to correct this code? I think this one contains the logic I need to do however it does not run properly.
Any help would be much appreciated. Thanks!


VBA Code:
desWS.Range("D & Range(""D"" & Rows.Count).End(xlUp).Offset(1):D & lastRow").Value = "Sheets(1).Name"
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,375
Members
448,955
Latest member
BatCoder

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