VBA to Remove hyphens and some other text in only one column of a Table

captainxcel

New Member
Joined
Jul 28, 2017
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have a table (list object) called balances_table which starts in cell A1 and is the only data on on a particular worksheet. I paste data into the table from a downloaded csv file as below (a few sample rows included). I'd like to write a macro to remove the strings "Account Number: " and "-" from the Account column so that the account column has only numbers like 11112222 and 33334444. The code I wrote (see below table) is not working exactly as planned. It does clean up the Account column exactly as I wanted, but it is also removing negative signs from the Balance column (which is in column C), which I do not want to happen. What am I doing wrong?
Thanks!

AccountNameBalance
Account Number: 1111-2222Smith Foods
-125.73​
Account Number: 3333-4444Johnson Supermarkets
1250.99​

Sub clean_up_acct_col()
Columns("A:A").Replace What:="-", Replacement:="", _
LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False
Columns("A:A").Replace What:="Account Number: ", Replacement:="", _
LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try the following on a copy of your sheet:
VBA Code:
Sub clean_up_acct_col()
    With Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        .Value2 = Evaluate("Substitute(Substitute(" & .Address(, , , 1) & ",""Account Number: "",""""),""-"","""")")
    End With
End Sub

Before:
Book1
ABC
1AccountNameBalance
2Account Number: 1111-2222Smith Foods-125.73
3Account Number: 3333-4444Johnson Supermarkets1250.99
Sheet1


After:
Book1
ABC
1AccountNameBalance
211112222Smith Foods-125.73
333334444Johnson Supermarkets1250.99
Sheet1
 
Upvote 0
Solution
Thank you kevin9999! Works great. I'll dig into the code this weekend to improve my knowledge. I kinda figured a with / end with would be needed.
 
Upvote 0
Thank you kevin9999! Works great. I'll dig into the code this weekend to improve my knowledge. I kinda figured a with / end with would be needed.
Happy to help, and thanks for the feedback 👍 😀
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,267
Members
449,093
Latest member
Vincent Khandagale

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