Identify first two digits of a number using VBA

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need to identify the first two digits of an 8 digit number and if it is "87", then populate another column with a value "GF".
I need to use VBA and not VLookup, since I have other codes in the same module.

My code is

Rich (BB code):
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Left(ws.Range("AI" & i), 2).Value = "87" Then
            ws.Range("BE" & i).Value = "GF"
        End If
    Next i

obviously failed at the LEFT functionality!
 
Last edited by a moderator:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
obviously failed at the LEFT functionality
Vba should not fail on that instruction, because an implicit conversion is done by Left when a number is submitted as first parametres...
Do you receive a RunTimeError, or what is the error? Which is the typical content of column WI?
 
Upvote 0
Vba should not fail on that instruction, because an implicit conversion is done by Left when a number is submitted as first parametres...
Do you receive a RunTimeError, or what is the error? Which is the typical content of column WI?
I get the below error
1668020307497.png


1668020532731.png
 
Upvote 0
You have to use
VBA Code:
If Left(ws.Range("AI" & I).Value, 2) = "87" Then
(not Left(ws.Range("AI" & i), 2).Value)
 
Upvote 0
Solution
Thank you for the feedback
(or: Sometime glasses are needed!)
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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