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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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