开发者

Excel VBA: Find and Delete Multiple Values from Strings in One Column

开发者 https://www.devze.com 2023-02-02 00:47 出处:网络
Background: I need to clean up a CSV file of my bank stateme开发者_如何学Pythonnt for use in an online financial reporting tool.

Background: I need to clean up a CSV file of my bank stateme开发者_如何学Pythonnt for use in an online financial reporting tool.

The transactions are all in the following formats (columns seperated by commas):

0901XX 892XXXXX, 24/12/2010, CARD PAYMENT TO MICKEY MOUSE29.90 GBP ON 22-12-2010, -29.9

The problem I have is that the information in the thrid column is all unique because of the inclusion of the transaction amount and date - "CARD PAYMENT TO MICKEY MOUSE*29.90 GBP ON 22-12-2010*". Consequently, I'm forced to categorise all 200+ transactions individually, which is not condusive with my mental wellbeing.

The best solution I can think of is to remove all numbers from this column and find and replace all of the redundant terms ("GBP ON", ".", "-") with a null value, but I'm not sure how to go about putting that together.

Any help would be greatly appreciated.


The following may help you. I am making the following assumptions:

  1. You are getting your online banking details in a .csv format initially
  2. Your third column is always of the form TextDescription+Amount+Currency+Date
  3. In the .csv file, the third column is column C in the workbook

Follow these steps:

  1. Open .csv file
  2. Open VBE editor by pressing ALT + F11
  3. Select Insert > Module from toolbar
  4. In module paste the following code, place cursor anywhere in code, and then hit `F5'

    Sub CleanTransaction()
    Dim rng As Range, cl As Range
    Dim lastRow As Long, iChar As Long
    lastRow = Range("C1").End(xlDown).Row
    Set rng = Range("C1:C" & lastRow)
    
    
    For Each cl In rng
       For iChar = 1 To Len(cl)
           If IsNumeric((VBA.Mid$(cl, iChar, 1))) Then
               cl = VBA.Trim$(VBA.Left$(cl, iChar - 1))
           End If
       Next iChar
    Next cl    
    End Sub
    

This should do the following for each item in column C:

CARD PAYMENT TO MICKEY MOUSE29.90 GBP ON 22-12-2010 

...becomes...

CARD PAYMENT TO MICKEY MOUSE

Note that the code works by looking for the first occurence of a number in the description and stripping out everything after that. This assumes that first occurence of a number always denotes the price. If the payment recipient had a number in the name then the code would fail e.g. CARD PAYMENT TO PHONES4U

0

精彩评论

暂无评论...
验证码 换一张
取 消