VBA SpecialCells Method

jjlafond

Board Regular
Joined
Jul 17, 2014
Messages
56
Is it possible to return a range containing a "specific text"?

Example: Return all cells containing *Pick this cell* in it?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Is it possible to return a range containing a "specific text"?

Example: Return all cells containing *Pick this cell* in it?

Are the asterisks you placed around the text supposed to be "wildcards"? In other words, is the text the only thing in the cell or could it be embedded within other text in the cell?
 
Upvote 0
The asterisks have no particular meaning, just a way to separate from the rest of the sentence.
I have several cells containing ONLY:

#1
#2
#1-4

I would like to select all cells containing #2.
 
Upvote 0
The asterisks have no particular meaning, just a way to separate from the rest of the sentence.
I have several cells containing ONLY:

#1
#2
#1-4

I would like to select all cells containing #2.

This function (which can only be called from other VB code not associated with a UDF), will return a range consisting of the cells whose sole content is the text passed into it as an argument...

Code:
Function RangeContaining(TextToFind As String) As Range
  ActiveSheet.UsedRange.Replace TextToFind, "#N/A", xlWhole
  Set RangeContaining = ActiveSheet.UsedRange.SpecialCells(xlConstants, xlErrors)
  RangeContaining.Value = TextToFind
End Function

Here is a simple macro that demonstrates the use of this function...

Code:
Sub Test()
  MsgBox RangeContaining("#2").Address(0, 0)
End Sub
 
Upvote 0
You are a lifesaver. Works perfectly. I should be able to incorporate this into other areas of my code as well to save a lot of time.
 
Upvote 0
Hopefully a little more help with this one too:

I am trying to create a line graph using the range I just created. This code comes from the macro recorder when selecting several cells one at a time. I would like to be able to create the graph using the defined range I just created (the new range contains only numbers defined by using an .offset on the sub above).


ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range(''Mold X & R Template'!$C$24,'Mold X & R Template'!$E$24,'Mold X & R Template'!$G$24,'Mold X & R Template'!$I$24,'Mold X & R Template'!$K$24,'Mold X & R Template'!$M$24,'Mold X & R Template'!$O$24,'Mold X & R Template'!$Q$24,'Mold X & R Template'!$S$24")

I do not need the sheet name (Mold...Tempalte). All data comes from the same sheet. (Unless the format requires it to be there.)
 
Upvote 0
I figured it out. Here's the code for anyone with the same issues:

ActiveChart.SetSourceData Source:=Target1Range
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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