Stripping characters using variable locations

D A

New Member
Joined
Nov 9, 2020
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
I found a function on this site that will strip the characters from a string and just leave the numbers. I would like to use variables as the object of the function. My current macro is finding different areas of my spreadsheet that I need to strip off unwanted characters. What I would like to do is use "Item" as my column location and "Rowid" as my row location and store the resulting outcome in Column "AM"+"Rowid. In this example they are specifically Item=2 and Rowid=7 (B7). When I run this it returns 27 in cell "AM7", which is understandable because of the current values of the variables. How do I format the StripChar line to allow me to use the variables? Thank you.

Range("AM" & Rowid).Select
ActiveCell = StripChar(Item & Rowid)


Function StripChar(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = .Replace(Txt, "")
End With
End Function
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi there and welcome to the forum. This should work:
VBA Code:
Range("AM" & Rowid).Select
ActiveCell = StripChar(Cells(Rowid, Item))


Function StripChar(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = .Replace(Txt, "")
End With
End Function
 
Upvote 0
Solution
Thank you so much, jmacleary! I know it seems like a simple solution but I tried so many different iterations.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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