Look for chars '*' or '#' in column range - excel vba

Nicha

New Member
Joined
Feb 10, 2023
Messages
48
Office Version
  1. 2016
Platform
  1. Windows
I need to build a procedure in vba that looks in a column range - (example $B$2:$B$8) - and check if any of those cells contain the chars '*' OR '#', in order to get the Row where the first match/partial match happens.
I've adapted a code presented on this site by Rick Rothstein, where the purpose was to check for the values {2,3,4,5,6,7,8,9,10}, using
Rich (BB code):
cells(Evaluate(Replace("MIN(IF( @={2,3,4,5,6,7,8,9,10},ROW(@)))", "@", Addr)), Found6.Column)
.

Q1_Table_2023-02-10.png


Suppose we have data in range [B2:B8] under the header "Total". I've adapted the Rick's Solution like this:
VBA Code:
Sub FindFirstOcurr_ValueIn_Rng()
'Find First Occurrence of Value in a Column

  Dim LastRow As Long, Found6 As Range, FirstNumber As Range, Addr As String
  Dim DataRow As Integer
  
  'Look for column "Total", where values 2 to 10 are to be found
  Set Found6 = cells.Find("Total", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
  
  If Found6 Is Nothing Then GoTo Skiddle
  
  LastRow = cells(Rows.Count, Found6.Column).End(xlUp).row
  Addr = cells(2, Found6.Column).Resize(LastRow - 1).Address
  
  'Devolve a 1ª linha
  'Set FirstNumber = cells(Evaluate(Replace("MIN(IF( @={2,3,4,5,6,7,8,9,10},ROW(@)))", "@", Addr)), Found6.Column)
  
  DataRow = Switch(IsNumeric(Evaluate("MATCH(""~*""," & Addr & ",0)")), Evaluate("MATCH(""~*""," & Addr & ",0)"), True, 9999)
     
  
Skiddle:
End Sub

The code below just works on the value in cell [B5]. In my case it's correct because is the first ocurrence. But, if the value on [B5] was '24*', that didn't work.
Can anyone help please?
 
This line If Not rng Is Nothing Then is redundant as if there are no text values, or the range contains formulae, the code will crash on the previous line.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If, as per your comments, the 1st text-value would equal the 1st occurence of one of these characters, then maybe try:

VBA Code:
Sub Test()

Dim DataRow As Long
Dim rng As Range

Set rng = Range("B2:B8").SpecialCells(xlCellTypeConstants, xlTextValues) 'Long syntax for .SpecialCells(2, 2)
If Not rng Is Nothing Then
    DataRow = rng.Row - 1
End If

End Sub
Hi, JvdV. Thank you too. Your code works well when exists text values. But if the range only contains Numeric values, that give a Run-Time error 1004 - No cells where found.
Thank you very much.
 
Upvote 0
Yeah, I suppose you want to now account for the fact no hit may occur. So what would you want to return if no single hit occurs? Since 'DataRow' appears to be an integer, maybe:

VBA Code:
With Application
    DataRow .IfError(.Evaluate(Replace("=MATCH(TRUE,((LEN(@)>3)+ISTEXT(@))>0,0)", "@", Addr)), 0)
End With

You'll get an 0 if no hit was found.
 
Upvote 0
Yeah, I suppose you want to now account for the fact no hit may occur. So what would you want to return if no single hit occurs? Since 'DataRow' appears to be an integer, maybe:

VBA Code:
With Application
    DataRow .IfError(.Evaluate(Replace("=MATCH(TRUE,((LEN(@)>3)+ISTEXT(@))>0,0)", "@", Addr)), 0)
End With

You'll get an 0 if no hit was found.
Thank you JvdV.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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