VBA for numbers within Ranges.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings I have had tremendous help in this area, with VLookUPs. Now I have 5 digits with the first four being numbers. I could make a massive Vlookup listing the numbers in a single column that would require thousand of entries. Please refer to the image. I can I adjust the VBA provided by "Snakehips". The X can be any letter, it cannot be another number.

VBA Code:
Private Sub USER_BRANCH()
   Dim i As Long, n As Variant
   Dim ShtAD, Sht72H, LUrng As Range
   Dim slash, GotIt As String
  
   Set ShtAD = Sheets("AirDrop")
   Set Sht72H = Sheets("72 Hr")
   Set LUrng = ShtAD.Range("A15:B23")
  
   Lastr = Sht72H.Range("A" & Rows.Count).End(xlUp).Row
  
    For i = 1 To Lastr
        slash = ""
        GotIt = ""
         n = Mid(Sht72H.Cells(i, 1).Value, 5, 5)
      
 If IsNumeric(n) Then n = CLng(n)
       On Error Resume Next
            If Not Sht72H.Cells(i, 6).Value = "" Then slash = " / "
            GotIt = Application.WorksheetFunction.VLookup(n, LUrng, 2, 0)
            If Not GotIt = "" Then
                Sht72H.Cells(i, 6).Value = Sht72H.Cells(i, 6).Value & slash & GotIt
            End If
    Next i
    On Error GoTo 0
End Sub


Capture.PNG
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
@Livin404 again you weren't to specific to what exactly you were wanting other than to check for 4 numbers and a letter.

With that in mind I came up with following for the 'Air Drop' sheet:

Livin04LookupTableVersion5Characters.xlsm
AB
1LookUpReturn
210001000X-2999X
32999
430003000X-4999X
54999
650005000X-5999X
75999
860006000X-6999X
96999
1070007000X-7499X
117499
1275007500X-7999X
137999
1480008000X-8499X
158499
1685008500X-8999X
178999
1890009000X-9499X
199499
2095009500X-9999X
219999
Air Drop



VBA Code:
Private Sub USER_BRANCHv2()
'
    Dim First4NumbersOf72Hr As Double
    Dim LastRow72Hr         As Long
    Dim LastRowAirDrop      As Long
    Dim RowCounter72Hr      As Long
    Dim RowCounterAirDrop   As Long
    Dim StringToSearchFor   As String
'
    LastRow72Hr = Sheets("72 Hr").Range("A" & Rows.Count).End(xlUp).Row
    LastRowAirDrop = Sheets("Air Drop").Range("A" & Rows.Count).End(xlUp).Row
 
    For RowCounter72Hr = 1 To LastRow72Hr                               ' Loop through each cell in column A to check against the table of values
        If Len(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value) = 5 Then                                  ' Check to see if the string = 5 characters
            If IsNumeric(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4)) = True Then            '   Check to see if first 4 characters are numbers
                If IsThisALetter(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 5, 1)) = True Then  '     Check to see if 5th chacter is a letter
                    First4NumbersOf72Hr = Val(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4))   '       Convert the first 4 characters from string to numeric
'
                    For RowCounterAirDrop = 2 To LastRowAirDrop - 1 Step 2
                        If First4NumbersOf72Hr >= Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value And First4NumbersOf72Hr <= Sheets("Air Drop").Range("A" & RowCounterAirDrop + 1).Value Then
                            If Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = "" Then
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value
                                Exit For
                            Else
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = Sheets("72 Hr").Range("F" & RowCounter72Hr).Value & "/" & Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value
                                Exit For
                            End If
                        End If
                    Next
                End If
            End If
        End If
    Next
End Sub


Function IsThisALetter(TestCharacter As String) As Boolean
'
'   This function takes a single character as input and checks to see if it is a letter. Results in True or False
'       You can use it in a cell formula ex. =IsThisALetter(A1), where cell A1 is a single character.
'
    IsThisALetter = Asc(UCase(TestCharacter)) > 64 And Asc(UCase(TestCharacter)) < 91    'Is the character a letter?
End Function

