Convert Variant to Range

abc_xyz

New Member
Joined
Jan 12, 2022
Messages
47
Office Version
  1. 2016
Platform
  1. Windows
Trying to create a code which checks if the dictionary value exists in each cell and if exists then keep that range in memory using Union argument. However, I am facing error with Set Rng = MyRng(i, 1) Else Set Rng = Union(Rng, MyRng(i, 1)) line. Can anyone please help me understand what am I doing wrong?


VBA Code:
MyRng = .Range("V1:V" & .Cells(Rows.Count, "V").End(xlUp).Row).Value

                For i = 2 To UBound(MyRng, 1)
            
                    If Dic.Exists(MyRng(i, 1)) Then
                        If Rng Is Nothing Then Set Rng = MyRng(i, 1) Else Set Rng = Union(Rng, MyRng(i, 1))
                    End If
        
                Next i
            
                    If Not Rng Is Nothing Then Rng.EntireRow.Copy
                    
                    Set Rng = Nothing
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
MyRng is an array, not a Range so you cannot Set a range to a value, which is what you are trying to do.
Try it like
VBA Code:
Set Rng = .Cells(i, 1)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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