Need help with VBA and Find and Replace Function - Can't get it to run

rip_vw32

New Member
Joined
Jul 26, 2016
Messages
4
Hello, I am hoping someone can help me with this....


I am trying to do create a macro to substitute part of a formula in column 'S', cells '1-254' on the worksheet “Original_Formula_Sheet" (so the range is 'S1:S254'). The formula essentially has a $503 absolute variable in it, and I need to replace it with a $753 in row 1. I have 254 rows to go through where the initial absolute variable needed to be replaced, but the value to be replaced increments with each subsequent line.


So what I am trying to do is list out all the variables to be changed and list out what they need to be changed to (eg: $503 to $753, $504 to $754,, etc...) on the worksheet "Values", columns 'A' and 'B', rows '1-254' (so the range is 'A1:B254')


When I have a macro built and try to run it I get a "Runtime Error '9': Subscript out of range"


Here is how I am trying to build the macro:


Code:
Sub Replace()


	Dim myList, myRange


	Set myList = Worksheets(“Values”).Range("A1:B254")
    
	Set myRange = Worksheets(“Original_Formula_Sheet”).Range("S4:S254")


	For Each cel In myList.Columns(1).Cells


	myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, LookAt:=xlWhole


	Next cel


End Sub


The error, when debugging, highlights the “Set myList = Worksheets(“Values”).Range(“A1:B254”)”.


Is there something that I am missing here... I am running Excel 2013
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi,

I have not tested your code with any data, but I did copy it into a worksheet.

The code did throw an error on the line that you stated. But I think your error is something minor in your use of the quotes that surround your sheet names.

Try this:

Code:
Sub Replace()

    Dim myList, myRange

    Set myList = Worksheets("Values").Range("A1:B254")
    Set myRange = Worksheets("Original_Formula_Sheet").Range("S4:S254")

    For Each cel In myList.Columns(1).Cells
        myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, LookAt:=xlWhole
    Next cel
    
End Sub

HTH

igold
 
Last edited:
Upvote 0
That is awesome...... it actually ran once I replaced the quotes.... thank you so much for taking a look at this. I truly appreciate it!
 
Upvote 0
Glad you got it going. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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