VBA If LEN cell < 10 then, if LEN cell <20 then...

db74

New Member
Joined
Jul 5, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello. Could anyone point me in the right direction to write a VBA statement based upon the number of characters in a cell. I'm trying to concatenate 5 columns but the second length of the 2nd column could vary, so I was trying to add a certain number of tabs to the column (depending on the size of the cell/text) to create equal spacing. I have already made this work as an Excel formula, but need it in VBA. I've started with
VBA Code:
c.Offset(,-1) &IF(LEN(c.Value)<10 THEN c.Value&Chr(9)&Chr(9)
but it's failing on the first IF. Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Could you share some sample data and the desired output?
 
Upvote 0
You could structure your IF statement something like this in VBA:
VBA Code:
If Len(c.Value) < 10 Then
    c.Offset(,-1).Value = c.Value & Chr(9) & Chr(9)
Else
    If If Len(c.Value) < 20 Then
        c.Offset(,-1).Value = c.Value & Chr(9)
    Else
        c.Offset(,-1).Value = c.Value
    End If
End If
 
Upvote 0
Thanks @Joe4 I'll give that a go. @PeteWright, I've added some test data below and I'm trying to concatenate all the columns but keep the spacing to then insert into a treeview. Not sure if this is possible yet, although I have managed to sort of make it work. If anyone has any better ideas on how to do this then happy to try them. Thanks

DateTitleAuthorStreamVersionsDocument No
16/09/2011Document Onejpb
2​
600​
16/09/2011Document Twojpb
2​
601​
21/09/2011Document One Hundred and Thirty Eightjpb
2​
597​
03/11/2011Document TwentyjpbADM
1​
661​
03/11/2011Document Fifty Fourjpb
1​
662​
 
Upvote 0
I've started to try and implement the If statement within the script to populate the treeview parent nodes, but encountering a scripting error. Any ideas if or how I can add the If Statement to the statement below? Thanks

VBA Code:
Set nParent = TDocTV.Nodes.Add(, "Parent" + c.Value, c.Value, c.Offset(, -1) + If(LEN(c.Value) <10 THEN c.Value &Chr(9) &Chr(9) &Chr(9) &Chr(9) Else IF(LEN(c.Value) <20 THEN c.Value &Chr(9) &Chr(9)
Else ''
End If
End If
 
Upvote 0
That is NOT how you structure an IF...THEN block in VBA. You are using it like an Excel worksheet function (single line), which does NOT work.

If you use the VBA IF...THEN block, your line MUST begin with IF, like I show. It would be structured something like (fill in the details for each one):
VBA Code:
If Len(c.Value) < 10 Then
    Set nParent = ...
Else
    If If Len(c.Value) < 20 Then
            Set nParent = ...
    Else
            Set nParent = ...
    End If
End If

See: If Then Else Statement in Excel VBA (explained with examples)
 
Upvote 0
Solution
That is NOT how you structure an IF...THEN block in VBA. You are using it like an Excel worksheet function (single line), which does NOT work.

If you use the VBA IF...THEN block, your line MUST begin with IF, like I show. It would be structured something like (fill in the details for each one):
VBA Code:
If Len(c.Value) < 10 Then
    Set nParent = ...
Else
    If If Len(c.Value) < 20 Then
            Set nParent = ...
    Else
            Set nParent = ...
    End If
End If

See: If Then Else Statement in Excel VBA (explained with examples)
Thanks @Joe4. I tried it a few different ways with blocking the statement, but didn't think to start the If from the start which is where I was going wrong. I'll try it again but appreciate the help as been coding for the last couple of weeks now and my brain is now hurting.
 
Upvote 0
OK, if you run into any more issues, just post back your latest code attempt, and we can see if we can help you fix it.

Note that I have never worked with "TreeView" before (so I cannot help with any specific "TreeView" issues), but I think the issue is mroe with your code structure than with TreeView.
 
Upvote 0
Thanks again @Joe4, it is now up and running. The only issue I have now is getting tabs to work, as neither vbtab or chr(9) seem to do anything, although space(#) does work in a treeview, so may need to use this but it might get more messy.
 
Upvote 0
Perhaps this might help or give you some ideas on how you can "pad" spaces on the end so that the total length is some specified number.

Let's say that you want "c.Offset(, -1)" to show value fom "c", padded with spaces at the end so that its total length is exactly 30 spaces.
Then you could do something like this:
VBA Code:
    c.Offset(, -1).Value = Left(c.Value & Application.WorksheetFunction.Rept(" ", 30), 30)
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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