VBA: if statement w/ Left function equal to a string

ZachSean

New Member
Joined
Feb 26, 2016
Messages
2
Hey there, been struggling with finding my error for a while so I figured I'd give this a try.
Here is the code that I'm trying to run.

Code:
Sub test()

    If (Left("E2", 1) = ":") Then
        Range("E2") = Right("E2", (Len("E2") - Range("E2").Find(":", "E2", 1)))
        MsgBox ("if statement worked")
    Else
        MsgBox ("if statement did not work")
    End If
            
End Sub

And this is what is stored in cell E2: ':DC1

This is a small chunk a large macro where the purpose of this section is to remove the colon from the beginning of a string. However the code does not ever enter the first section where it would remove the colon as it always displays the message box "if statement did not work".
It's a problem with the first line of the code since I've never had it display the "if statement worked" message box.

Any help is appreciated.

For anyone who attempts to help, here are some things I've already tried:
- using E2 instead of "E2"
- using Left$() instead of Left()
- removing the brackets around the if statement
- using characters other than a colon
- using cells other than E2
- removing the apostrophe from ':DC1
- solely placing a colon in cell E2

I'm sure it's something simple that I'm missing/forgetting but for the life of me I can't figure it out.
Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board!

I haven't evaluated your logic, but your formulas are failing because all your range references need the word "Range" in front of them, i.e.
Code:
If (Left([COLOR=#ff0000]Range[/COLOR]("E2"), 1) = ":") Then
    Range("E2") = Right([COLOR=#ff0000]Range[/COLOR]("E2"), (Len([COLOR=#FF0000]Range[/COLOR]("E2")) - Range("E2").Find(":", [COLOR=#FF0000]Range([/COLOR]"E2"), 1)))
 
Upvote 0
Hahahaahahh great, I figured it was something minor.
For anyone who decides to look at my code, there was a second error shown below.
Rich (BB code):
Sub test()

    If (Left(Range("E2"), 1) = ":") Then
        Range("E2") = Right(Range("E2"), (Len(Range("E2")) - Range("E2").Find(":", Range("E2"), 1)))
        MsgBox ("if statement worked")
    Else
        MsgBox ("if statement didn't work")
    End If
            
End Sub

I fixed this by replacing the line with:
Range("E2") = Replace(Range("E2")
Thanks again for the help!
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

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