Vba Len with If statement

excel01noob

Board Regular
Joined
Aug 5, 2019
Messages
93
Office Version
  1. 365
  2. 2016
Hi

I am trying to setup a Vba code for a big excel file which has accounts in a column A (formatted as number, length either 5, 6 or 7 digits)

I want to copy that account number, add a character at the end (depending on the length, for 5 digits account add character A, for 6 digits account add character K, for 7 digits account add character Z)

I was trying the first part but I am already getting a type mismatch, could you guide me on this?
thank you

Sub LENGHT()

'fills the different colums added

Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

If Len(Range("A2:A" & LastRow)) = 5 Then
Range("K2:K" & LastRow) = Range("A2:A" & LastRow) & "A"

End If

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
VBA Code:
Sub excel01noob()
   With Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate(Replace("if(@="""","""",@&choose(len(@),,,,,""A"",""K"",""Z""))", "@", .Offset(, -10).Address))
   End With
End Sub
 
Upvote 0
Solution
... and, as usual, much more long-winded than Fluff's solution (and it took a lot longer!)...
VBA Code:
Sub append_account()
Dim rng As Range
Dim cl As Range
Dim intgr As Integer
Dim LastRow As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

With Me

    Set rng = .Range("A2:A" & LastRow)
    
    For Each cl In rng
        Select Case Len(cl.Value)
            Case 5
                cl.Offset(0, 10).Value = cl.Value & "A"
            Case 6
                cl.Offset(0, 10).Value = cl.Value & "K"
            Case 7
                cl.Offset(0, 10).Value = cl.Value & "Z"
                
             Case Else
                MsgBox "A/C number of digits not in the range 5 to 7!"
                
        End Select
    
    Next

End With

End Sub
 
Upvote 0
it works with Fluff's code, thank you!

sykes, your's seem to not work, it stops here "With Me".
I see added value with the msgbox warning though!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

excel01noob

I realise that Fluff's solution has done the trick, but just to answer your point (and for the benefit of anyone else referring to this post in the future)...

... I used the Me keyword, thinking you'd realise that the code needed to go into the sheet module, of the sheet in question - because if you fire the code when the required sheet isn't the active one, it'll try and run on whatever's active at the time, and will probably error-out. Unfortunately you've not stated how you're calling this procedure, so I had to guess.

If you needed a solution which could be called from anywhere, then refer to the sheet, specifically, thus (replace "Test" with the actual name of your worksheet):
VBA Code:
Sub append_account_nos()
Dim rng As Range
Dim cl As Range
Dim LastRow As Long

    With Sheets("Test")
        LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = .Range("A2:A" & LastRow)
        
            For Each cl In rng
                Select Case Len(cl.Value)
                    Case 5
                        cl.Offset(0, 10).Value = cl.Value & "A"
                    Case 6
                        cl.Offset(0, 10).Value = cl.Value & "K"
                    Case 7
                        cl.Offset(0, 10).Value = cl.Value & "Z"
        
                     Case Else
                        MsgBox "A/C number of digits not in the range 5 to 7!"
                End Select
            Next
    End With

End Sub

Hope that's of interest. :)
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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