I'm having difficulty selecting a range of cells in VBA. I already have a range of cells that are scattered, but am trying to add the three cells to the right of each cell in the range to the selection.
So far I've tried:
SelectedRange("Hi").Resize(1, 4).Select
, which gives an error when ran; I assume it does not work because SelectedRange is a range and not a cell.
SelectedRange() searches for the input string and returns a range of each cell that matches the input, which is a modified version of this code: http://www.thespreadsheetguru.com/the-code-vault/2014/4/21/find-all-instances-with-vba
Function SelectedRange(ByVal fnd As String) As Range
Dim FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range
Set myRange = ActiveSheet.UsedRange
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.find(what:=fnd, after:=LastCell)
'Test to see if anything was found
If Not FoundCell Is Nothing Then
FirstFound = FoundCell.Address
Else
GoTo NothingFound
End If
Set rng = FoundCell
'Loop until cycled through all unique finds
Do Until FoundCell Is Nothing
'Find next cell with fnd value
Set FoundCell = myRange.FindNext(after:=FoundCell)
'Add found cell to rng range variable
Set rng = Union(rng, FoundCell)
'Test to see if cycled through to first found cell
If FoundCell.Address = FirstFound Then Exit Do
Loop
Set SelectedRange = rng
'Report Out Message
MsgBox SelectedRange.Cells.Count & " cell(s) were found containing: " & fnd
Exit Function
'Error Handler
NothingFound:
MsgBox "No cells containing: " & fnd & " were found in this worksheet"
End Function
Thanks.