VBA: Issue

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
I have some code that makes changes to an entire sheet that I'm working with. I thought I would change it so it would make changes to ONLY the first row. This being because the type of data the code is looking for will only be in the first row.

However, now when I select the formula to operate on a different tab. It doesn't work as it should.
Code:
Application.ScreenUpdating = False


Sheets("Data Sheet").Rows("1").Select
     Selection.Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Application.ScreenUpdating = True
End Sub
The only difference between my VBA and the VBA in the actual macro is that the macro runs through about forty or fifty of these, instead of just one. Can someone please help me understand why this only works when I am ON the tab in question?

Hope you are well,

Steve
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you are not already on "Data Sheet", you cannot select both the sheet and range at the same time.
One way to do it is to like this:
Code:
Sheets("Data Sheet").Activate
     Rows("1").Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

or, you do not need to even activate the sheet to perform that replace, just use:
Code:
    Sheets("Data Sheet").Rows("1").Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Last edited:
Upvote 0
If you are not already on "Data Sheet", you cannot select both the sheet and range at the same time.
One way to do it is to like this:
Code:
Sheets("Data Sheet").Activate
     Rows("1").Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

or, you do not need to even activate the sheet to perform that replace, just use:
Code:
    Sheets("Data Sheet").Rows("1").Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

This isn't working, and I'm not clear on why. It appears to me that this should work.
 
Upvote 0
It works for me when I tested it out.
Are you sure that these cells you are trying to replace have EXACTLY "Product Name" in them, with no other text or extra spaces?
 
Upvote 0
.
Let me ask ... when you change sheets are you changing the sheet name here :

Code:
[COLOR=#333333]Sheets("[/COLOR][COLOR=#ff0000][B]Data Sheet[/B][/COLOR][COLOR=#333333]").Rows("1").Select[/COLOR]
 
Upvote 0
It works for me when I tested it out.
Are you sure that these cells you are trying to replace have EXACTLY "Product Name" in them, with no other text or extra spaces?

When I open and run the macro when I have the sheet open, "Data Sheet", it does what it is supposed to do. When I am on a different sheet it doesn't work.
 
Upvote 0
Though it shouldn't make a difference, try breaking it up into two commands like this and see if that makes a difference:
Code:
    Sheets("Data Sheet").Activate
    ActiveSheet.Rows("1").Replace What:="Product Name", Replacement:="Productname", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Upvote 0
The only difference between my VBA and the VBA in the actual macro is that the macro runs through about forty or fifty of these, instead of just one.
Show us the loop that you are using to do the above.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,236
Messages
6,123,799
Members
449,127
Latest member
Cyko

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