Which resulted in the following for the '72 Hr' sheet that has the values to check column (A) and the Results column (F):

Livin04LookupTableVersion5Characters.xlsm
ABCDEF
12999xSomething/1000X-2999X
21000X1000X-2999X
30999xSomething
41234X567
53000x3000X-4999X
64999X3000X-4999X
75000X5000X-5999X
85999x5000X-5999X
96000x6000X-6999X
106999X6000X-6999X
117000x7000X-7499X
127499x7000X-7499X
137500X7500X-7999X
147999x7500X-7999X
1580000
168000x8000X-8499X
178000#
188499X8000X-8499X
198500x8500X-8999X
208500-
218999X8500X-8999X
229000x9000X-9499X
239499X9000X-9499X
249500x9500X-9999X
259999x9500X-9999X
72 Hr


Let us know how it goes.
 
Upvote 0
Here is an alternative version if you only want to have the table you posted in the 'Air Drop' sheet column A:

Livin04LookupTableVersion5CharactersAlternateV1.xlsm
A
1LookUp
21000X-2999X
33000X-4999X
45000X-5999X
56000X-6999X
67000X-7499X
77500X-7999X
88000X-8499X
98500X-8999X
109000X-9499X
119500X-9999X
12
Air Drop



And the code to utilize that:

VBA Code:
Private Sub USER_BRANCHv2AlternateV1()
'
    Dim First4NumbersOf72Hr As Double
    Dim LastRow72Hr         As Long
    Dim LastRowAirDrop      As Long
    Dim RowCounter72Hr      As Long
    Dim RowCounterAirDrop   As Long
'
    LastRow72Hr = Sheets("72 Hr").Range("A" & Rows.Count).End(xlUp).Row
    LastRowAirDrop = Sheets("Air Drop").Range("A" & Rows.Count).End(xlUp).Row
 
    For RowCounter72Hr = 1 To LastRow72Hr                                       ' Loop through each cell in column A to check against the table of values
        If Len(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value) = 5 Then                                  ' Check to see if the string = 5 characters
            If IsNumeric(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4)) = True Then            '   Check to see if first 4 characters are numbers
                If IsThisALetter(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 5, 1)) = True Then  '     Check to see if 5th chacter is a letter
                    First4NumbersOf72Hr = Val(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4))   '       Convert the first 4 characters from string to numeric
'
                    For RowCounterAirDrop = 2 To LastRowAirDrop                 ' Loop through each cell in column A to check for a match of high and Low values
                        If First4NumbersOf72Hr >= Val(Left(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 4)) And _
                          First4NumbersOf72Hr <= Val(Mid(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 7, 4)) Then   ' If match found then ...
                            If Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = "" Then                                      '   Is the cell in column F "" ?
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value ' Yes, set the cell value
                                Exit For                                                                                                    ' Exit this For Loop
                            Else
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = _
                                  Sheets("72 Hr").Range("F" & RowCounter72Hr).Value & "/" & Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value ' Append a value
                                Exit For                                                                                                    ' Exit this For Loop
                            End If
                        End If
                    Next
                End If
            End If
        End If
    Next
End Sub


Function IsThisALetter(TestCharacter As String) As Boolean
'
'   This function takes a single character as input and checks to see if it is a letter. Results in True or False
'       You can use it in a cell formula ex. =IsThisALetter(A1), where cell A1 is a single character.
'
    IsThisALetter = Asc(UCase(TestCharacter)) > 64 And Asc(UCase(TestCharacter)) < 91    'Is the character a letter?
End Function

The results are the same as in my previous post here for the '72 Hr' sheet that has the values to check column (A) and the Results column (F).
 
Upvote 0
Thank you for getting back, I’ll test it next time I’m on my laptop. Just so you know, though the image doesn’t show there are text values in the description. For exam a number like 3677A would have gotten whatever I had listed in the second row of Column B. The X shown would be a letter and the number would fall within the given range in Column A.
 
