Running macro on dif sheet, without that macro's range references applying to the CURRENT sheet

d0rian

Active Member
Joined
May 30, 2015
Messages
313
Office Version
  1. 365
Basic Q here, but having trouble finding best answer. This is simple macro I have on a sheet titled "NXT":

Code:
Sub CPHC_nxt_CB()
    Range(Range("CB_wipe_area").value).ClearContents
    Range("CB_formgrab").Copy
    Range(Range("CB_paste_area").value).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Range(Range("CB_paste_area").value).Copy
    Range(Range("CB_paste_area").value).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub

CB_wipe_area and CB_paste_area are dynamic ranges that are defined in named cells on that sheet ("nxt"), and are currently:
CB_wipe_area: V4:AC999
CB_paste_area: V4:AC534

The code is fine when run from the NXT sheet but I want to run this code from a different sheet (named "main"). I have a macro I run from the "main" sheet that includes this:

Code:
My_Macro()
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = False

[B]    With Sheets("nxt")[/B]
[B]        Call CPHC_nxt_CB[/B]
[B]    End With[/B]

    Application.ScreenUpdating = True
    Application.CutCopyMode = False
End Sub

The problem is that when I run it (from the MAIN sheet), it clears those ranges (V4:AC999 and V4:AC534) on the MAIN sheet, not on the NXT sheet. What's the correct way to tell Excel "I want you to run this Macro on that other sheet but understand that the ranges apply to THAT sheet, not this one?"
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Change the code to explicitly refer to that sheet:

Code:
Sub CPHC_nxt_CB()
With Sheets("nxt")
    .Range(Range("CB_wipe_area").value).ClearContents
    .Range("CB_formgrab").Copy
    .Range(Range("CB_paste_area").value).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    .Range(Range("CB_paste_area").value).Copy
    .Range(Range("CB_paste_area").value).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
End With
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,760
Members
449,336
Latest member
p17tootie

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