VBA assigning ActiveCell.SpecialCells to variable

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
102
Hi

Can anyone please explain why below code works?

If I do debug.print x I can see that x got value of the last cell rather than the address of last cell. So it is not clear to me how below loop works if as a second argument of Range I entered is x, which should not provide correct value (according to debug.print) but code is working well. So I am not sure what’s going on here. When I tried to assing ActiveCell.SpecialCells(xlCellTypeLastCell).Address to x I got mismatch error.


Code:
Sub test()
   
    Dim x As Range
 
    Set x = ActiveCell.SpecialCells(xlCellTypeLastCell)

    For Each x In Range("d4", x)
    x.Offset(0, 1) = x * 1.5
    Next x
   
 
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi illusionek,

In your code example the Range variable x is initially assigned the Range Object returned by ActiveCell.SpecialCells(xlCellTypeLastCell)

The Application.Range(Cell1, Cell2) property accepts many data types for the Cell1 and Cell2 parameters.
So when your code evaluates the expression,
Code:
Range("d4", x)
...it uses x as a Range Object instead of reading the Value property of that Range.

When you observe that you "can see that x got value of the last cell rather than the address of last cell" that's probably because hovering your cursor over the "x" variable or doing a Debug.Print x statement will return the default Value property. That doesn't mean that x has been assigned just that Value- it's been assigned the entire Range Object.

btw, Even though your code works, I'd recommend against using the variable x for both Each cell in a Range and one of the arguments used to define that Range. At best, it makes the code harder to follow. At worst, in similar scenarios, it can lead to unintended consequences.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,207,109
Messages
6,076,599
Members
446,215
Latest member
userds5593

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