using LEFT function in Excel VBA

basb87

New Member
Joined
May 3, 2017
Messages
23
Hi there,

I want to use the LEFT function in VBA. But it doesn't work and I cannot find the solution on internet.

Can you help, please?

here is my code:

Code:
Sub automatisch()
Dim landcode As String
If Range("A1").Value = 2 And Left(range.("B1").Value, 2) = "Ha" Then
    Range("C1").Value = "OK!"
        
    Else: Range("C1") = "A1 is not 2 and/or B1 doesn't start with 'Ha'."
    
End If
    
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Why doesn't it work? If that is the exact copy of your code, then it's not working because of the period in between "range" and ("B1")...

Try this:

Code:
Sub automatisch()
Dim landcode As String
If Range("A1").Value = 2 And Left(Range("B1").Value, 2) = "Ha" Then
    Range("C1").Value = "OK!"
Else
    Range("C1") = "A1 is not 2 and/or B1 doesn't start with 'Ha'."
End If
End Sub

Note: as you can see in your code, "range" is not capitalized...if you ever type something without capitalizing it (and it should be capitalized), and it's in the correct format/syntax, then it will be automatically capitalized once you leave that line. If it doesn't auto-capitalize, then you know you've typed it wrong. Hopefully that makes sense.

Edit: Also, in case you wanted to get more specific with your C1 value:

Code:
Sub automatisch()
Dim landcode As String
If Range("A1").Value = 2 And Left(Range("B1").Value, 2) = "Ha" Then
    Range("C1").Value = "OK!"
Else
    If Range("A1").Value <> 2 And Left(Range("B1").Value, 2) <> "Ha" Then
        Range("C1").Value = "A1 is not 2 and B1 doesn't start with 'Ha'."
    ElseIf Range("A1").Value <> 2 Then
        Range("C1").Value = "A1 is not 2"
    ElseIf Left(Range("B1").Value, 2) <> "Ha" Then
        Range("C1").Value = "B1 doesn't start with 'Ha'."
    End If
End If
End Sub
 
Last edited:
Upvote 0
Thanks!

But now I also would like to apply this to the whole column, except the first row (that's a header). (the amount of rows differ from time to time)
So A2 needs to be empty and B2 needs to be "Ha" and then C3 wil be "OK!". The same counts for the 4th row and so on.

Is that possible?

Thanks in advance!!
 
Upvote 0
This finds the last row based on column A. If need to find the last row based on a different column then change 1 to the column number to use.
Code:
Sub automatisch()
Dim landcode As String
Dim lr As Long
lr = Cells(Rows.Count, [COLOR=#FF0000]1[/COLOR]).End(xlUp).Row
For i = 2 To lr
    If Range("A" & i).Value = 2 And VBA.left(Range("B" & i), 2) = "Ha" Then
        Range("C" & i).Value = "OK!"
        
        Else: Range("C" & i) = "A" & i & " is not 2 and/or B" & i & " doesn't start with 'Ha'."
        
    End If
Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,915
Messages
6,156,780
Members
451,381
Latest member
abdallah bennani

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