Adapt VBA code where "Find" is equal to a MID function (etc)

GTS

Board Regular
Joined
Aug 31, 2009
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hello

I'm trying to adapt the following to do something very similar, but struggling to get it to work.
The following code finds the word "TOTAL" (exactly) in a cell in Column A and then sets the height of the row below to 2.

1) I have a requirement where I need to find cells in Column A where the formula LEN(A1) = 9 and LEFT(A1,2) = "RI" or "RE" or "RW".

VBA Code:
    Dim c As Range
    Dim FirstAddress As String
    
    Set c = Columns("A").Find(What:="TOTAL", LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=True)
    If Not c Is Nothing Then
        FirstAddress = c.Address
        Do
            c.Offset(1, 0).RowHeight = 2
            Set c = Columns("A").FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If

Appreciate any assistance. Thank you.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Apologies... forgot to edit the title before posting (and seems that cannot be edited?). Dealing with LEFT, not MID function.
 
Upvote 0
Hello

I'm trying to adapt the following to do something very similar, but struggling to get it to work.
The following code finds the word "TOTAL" (exactly) in a cell in Column A and then sets the height of the row below to 2.

1) I have a requirement where I need to find cells in Column A where the formula LEN(A1) = 9 and LEFT(A1,2) = "RI" or "RE" or "RW".

VBA Code:
    Dim c As Range
    Dim FirstAddress As String
   
    Set c = Columns("A").Find(What:="TOTAL", LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=True)
    If Not c Is Nothing Then
        FirstAddress = c.Address
        Do
            c.Offset(1, 0).RowHeight = 2
            Set c = Columns("A").FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If

Appreciate any assistance. Thank you.
Where does the new requirement go in relation to the rest of the code?
 
Upvote 0
Also, can you show the rest of the code? This little bit doesn't seem to do anything when I test it.

Edit: Nevermind, FIND is case sensitive.
 
Upvote 0
This is part of a much larger macro. The adaptation I'm looking for would be a new segment of code. It would not 'fit in' to what I'm showing.
The snippet of code shown goes down Col A and wherever it finds the word "TOTAL" exactly in a cell, it will set the height of the row below to 2.

What I'm trying to do is to go down Col A and find any entry that starts with RI (or RE or RW) and if that entry is exactly 9 characters in length, then the row below is to be set to a height of 2.

Thanks.
 
Upvote 0
Ok, this should do it. Just make sure you change the variable "i" if you are already using it somewhere else in your code.
VBA Code:
Dim i As Long
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
    If Left(Range("A" & i).Value, 2) = "RI" Or Left(Range("A" & i).Value, 2) = "RE" Or Left(Range("A" & i).Value, 2) = "RW" Then
        If Len(Range("A" & i).Value) = 9 Then
            Range("A" & i).Offset(1, 0).RowHeight = 2
        End If
    End If
Next i
 
Upvote 0
Solution
Thanks dreid1011. Looks like that should work nicely. I have to shut down now and won't get to try this until next week. I'll return if I have any issues.
I was able to finally modify the code I posted (using Find instead of Left) but your solution should be more robust and faster I suspect. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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