VBA to remove characters and commas

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Hi, i need a macro to do the following,

1. Remove all commas from activeworksheet ( i notice i cant see the commas in excel, but when i open notepad i have commas in empty rows)

2. Remove all characters such as = + # ( ) $ from Column 5

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
try
Code:
Sub test()
Dim e
With ActiveSheet.UsedRange
    For Each e In [{",","+","#","(",")","$"}]
        .Replace e, "", xlPart
    Next
End With
End Sub
 
Upvote 0
Seiya when i save the file as CSV and open via notepad i can see lots commas

,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
Portfolio ID,Valuation Date,Asset Category,Sub Category,Security Code / Cash ID,Description,Units,Local Price,Market Value Local Currency,Market Value AUD,Total PV AUD,Accrued Interest AUD,Currency,Exchange Rate,Expiry Date,Trade Date,Coupon,Maturity Date,Extra Reference
SELCHH ,30/04/2009,,,AU000000FAT2,Fat Prophets Australia Fund Ltd,880145.00 ,0.56,492881.20 ,492881.20 ,1498856.08 ,0.00 ,AUD,1,,,,,
SELCHH ,30/04/2009,,,AU000000WIL7,Wilson Investment Fund Ltd,2074175.00 ,0.485,1005974.88 ,1005974.88 ,1498856.08 ,0.00 ,AUD,1,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
 
Upvote 0
That tells that you have many rows/columns that are not empty...

Run this before you save the file as CSV.
Code:
Sub test()
With ActiveSheet.UsedRange
    .Value = Evaluate("if(" & .address & "<>"""",trim(" & .address & "),"""")")
End With
End Sub
 
Upvote 0
Hi Seiya,

Watches your post in this forum, u r the one who gives maximum solution or all solution in vba. Am also keen to learn vba, any suggestion from your side on how to go about will be really helpful.
 
Upvote 0
Hello,

Code is for remove the Non-Numeric and Non-Alpha values from cell:

Code:
Function RemoveChar()

Dim rngColumn As Range   

Set rngColumn = Range("E:E")

     For Each cell In rngColumn
            
            If Trim(cell.Value) = "" Then
                GoTo ForNext
            End If
            
            b = ""
            a = ""
            For I = 1 To Len(cell.Value)
                a = Mid(cell.Value, I, 1)
                If IsNumeric(a) = True Or IsAlpha(a) = True Then
                    b = b & a
                End If
            Next
            
            cell.Value = b
            
ForNext:

    Next
End Function


Private Function IsAlpha(ByVal sChr As String) As Boolean
    IsAlpha = sChr Like "[A-Za-z]"
End Function

Next code i'll provide you after some time.
OK?
 
Upvote 0
Nope still getting commas

,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
PortfolioID,ValuationDate,AssetCategory,SubCategory,SecurityCode/CashID,Description,Units,LocalPrice,MarketValueLocalCurrency,MarketValueAUD,TotalPVAUD,AccruedInterestAUD,Currency,ExchangeRate,ExpiryDate,TradeDate,Coupon,MaturityDate,ExtraReference
SELCHH ,30/04/2009,,,AU000000FAT2,FatProphetsAustraliaFundLtd,880145.00 ,0.56,492881.20 ,492881.20 ,1498856.08 ,0.00 ,AUD,1,,,,,
SELCHH ,30/04/2009,,,AU000000WIL7,WilsonInvestmentFundLtd,2074175.00 ,0.485,1005974.88 ,1005974.88 ,1498856.08 ,0.00 ,AUD,1,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
 
Upvote 0
Then try
Code:
Sub test()
With Columns(1)
    For Each r In .SpecialCells(2)
        If Len(r.Value) < 2 Then r.ClearContents
    Next
    .SpecialCells(4).EntireRow.Delete
End With
End Sub
 
Upvote 0
Then try
Code:
Sub test()
With Columns(1)
    For Each r In .SpecialCells(2)
        If Len(r.Value) < 2 Then r.ClearContents
    Next
    .SpecialCells(4).EntireRow.Delete
End With
End Sub

But this will delete all the other text (if any in the OP's file) for which the length is 1 character. I guess you would have to warn the OP about the outcome of your code.

Edit: what does (4) represent ? Is it xlTextValues ?
 
Last edited:
Upvote 0
Yay that worked, now if cells are not found i have put On Errror Resume Next, is this correct?

Code:
With Columns(1)
    For Each r In .SpecialCells(2)
        If Len(r.Value) < 2 Then r.ClearContents
    Next
    On Error Resume Next
    .SpecialCells(4).EntireRow.Delete
End With
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,221
Members
453,152
Latest member
ChrisMd

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