Variable for "nspace"..." "; " "; " "..

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
Hi All,
I have a report downloaded from a mainframe system that has blank cells in column A. These are not really blank cells or NullValueString. Some of them have 1 space others have 2 or 3....up to 8 spaces.

I want to set up a variable to loop through them, something like nSpance = len(cells(i,1)). This would not work because the cells that are with contents would also return a number. Thanks in advance for your help.

Here is my code without the variable:

Sub VariableSpace()

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To FinalRow

If Cells(i, 1).Text = " " Then
Cells(i, 1).Value = "One space"
ElseIf Cells(i, 1).Value = " " Then
Cells(i, 1).Value = "Two spaces"
ElseIf Cells(i, 1).Value = " " Then
Cells(i, 1).Value = "Three spaces"
End If

Next i

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
One way:

Code:
Sub VariableSpace()
    Dim cell        As Range
 
    For Each cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
        With cell
            If Len(.Text) > 0 And _
               Len(WorksheetFunction.Trim(.Text)) = 0 Then
                .Value = Len(.Text) & " spaces"
            End If
        End With
    Next cell
End Sub
 
Upvote 0
You're welcome, glad it worked for you.
 
Upvote 0
Code:
If Len(.Text) > 0 And Len(WorksheetFunction.Trim(.Text)) = 0 Then
Why did you choose to use the WorksheetFunction's TRIM function instead of VB's built-in Trim function? Wouldn't this work as well?

Code:
If Len(.Text) > 0 And Len(Trim(.Text)) = 0 Then
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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