tjdickinson
Board Regular
- Joined
- Jun 26, 2021
- Messages
- 61
- Office Version
- 365
- Platform
- Windows
In a workbook with multiple sheets, I want to find the address of a cell containing the string "MIS", which will always be in a sheet in which cell A1 begins "P " (with a space after the letter P). I then want to save this address to a range variable and pass it to another sub which will set the value of every cell at that address on every sheet to "MIS".
Here's the code I have so far:
The first problem seems to be that misCell is defined as the contents of the cell, not the address. When I run
But when I run
Maybe this is fine, but I feel like the variable should be holding the range, not the value, and I'm not sure how to fix that.
The second problem is in using the variable to set the cell value in other sheets. If I use
Thanks for your help!
Here's the code I have so far:
VBA Code:
Sub misRef(misCell As Range) ' the variable misCell is created as range in another sub which calls this sub
Dim xSh As Worksheet
For Each xSh In Worksheets ' search every sheet in the workbook
xSh.Select
If Left(Range("A1"), 2) = "P " Then ' if A1 starts with "P " (with a space after P) -- this sheet will always contain 'MIS' somewhere in it
Set misCell = Range("C4:F5,B7:F8,B11:E12,B14:E15").Find("MIS") ' search the range c4 (etc) for the string "MIS" and define the variable with the range
End If
Next
End Sub
Debug.Print misCell
, it spits out:
Code:
MIS
1A1
Debug.Print misCell.Address
it gives:
Code:
$E$5
The second problem is in using the variable to set the cell value in other sheets. If I use
misCell.Value = "MIS"
, it redefines the variable as "MIS", even though it is supposed to be a range (and Range.Value normally sets what's in the cells of that range). I tried using With misCell
and With misCell.Address
, but that gave me errors. So, again, I'm not sure what I need to do here.Thanks for your help!