• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA code to screen digits and perform action

Dear all,

I'm looking for a VBA code which can perform the following:

Scroll through some digits and

- if the first number is <> "0" it should perform left(A1;6)
- if the first number is "0" it should perform left(A1;5)
- if the first number is "0" it should perform left(A1;4)

Anybody some clue on how I can achieve this?

Dear regards,

Marc
 
The last 2 look like the same condition. I would do this with a function.

This will do it with a function:
Function something(c1 As Range, c2 As Range)
Dim sHolder As String
Dim sResult As String

sHolder = Left(c1.Value, 1)

Select Case sHolder
Case sHolder = "0"
sResult = Left(c2, 6)
Case Else
sResult = Left(c2, 5)
End Select

something = sHolder


End Function
 
Thanks Dan, made a mistake though. What I meant:

- if the first number is <> "0" it should perform left(A1;6)
- if the first number is "0" it should perform left(A1;5)
- if the first number is "00" it should perform left(A1;4)

So after

Case Else
sResult = Left(c2, 5)

I should include some kind of if this function... Any idea how this would look like? Thanks big time!
 
Hi Marc,

Try this :

Code:
Function something(c1 As Range)
        Dim sHolder As String
        Dim sResult As String

        If Left(c1.Value, 2) = "00" Then
            sResult = Left(c1, 4)
        Else
            sHolder = Left(c1.Value, 1)

            Select Case sHolder
                  Case sHolder = "0"
                        sResult = Left(c1, 5)
                  Case Else
                        sResult = Left(c1, 6)
            End Select
        End If
        something = sResult
End Function
Narayan
 
Back
Top