Copy Range Based on Sheet Name VBA

mdonovan890

New Member
Joined
Dec 22, 2016
Messages
24
i'm New VBA and Marcos and am hoping someone can help. I have workbook with 50 plus sheets and I am trying to create a Macro that will copy the same range of cells on all sheets that have "Box" in the in the name and copy it to a Master Sheet in the same workbook with the originating sheet name shown in column J.


The issue is that this code copies all sheets within the workbook, the source column does not populate correctly and none of the formatting is copied. I am using Excel 2016 any help would be greatly appreciated.

After a lot of searching, this is what I have so far:

Public Sub m()
Dim lRow As Long
Dim sh As Worksheet
Dim shArc As Worksheet
Set shArc = ThisWorkbook.Worksheets("Archive")
For Each sh In ThisWorkbook.Worksheets
Select Case sh.Name
Case Is <> "Archive"
lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row + 1
sh.Range("A2:I110").Copy
shArc.Range("A" & lRow).PasteSpecial
DestSh.Cells(Last + 1, "J").Resize(CopyRng.Rows.Count).Value = sh.Name
End Select
Next
Application.CutCopyMode = False
Set shArc = Nothing
Set sh = Nothing
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Here is an untested modifcation

Code:
Sub box()
    Dim sh As Worksheet
    Dim lRow As Long
    Dim shArc As Worksheet
    Set shArc = ThisWorkbook.Worksheets("Archive")


    For Each sh In Worksheets
        If InStr(sh.Name, "Box") > 0 Then
            lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row + 1
            sh.Range("A2:I110").Copy
            shArc.Range("A" & lRow).PasteSpecial
            DestSh.Cells(Last + 1, "J").Resize(CopyRng.Rows.Count).Value = sh.Name
        End If
    Next ws
    Application.CutCopyMode = False
    MsgBox "Complete"


End Sub

Post back with issues or success story
 
Upvote 0
Not sure if I'm clear on all your requirements but U can trial...
Code:
Public Sub m()
Dim lRow As Double
Dim sh As Worksheet
Dim shArc As Worksheet
Set shArc = ThisWorkbook.Worksheets("Archive")
For Each sh In ThisWorkbook.Worksheets
If InStr("Box", sh.Name) Then
lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row
sh.Range("A2:I110").Copy
shArc.Range("A" & lRow + 1).PasteSpecial xlPasteValuesAndNumberFormats
shArc.Range("J" & lRow + 1) = sh.Name
Application.CutCopyMode = False
End If
Next
End Sub
HTH. Dave
ps. please use code tags
 
Upvote 0
Thank you for your assistance, when i tried running your code, it returned with an "Invalid Next Control Variable Reference" Error, do you happen to know what changes I need to make to correct this?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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