If and elseif

Doc203

New Member
Joined
Jan 23, 2011
Messages
32
I am struggling to get this right -

This looks to see if the contents of a cell begin with a letter - if it is a letter then delete the cell and shift left. It loops until it finds a blank cell. I need it to repeat the process until the cell begins with a number.

This code works, but only checks each cell once and then moves to the next cell:

Code:
Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      If Val(Left(Cells(irow, 2), 1)) = IsText Then
        Cells(irow, 2).Delete Shift:=xlShiftToLeft
      End If
      irow = irow + 1
    Loop

I have tried this code and it works on the 1st row, but will not go to the next row and hangs:

Code:
Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      If Val(Left(Cells(irow, 2), 1)) = IsText Then
        Cells(irow, 2).Delete Shift:=xlShiftToLeft
        Else: End
      End If
      irow = irow + 1
    Loop

I am sure I need an "ISERROR" or something, but I cannot figure it out. Thanks for any help.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
It's because this is not a valid function:

= IsText

It is comparing to a variable called IsText which will always be false.

Change it to this:

If Application.WorksheetFunction.IsText(Val(Left(Cells(irow, 2), 1))) Then

Cheers

Dan
 
Upvote 0
Thanks for the reply - it returns "Argument not optional" with the following code:

Code:
Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      If Val(Left(Cells(irow, 2), 1)) = Application.WorksheetFunction.IsText Then
        Cells(irow, 2).Delete Shift:=xlShiftToLeft
      End If
      irow = irow + 1
    Loop
 
Upvote 0
Ok, re-read it and changed it to:
Code:
Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      If Application.WorksheetFunction.IsText(Val(Left(Cells(irow, 2), 1))) Then
        Cells(irow, 2).Delete Shift:=xlShiftToLeft
      End If
      irow = irow + 1
    Loop

No good, I will keep trying :)
 
Upvote 0
It will run the original way that I had it, I just would like to get it to keep deleting until it finds that the cell begins with a number and move to the next one.

I can accomplish this by re-running the macro 4 or 5 times, but I would like to put it into a loop of some type.
 
Upvote 0
It will run the original way that I had it, I just would like to get it to keep deleting until it finds that the cell begins with a number and move to the next one.

I can accomplish this by re-running the macro 4 or 5 times, but I would like to put it into a loop of some type.

Does this work?

Code:
Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      If IsText(Val(Left(Cells(irow, 2), 1))) = True Then Cells(irow, 2).Delete Shift:=xlToLeft
      If IsNumeric(Val(Left(Cells(irow, 2), 1))) = True Then exit sub
      irow = irow + 1
    Loop
 
Upvote 0
Thanks for the reply John - I got it to work with this, but I like seeing different options...

Code:
    Dim irow As Integer
    irow = 2
    Do While Len(Cells(irow, 2)) > 0
      Do While Val(Left(Cells(irow, 2), 1)) = IsText
        Cells(irow, 2).Delete Shift:=xlShiftToLeft
      Loop
      irow = irow + 1
    Loop
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,239
Members
452,898
Latest member
Capolavoro009

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