Upvote 0
Thank you for getting back, I’ll test it next time I’m on my laptop. Just so you know, though the image doesn’t show there are text values in the description. For exam a number like 3677A would have gotten whatever I had listed in the second row of Column B. The X shown would be a letter and the number would fall within the given range in Column A.
Ok, here is the code to handle the 'Air Drop' sheet with the range in Column A and the Description in column B:

Livin04LookupTableVersion5CharactersAlternateV2.xlsm
AB
1LookUpDescription
21000X-2999XText2
33000X-4999XText3
45000X-5999XText4
56000X-6999XText5
67000X-7499XText6
77500X-7999XText7
88000X-8499XText8
98500X-8999XText9
109000X-9499XText10
119500X-9999XText11
12
Air Drop


VBA Code:
Private Sub USER_BRANCHv2AlternateV2()
'
    Dim First4NumbersOf72Hr As Double
    Dim LastRow72Hr         As Long
    Dim LastRowAirDrop      As Long
    Dim RowCounter72Hr      As Long
    Dim RowCounterAirDrop   As Long
'
    LastRow72Hr = Sheets("72 Hr").Range("A" & Rows.Count).End(xlUp).Row
    LastRowAirDrop = Sheets("Air Drop").Range("A" & Rows.Count).End(xlUp).Row
 
    For RowCounter72Hr = 1 To LastRow72Hr                                       ' Loop through each cell in column A to check against the table of values
        If Len(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value) = 5 Then                                  ' Check to see if the string = 5 characters
            If IsNumeric(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4)) = True Then            '   Check to see if first 4 characters are numbers
                If IsThisALetter(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 5, 1)) = True Then  '     Check to see if 5th chacter is a letter
                    First4NumbersOf72Hr = Val(Left(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 4))   '       Convert the first 4 characters from string to numeric
'
                    For RowCounterAirDrop = 2 To LastRowAirDrop                 ' Loop through each cell in column A to check for a match of high and Low values
                        If First4NumbersOf72Hr >= Val(Left(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 4)) And _
                          First4NumbersOf72Hr <= Val(Mid(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 7, 4)) Then   ' If match found then ...
                            If Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = "" Then                                      '   Is the cell in column F "" ?
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value ' Yes, set the cell value
                                Exit For                                                                                                    ' Exit this For Loop
                            Else
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = _
                                  Sheets("72 Hr").Range("F" & RowCounter72Hr).Value & "/" & Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value ' Append a value
                                Exit For                                                                                                    ' Exit this For Loop
                            End If
                        End If
                    Next
                End If
            End If
        End If
    Next
End Sub


Function IsThisALetter(TestCharacter As String) As Boolean
'
'   This function takes a single character as input and checks to see if it is a letter. Results in True or False
'       You can use it in a cell formula ex. =IsThisALetter(A1), where cell A1 is a single character.
'
    IsThisALetter = Asc(UCase(TestCharacter)) > 64 And Asc(UCase(TestCharacter)) < 91    'Is the character a letter?
End Function

And the results on the '72 Hr' sheet:

Livin04LookupTableVersion5CharactersAlternateV2.xlsm
ABCDEF
12999xSomething/Text2
21000XText2
30999xSomething
41234X567
53000xText3
64999XText3
75000XText4
85999xText4
96000xText5
106999XText5
117000xText6
127499xText6
137500XText7
147999xText7
1580000
168000xText8
178000#
188499XText8
198500xText9
208500-
218999XText9
229000xText10
239499XText10
249500xText11
259999xText11
26
72 Hr
 
Upvote 0
Thank you, I will test it properly soon. I had a chance to read the comments. I'm afraid my target characters from a 12 digit text string, are characters 5-9. I definitely understand your frustration. Yet again between my thought process and posting things get a little muddled.
 
Upvote 0
So you are now saying that you have a 12 character text string that you want to test characters 5,6,7,8 for numbers & character 9 for a letter? The rest of the characters to the left of 5 thru 9 and to the right don't matter?
 
Upvote 0
So you are now saying that you have a 12 character text string that you want to test characters 5,6,7,8 for numbers & character 9 for a letter? The rest of the characters to the left of 5 thru 9 and to the right don't matter?
Assuming my previous post is correct, that brings us to the following:

