Excel VBA Replace Values Dynamically

Matt29108

New Member
Joined
Aug 4, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

In the code below, I am selecting a specific range. Within that range I am replacing all the cells that contain $O$1 with a formula which contains the RowNumber. Note that everytime I run the code, the RowNumber value changes. So I cannot simply write a number as a replacement. When I run the code replace the $O$1 with the Offset formula, Excel do not see the RowNumber as an integer but as a word like the picture below. How can I make Excel recognize RowNumber as a number during replacement?

Thanks in advance for the help.

1646229863381.png


Sub Example()

Dim wb As Workbook
Dim ws As Worksheet
Dim RowNumber As Integer

Set wb = ActiveWorkbook
Set ws= Sheets("ws")

RowNumber = Application.WorksheetFunction.CountA(ws.Range("E:E"))

ws.Range(ws.Cells(RowNumber + 14, 1), DK.Cells(RowNumber + 47, 4)).EntireRow.Replace What:="=$O$1", Replacement:="Offset(RowNumber,0,1)"

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
For this section:
ws.Range(ws.Cells(RowNumber + 14, 1), DK.Cells(RowNumber + 47, 4)).EntireRow.Replace What:="=$O$1", Replacement:="Offset(RowNumber,0,1)"
simply change
VBA Code:
Replacement:="Offset(RowNumber,0,1)"
to this
VBA Code:
Replacement:="Offset(" & RowNumber & ",0,1)"
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,793
Members
448,994
Latest member
rohitsomani

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