Referring to another sheet and going back to original sheet within loop

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Dear all,

I have a problem with the following macro:

Code:
Sheets("X").SelectRange("A2").Select
ActiveCell.SpecialCells(xlLastCell).Select
LastCellData = ActiveCell.Address
LastRowNew = ActiveCell.Row




For Each ws In Worksheets
    If (ws.Name <> "Overview") And (ws.Name <> "X") And (ws.Name <> "Y") And (ws.Name <> "Data") Then
        ws.Activate
        
        Columns("F:F").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbo
        Range("B:B").Select
        Selection.Copy
        Range("F1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    
        Sheets("Overview").Select
        Range("A2:A" & LastRowNew).Select
        Selection.Copy
[B]        Sheets(///the corresponding sheet within the loop///).Select[/B]
        Range("A2").Select
        ActiveSheet.Paste
        End If
    Next ws
End Sub

Basically, I want to insert a static range that is located on a different sheet in every sheet within my loop. I am struggling how to "return" to the sheet within my loop after going to the sheet Overview.
Is it possible to build a named range and insert this named range?

Code:
Sheets("X").SelectRange("A2").Select
ActiveCell.SpecialCells(xlLastCell).Select
LastCellData = ActiveCell.Address
LastRowNew = ActiveCell.Row
[B]Dim rangeCells = Range("A2:A" & LastRowNew).Values[/B]




For Each ws In Worksheets
    If (ws.Name <> "Overview") And (ws.Name <> "X") And (ws.Name <> "Y") And (ws.Name <> "Data") Then
        ws.Activate
        
        Columns("F:F").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbo
        Range("B:B").Select
        Selection.Copy
        Range("F1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
  
        Range("A2").Select
[B]        ActiveSheet.Paste /// How do I insert the named range here?[/B]
        End If
    Next ws
End Sub

Thanks in advance,
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I am struggling how to "return" to the sheet within my loop after going to the sheet Overview.

Replace this :
Code:
Sheets("Overview").Select
Range("A2:A" & LastRowNew).Select
Selection.Copy

With this :
Code:
Sheets("Overview").Range("A2:A" & LastRowNew).Copy
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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