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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,215,637
Messages
6,125,964
Members
449,276
Latest member
surendra75

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