Assigning an unknown variable

ShaunD30

Board Regular
Joined
Jun 19, 2008
Messages
172
Not sure how to go about doing this. Lets say I have a sheet and this is column A

1234
1234
1234
1235
1235
1236
1236

I need the code to loop through the data in column A and assign a variable for the range of 1234..and then as soon as it finds 1235, assign a variable for the range which it finds 1235..and so on..so each unique set of values has a variable..

Is this possible?

Shaun
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Not sure what you mean by has a unique variable? Do you mean set it as a named range? Sorry I am leaving work now, but will check back later tonight or tomorrow if someone hasn't given you an answer, but I'm not completely sure what you are asking for?
 
Upvote 0
Perhaps a collection will work
Code:
Dim collectionOfRanges As New Collection
Dim keyStr As String, newRange As Range
Dim oneCell As Range
With ThisWorkbook.Sheets("Sheet1").Range("A:A"): Rem adjust
    For Each oneCell In Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    
        keyStr = CStr(oneCell.Value)
        Set newRange = oneCell
        
        On Error Resume Next
            Set newRange = Application.Union(collectionOfRanges(keyStr), oneCell)
            collectionOfRanges.Remove keyStr
        On Error GoTo 0
        collectionOfRanges.Add Item:=newRange, key:=keyStr
    Next
End With
MsgBox collectionOfRanges("1234").Address
 
Upvote 0
I get this error when it gets to the MsgBox

Run-Time Error '5':

Invalid procedure call or argument
 
Upvote 0
Is 1234 one of the cells in column A? If so, is it text with a leading/trailing space?

Try
Code:
MsgBox collectionOfRanges(CStr(Range("A1").Value)).Address
 
Upvote 0

Forum statistics

Threads
1,207,402
Messages
6,078,270
Members
446,324
Latest member
JKamlet

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