Consulting

Results 1 to 13 of 13

Thread: To "credit" or not?

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    82
    Location

    To "credit" or not?



    That's me.

    I thought I might be getting to the "toddling" stage in this VB, but no - back to crawling.

    I have a column of data that has been screen dumped from a mainframe. It is mainly all numbers, with some cells being blank or having hyphens etc in them.

    The numbers have been written as 2 345 678.00DB to indicate debit entries, and to indicate credit entries it is just the number, no minuses or anything like that.

    It is not a problem to get it all into number format etc etc, but what I can't work out is how do I turn the credits (numbers without any trailing letters) into a number having a "-" at the front of the number that M/Soft will accept as a legitimate negative number? Doing text to columns etc doesn't work, it needs to be script driven.


    Pls point me in the right direction?

    Thanks & take care

    Amanda

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Amanda,

    You mean something like this?[vba]Sub TryThis1()
    If IsNumeric(Range("a2")) Then Range("a2") = -Range("a2")
    End Sub[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    hi amanda...

    my -.5 euro cents...

    best,
    wolfgang

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Amanda,

    Try the following:[vba]Sub AmandaToCreditOrNot()
    Dim RegEx As Object, RegC As Object, CLL As Range, TempArr() As Variant, i As Long
    Dim tStr As String
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Global = True
    RegEx.IgnoreCase = True
    RegEx.Pattern = "\b([\d_]+?)(DB)?\b"
    For Each CLL In Range("A1", Range("A65536").End(xlUp))
    tStr = Replace(CLL.Text, ".", "_")
    If RegEx.Test(tStr) Then
    Set RegC = RegEx.Execute(tStr)
    ReDim TempArr(RegC.Count)
    For i = 0 To RegC.Count - 1
    TempArr(i) = CDbl(IIf(Len(RegC.Item(i).SubMatches(1)) > 0, "", "-") & _
    Replace(RegC.Item(i).SubMatches(0), "_", "."))
    Next
    CLL.Resize(1, RegC.Count).Value = TempArr
    End If
    Next
    Set RegEx = Nothing
    Set RegC = Nothing
    Set CLL = Nothing
    End Sub[/vba]Right now it does the used cells in column A starting with A1, but feel free to adjust the Range("A1", Range("A65536").End(xlUp)) to whatever suits you best.

    Matt

  5. #5
    VBAX Regular
    Joined
    Nov 2005
    Posts
    82
    Location
    Hi Guys,

    Getting there but not quite. Currently there are no "-" in the column, I need to get them in where there is a number that doesn't have any, & I need them to be in the front of the number.

    Then when that is done, I can remove the "DB" portion from the rest - hence I will then have debits and credits in a format M/Soft can understand and work with. Here is a small extract from the column which will probably illustrate better.

    PHP Code:
     912 303.78DB 
    1 436 622.72DB
    4 438.24
    97 136.60
    97 136.00DB
    161 188.82DB 
    In this small extract/example it would be the 3rd & fourth numbers that I would need to have a minus in front of, (then I can delete the "DB"). Everything is in Col B, total number of rows can vary as it is updated on a constant basis & obviously it would have to go through all the cells in the column because they are in random order.

    Cheers

    Amanda
    Last edited by Amanda1; 12-13-2005 at 06:57 AM. Reason: correcting layout

  6. #6
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location

    Arrow

    Hi Amanda, If your initial data is this (in column B)
    PHP Code:
     912 303.78DB 
    1 436 622.72DB
    4 438.24
    97 136.60
    97 136.00DB
    161 188.82DB 
    Are you looking to have the following (with the space being the separator between columns B and C)
    PHP Code:
     912 303.78
    1 436 622.72
    -438.24
    97 
    -136.60
    97 136.00
    161 188.82 
    Are you only looking to fix the db/cr for the last number in the cell, or for each of them?

  7. #7
    VBAX Regular
    Joined
    Nov 2005
    Posts
    82
    Location
    Hi,

    Sorry Matt, must be something about a time lapse in the southern hemisphere - when I posted mine back, yours wasn't on there - now it suddenly is & in front of mine. Not quite sure how that works - but not a problem.

    Your example above is what I'm looking for, other than if you take the 3rd line, you have "4 -438.24" & the same with the next one of "97 -136.60" However, the "-" is in the wrong place. It must be in front of the first number in the cell.

    I.E the format 4 438.24 is actually four thousand, four hundred and thirty eight - that is just the way it comes down on the screen dump - unfortunately; so the "minus" needs to be in front of the first "4", not the second one.

    I reckon I can manage to get the "DB" portion out, so that I am not worried about, but I can't get a "-" in front of the complete number, and only on those that do not end with "DB". (By the way the "DB" has no spaces in front of it, it literally is at the end of the number).

    Does that make a bit more sense?

    Cheers & take care

    Amanda

  8. #8
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Here is a formula solution.


    ="-"&SUBSTITUTE(LEFT(A1,LEN(A1)-2)," ","",1)

    Or

    =IF((RIGHT(A3,2)="DB"),"","-"&A3)

  9. #9
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Where's the "SHAZAM!"?

    Amanda,
    So after running, you want the above numbers to look like:
    PHP Code:
    912303.78
    1436622.72
    -4438.24
    -97136.6
    97136
    161188.82 
    ? I didn't realize that the whole string was just one number. If that is correct, the following should work fine for you:[vba]Sub AmandaToCreditOrNot()
    Dim RegEx As Object, RegC As Object, CLL As Range, tStr As String
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Global = False
    RegEx.IgnoreCase = True
    RegEx.Pattern = "^([\d\. ]+?)(DB)?$"
    For Each CLL In Range("B1", Range("B65536").End(xlUp))
    tStr = Trim$(CLL.Text)
    If RegEx.Test(tStr) Then
    Set RegC = RegEx.Execute(tStr)
    CLL.Value = CDbl(IIf(Len(RegC.Item(0).SubMatches(1)) > 0, "", "-") & _
    Replace(RegC.Item(0).SubMatches(0), " ", ""))
    End If
    Next
    Set RegEx = Nothing
    Set RegC = Nothing
    Set CLL = Nothing
    End Sub[/vba]Matt

  10. #10
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    After seeing mvidas code here is a formula solution:

    =IF((RIGHT(A1,2)="DB"),SUBSTITUTE(LEFT(A1,LEN(A1)-2)," ","",1),"-"&SUBSTITUTE(A1," ",""))

    But I like mividas code better.

  11. #11
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Personally I think a formula way might be the better way to go (if vba isn't necessary....), though I did have a problem with yours when the cell had a space at the end (result of copy/paste from website). A formula way to get around that, and also to treat the number as a value, would be:
    =VALUE(IF(RIGHT(TRIM(A1),2)="db",LEFT(SUBSTITUTE(A1," ",""),LEN(SUBSTITUTE(A1," ",""))-2),"-"&SUBSTITUTE(A1," ","")))

    But I know Amanda is seemingly loving vba, so it might be better for her. Although, the routine I suggested is more of the intermediate level, as it does use regular expressions.
    Amanda- I could probably create a routine that does the same thing, but without regular expressions, just so you might be able to follow it along to see what it is doing. Although, you should learn regexp as well, as it is immensely useful and very quick.


    And I still say you should start out every post with "SHAZAM!" I'd do it, but "MVIDAS!" doesn't have quite the same ring.

  12. #12
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Hi mvidas,

    The formula that I provided works but you have a point that when copy and paste from the website it will bring junk data along with it. So here is a slight modification to my formula.

    SHAZAM!


    =IF((RIGHT(TRIM(A1),2)="DB"),SUBSTITUTE(LEFT(A1,LEN(TRIM(A1))-2)," ","",1),"-"&SUBSTITUTE(A1," ",""))

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you don't want to delve into the mysteries of RegExp yet, you could try either the sub or function options:
    [VBA]
    Sub Numbers()
    Dim cel As Range
    For Each cel In Selection
    cel.Value = Number(cel)
    Next
    End Sub

    Function Number(data) As Double
    If InStr(1, data, "DB") > 1 Then
    Number = -Replace(Replace(data, "DB", ""), " ", "")
    Else
    Number = --Replace(data, " ", "")
    End If
    End Function

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •