Using VBA, select from activecell to a certain value below?

xluserg

Board Regular
Joined
Jan 30, 2010
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hi Good day

I've been trying to work this out, and then googled with no luck.

How do I highlight "Select" from activecell, down to a cell below that has the value "Total" using VBA code.

Sometimes there is a different number of cells in between these two values.

Thanks

Graham
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:
Code:
Sub Macro1()
n = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
For r = ActiveCell.Row To n
If Cells(r, ActiveCell.Column) = "Total" Then
Range(ActiveCell, Cells(r, ActiveCell.Column)).Select
End If
Next
End Sub
 
Upvote 0
One way:

Code:
Dim rngFound As Range

Set rngFound = Activecell.EntireColumn.Find(What:="Total",After:=Activecell,SearchDirection:=xlNext,LookAt:=xlWhole,Lookin:=xlValues)

If Not rngFound Is Nothing Then
  Range(Activecell,rngFound).Select
Else
 MsgBox "Total not found below active cell!"
End if
 
Upvote 0
hi Machopicho
Your code selects from activecell to the 8th occurrence of Totol "activecell A15 to A160" I don't know why that is?


Hi Richard
Your code works fine


Thanks guys for the replies


Also what if I wanted to do the same to find a word in a string of words and numbers: "date 1990001 Total 09837" (could be any length of words and numbers)

Thanks again guys

Graham
 
Upvote 0
To answer the second question, see Richard's solution

Code:
LookAt:=xlWhole
change to

Code:
LookAt:=xlPart

Machopicho's solution does what you have identified because it doesn't stop after it finds the first 'total' but rather after it has checked all the data in the column.
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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