Livin04LookupTableVersion5CharactersAlternateV3.xlsm
AB
1LookUpDescription
21000X-2999XText2
33000X-4999XText3
45000X-5999XText4
56000X-6999XText5
67000X-7499XText6
77500X-7999XText7
88000X-8499XText8
98500X-8999XText9
109000X-9499XText10
119500X-9999XText11
12
Air Drop


Code you could use:

VBA Code:
Private Sub USER_BRANCHv2AlternateV3()
'
    Dim Characters5Thru8Of72Hr  As Double
    Dim LastRow72Hr             As Long
    Dim LastRowAirDrop          As Long
    Dim RowCounter72Hr          As Long
    Dim RowCounterAirDrop       As Long
'
    LastRow72Hr = Sheets("72 Hr").Range("A" & Rows.Count).End(xlUp).Row
    LastRowAirDrop = Sheets("Air Drop").Range("A" & Rows.Count).End(xlUp).Row
 
    For RowCounter72Hr = 1 To LastRow72Hr                                       ' Loop through each cell in column A to check against the table of values
        If Len(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value) = 12 Then                                     ' Check to see if the string = 12 characters
            If IsNumeric(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 5, 4)) = True Then              '   Check to see if 5th-8th characters are numbers
                If IsThisALetter(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 9, 1)) = True Then      '     Check to see if 9th chacter is a letter
                    Characters5Thru8Of72Hr = Val(Mid(Sheets("72 Hr").Range("A" & RowCounter72Hr).Value, 5, 4))  '       Convert the 5th-8th characters from string to numeric
'
                    For RowCounterAirDrop = 2 To LastRowAirDrop                 ' Loop through each cell in column A to check for a match of high and Low values
                        If Characters5Thru8Of72Hr >= Val(Left(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 4)) And _
                          Characters5Thru8Of72Hr <= Val(Mid(Sheets("Air Drop").Range("A" & RowCounterAirDrop).Value, 7, 4)) Then    ' If match found then ...
                            If Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = "" Then                                          '   Is the cell in column F "" ?
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value ' Yes, set the cell value
                                Exit For                                                                                                    ' Exit this For Loop
                            Else
                                Sheets("72 Hr").Range("F" & RowCounter72Hr).Value = _
                                  Sheets("72 Hr").Range("F" & RowCounter72Hr).Value & "/" & Sheets("Air Drop").Range("B" & RowCounterAirDrop).Value ' Append a value
                                Exit For                                                                                                    ' Exit this For Loop
                            End If
                        End If
                    Next
                End If
            End If
        End If
    Next
End Sub


Function IsThisALetter(TestCharacter As String) As Boolean
'
'   This function takes a single character as input and checks to see if it is a letter. Results in True or False
'       You can use it in a cell formula ex. =IsThisALetter(A1), where cell A1 is a single character.
'
    IsThisALetter = Asc(UCase(TestCharacter)) > 64 And Asc(UCase(TestCharacter)) < 91    'Is the character a letter?
End Function

And the Test column A Data/Column F results on the '72 Hr' sheet:

Livin04LookupTableVersion5CharactersAlternateV3.xlsm
ABCDEF
1ABCD2999x123Something/Text2
2ABCD1000X123Text2
3ABCD0999x123Something
4ABCD1234X5678
5ABCD3000x123Text3
6ABCD4999X123Text3
7ABCD5000X123Text4
8ABCD5999x123Text4
9ABCD6000x123Text5
10ABCD6999X123Text5
11ABCD7000x123Text6
12ABCD7499x123Text6
13ABCD7500X123Text7
14ABCD7999x123Text7
1580000
16ABCD8000x123Text8
17ABCD8000#123
18ABCD8499X123Text8
19ABCD8500x123Text9
20ABCD8500-123
21ABCD8999X123Text9
22ABCD9000x123Text10
23ABCD9499X123Text10
24ABCD9500x123Text11
25ABCD9999x123Text11
26
72 Hr
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,836
Members
449,343
Latest member
DEWS2031

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