With 'Set StrVal=ActiveCell.Address' and ‘Dim StrVal As String’ I get 'Object required' error With ‘Dim StrVal As Range’, a ‘Type Mismatch’ error

Cozkincaud

New Member
Joined
Apr 18, 2019
Messages
8
I need to copy the ActiveCell.Address to a string variable to test if a cell is in a range for the Intersect function. I would be very grateful to anyone who could resolve this problem for me. I would also be grateful for any suggestions or comments on the other code in this macro.

My thanks in advance for your comments and suggestions. Here is the macro:

Dim QualifierRange As Range
Dim StrVal As Range NOTE: I also run the macro with "StrVall As String"
QualifierRange = Sheets("Tags III").Range("E43:E50"
Set QualifierRange = Range
Set StrVal = ActiveCell.Address 'Error is 'Type Mismatch' if I Dim Strval as 'Range'. If I Dim it as 'String' i get an 'Object required' error.
If Intersect(QualifierRange, StrVal) Is Nothing Then
ActiveCell.Select
Selection.Copy
Sheets("Tags Insert").Select
Range("G14").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Else
ActiveCell.Select
Selection.Copy
Sheets("Tags Insert").Select
Range("G8").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
End If
End Sub

The macro is intended to copy a cell value and then paste it in another worksheet (TagsInsert). Where It will be pasted in this other sheet (TagsInsert) depends on if the copied cell is in a specified range (QualifierRange)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Sorry! There is a typo in my macro. The line 'QualifierRange = Sheets("Tags III").Range("E43:E50"', should have a closing bracket. When I run my macro this line does have a closing bracket. So a missing bracket is not the problem. Again, apologies for my typing error.
 
Upvote 0
If you Dim 'StrVal' as a String, you need to remove the 'Set' from the line:

VBA Code:
Set StrVal = ActiveCell.Address
to
VBA Code:
StrVal = ActiveCell.Address

If you Dim StrVal as a Range, then perhaps just this:

VBA Code:
Set StrVal = ActiveCell
 
Upvote 0
If you Dim 'StrVal' as a String, you need to remove the 'Set' from the line:

VBA Code:
Set StrVal = ActiveCell.Address
to
VBA Code:
StrVal = ActiveCell.Address

If you Dim StrVal as a Range, then perhaps just this:

VBA Code:
Set StrVal = ActiveCell
Thanks for your suggestions, but there is still a problem:

If I run the macro with Dim StrVal As String and StrVal = ActiveCell.Address I get a '"Type Mismatch" error

If I run it with Dim StrVal As Range and Strval = ActiveCell (no 'set' function' and no '.address' for ActiveCell), .I get Run-time error 91, "Object variable or with block variable not set". Does this mean I need the set function if I use Dim StrVal As Range?

I cannot see that I can drop address from 'ActiveCell.Address' as it is the address of the cell that I need to pass the StrVal for the following Intersect statement (If Intersect(QualifierRange, CurrentCell) Is Nothing Then).
 
Upvote 0
If I run the macro with Dim StrVal As String and StrVal = ActiveCell.Address I get a '"Type Mismatch" error
I didn't test this part, so I am not entirely sure of the solution.

If I run it with Dim StrVal As Range and Strval = ActiveCell (no 'set' function' and no '.address' for ActiveCell), .I get Run-time error 91, "Object variable or with block variable not set". Does this mean I need the set function if I use Dim StrVal As Range?
Yes, exactly. If you are using a variable declared as a Range, you have to use 'Set' with it, and I believe you can omit '.Address' but try it both ways and see what works.
 
Upvote 0
@Cozkincaud what happens with the code below?
VBA Code:
Sub Cozkincaud()
    Dim QualifierRange As Range
    Dim StrVal As String                         'NOTE: I also run the macro with "StrVall As String"

    Set QualifierRange = Sheets("Tags III").Range("E43:E50")

    StrVal = ActiveCell.Address                  'Error is 'Type Mismatch' if I Dim Strval as 'Range'. If I Dim it as 'String' i get an 'Object required' error.

    If Intersect(QualifierRange, Range(StrVal)) Is Nothing Then
        ActiveCell.Copy Sheets("Tags Insert").Range("G14").End(xlUp).Offset(1, 0)
    Else
        ActiveCell.Copy Sheets("Tags Insert").Range("G8").End(xlUp).Offset(1, 0)
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,144
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