Adding to a Range

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,120
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
VBA Code:
                        If lstYr > "" Then
                                Set rtracks = .Range("A1:S1").Find(What:="*" & Right(lstYr, 2) & "*", LookIn:=xlValues, LookAt:=xlPart)
                                For Each Found In rtracks
                                Debug.Print Found
                                Next
                                Stop
                        Else
                                Set rtracks = .Range("A2:S2")
                                For Each Found In rtracks
                                Debug.Print Found
                                Next
                                Stop
                        End If
if lstYr is "" rtracks contains all Founds in that range. But otherwise it only has the first instance.
Because the way rtracks is used, I need it to contain every instance. How might I do that ?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Can you show us an image of your data, and explain exactly what it is you are trying to do?
It would also be helpful if you showed us your complete VBA procedure, so we can where these variables are coming from and what they are being set to.
 
Upvote 0
VBA Code:
                        If lstYr > "" Then
                                Set rtracks = .Range("A1:S1").Find(What:="*" & Right(lstYr, 2) & "*", LookIn:=xlValues, LookAt:=xlPart)
                                For Each Found In rtracks
                                Debug.Print Found
                                Next
                                Stop
                        Else
                                Set rtracks = .Range("A2:S2")
                                For Each Found In rtracks
                                Debug.Print Found
                                Next
                                Stop
                        End If
if lstYr is "" rtracks contains all Founds in that range. But otherwise it only has the first instance.
Because the way rtracks is used, I need it to contain every instance. How might I do that ?
The function 'fncFindInstancesInRange' will return a range of cells, each of which contains the value searched for.

I have added a calling sub and some worksheet data for you to test it.

Can you use this for your project?

Adding to a Range.xlsm
ABC
1Column 1Column 2Column 3
2GA3NA5GU^xN^#0Cf
3i62P@HPKaxyJLY3e*V#GU^%xf^bs
4rxN^#%xf^bs@m!r4KGl%elgw
5Am#k*zYk2i49$1R
64KGl%elgw0O$Bkhu70Wr&Zzr
7X6Xme3gDdoa&!!yad5Zfy
8Cfhu70Wr&BQIhc7T0dCOPMT*7*
9c7T0pkzEzHSDuoowh8vj2&ss
10ZzrdCOPMpkzEY1lxLo54INR
11@p3lipCMV!bb#FW8
12T*7*o54EsuUvGn8ycEH
13Y1lxLmtZir%gf04cGA3NA5
14INRG2$zYkljDJcq
15@6EbKJejnc9MCuWoFFp1
16n8yipCASB1
17EsuUv2$8^vlvSQ6Vt9tq
18cEHjDJcqAMV!KJejncTxwQ9O
19u57Bha%&qnrf$iFD@m!
20SB1TxwQ99MCy$BkBQIh
Sheet1
Cell Formulas
RangeFormula
A1:C1A1="Column " & COLUMN()


VBA Code:
Public Sub subAddingToARange()
Dim rng As Range
    
    Set rng = fncFindInstancesInRange(Worksheets("Sheet1").Range("A2:C20"), 1)
             
    MsgBox rng.Address

End Sub

Public Function fncFindInstancesInRange(SearchRange As Range, SearchWord As String) As Range
Dim Position As Long
Dim cell As Range
Dim rng As Range

  For Each cell In SearchRange
  
      Position = InStr(1, cell, SearchWord, vbTextCompare)
    
      Do While Position > 0
      
        If StrComp(SearchWord, Mid(cell.Value, Position, Len(SearchWord))) = 0 Then
  
            If Not rng Is Nothing Then
            
              Set rng = Union(rng, cell)
              
            Else
              
              Set rng = cell
              
            End If
        
        End If
       
        Position = InStr(Position + 1, cell, SearchWord, vbTextCompare)
      
      Loop
      
  Next cell
    
  Set fncFindInstancesInRange = rng
  
End Function
 
Upvote 0
Solution
Joe, what I posted was an example of a particular task in isolation. Without any more code.
Someone told me a range didn't have to be continuous, but if it was ""I:K" then column J is included.. But I just found that "I,K"
will only include those 2 columns. @HighAndWilder, your code worked perfectly with rng.Address correctly being $I$1,$K$1
I ended up Looping through the range and building a string of column Letters, coma delimited. Your method is more elegant so I've changed to that.
Thanks, nice solution.
 
Upvote 0
Joe, what I posted was an example of a particular task in isolation. Without any more code.
Someone told me a range didn't have to be continuous, but if it was ""I:K" then column J is included.. But I just found that "I,K"
will only include those 2 columns. @HighAndWilder, your code worked perfectly with rng.Address correctly being $I$1,$K$1
I ended up Looping through the range and building a string of column Letters, coma delimited. Your method is more elegant so I've changed to that.
Thanks, nice solution.
I'm glad that it worked for you.

Have a read of this about non-contigious ranges, Ask any questions if you have any.

Range Areas: Working with Non Contiguous Ranges Using Range Areas in VBA - FasterCapital
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,746
Members
453,370
Latest member
juliewar

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