Vb code executing and switching tabs.. weird??

alokie

New Member
Joined
Sep 6, 2011
Messages
5
Hello all, I am still new to this, but I have some code below that is supposed to update imputed on the "Updates" tab and update the data on the "Pricing Page". This is updating a specified range but when executed it is switching from the Updates tab to the Pricing page tab. Any ideas on why?

Code:
Sub ReplacePremier()
    Dim wks         As Worksheet
    Dim vWhat       As Variant
    Dim vRepl       As Variant
     
    vWhat = Worksheets("Updates").Range("A7").Value
    vRepl = Worksheets("Updates").Range("C7").Value
    Worksheets("Pricing Page").Select
    Set target = Range("C7:C50")
     
    Sheets("Pricing Page").Range("C7:C50").Replace What:=vWhat, _
    Replacement:=vRepl, _
    LookAt:=xlPart, _
    SearchOrder:=xlByRows, _
    MatchCase:=False, _
    SearchFormat:=False, _
    ReplaceFormat:=False
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Because that's what the code is telling it to do. Try this
Code:
Sub ReplacePremier()
    Dim wks         As Worksheet
    Dim vWhat       As Variant
    Dim vRepl       As Variant
   Application.ScreenUpdating = False  
    vWhat = Worksheets("Updates").Range("A7").Value
    vRepl = Worksheets("Updates").Range("C7").Value
    Worksheets("Pricing Page").Select
    Set target = Range("C7:C50")
     
    Sheets("Pricing Page").Range("C7:C50").Replace What:=vWhat, _
    Replacement:=vRepl, _
    LookAt:=xlPart, _
    SearchOrder:=xlByRows, _
    MatchCase:=False, _
    SearchFormat:=False, _
    ReplaceFormat:=False
    Worksheets("Updates").Select
    Application.ScreenUpdating = True  
End Sub
 
Upvote 0
You should be able to do this without selecting.
Code:
Option Explicit
Sub ReplacePremier()
Dim wks As Worksheet
Dim target As Range
Dim vWhat As Variant
Dim vRepl As Variant
    vWhat = Worksheets("Updates").Range("A7").Value
    vRepl = Worksheets("Updates").Range("C7").Value
    Set target = Worksheets("Pricing Page").Range("C7:C50")
    target.Replace What:=vWhat, _
                   Replacement:=vRepl, _
                   LookAt:=xlPart, _
                   SearchOrder:=xlByRows, _
                   MatchCase:=False, _
                   SearchFormat:=False, _
                   ReplaceFormat:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,991
Members
449,480
Latest member
yesitisasport

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