With statement returns unexpected result

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,940
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have values in cells A1, B1 and C1.

I want to shift cell B1 to the right, so the value in C1 is shifted to D1 and the value in B1 is shifted to C1.

Then I want to put the value of "abc" into cell B1.

This is my code:

Code:
Range("B1").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
   
Range("B1").Value2 = "abc"

This works as expected.


However, I thought it would be compact if I used a With Statement, so I changed it to:

Code:
With Range("B1")
   
    .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
   
    .Value2 = "abc"
   
End With

What I found surprising was after running this second piece of code, cell B1 is blank and cell C1 contains the value "abc".

I expected cell B1 to contain the value "abc".

What is wrong?

Thanks
 
Your With statement is holding a reference to a cell. Your first statement moves that cell, but the reference is still to that same cell, so the next line writes to C1. Your original code refers to B1 each time, but the second time B1 is not the same cell as it was in the first line.
 
Upvote 0
Your With statement is holding a reference to a cell. Your first statement moves that cell, but the reference is still to that same cell, so the next line writes to C1. Your original code refers to B1 each time, but the second time B1 is not the same cell as it was in the first line.
Thanks for the explanation.

Seems I must be cautious when tidying up code using With. It's not identical to factorising in maths.
 
Upvote 0
Hello,

That's an interesting question. Here is the explanation. Think of the "With" as an anchor on a cell and Insert as a real insertion of new data. When you do the insertion, you actually move the cell B1 to the right, so in C1. So then, when you assign a value, your "with cell" is no longer in B1 but in C1. Here below you should run this code to understand the cell "moving around".

VBA Code:
Sub test()
  With Range("B1")
      Debug.Print .Address  ' will show B1
      .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
      Debug.Print .Address  ' will show C1
  End With
End Sub

To compensate you can use a .Offset, or simply do as you did first, so you reevaluate what is at the address "B1".
 
Upvote 0
Using With is like using a variable - your revised code is essentially the same as:

VBA Code:
Dim cell As Range
Set cell = Range("B1")
cell.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
  
cell.Value2 = "abc"

The Range property actually returns a different pointer each time you call it, even if you are referring to the same cell (which is why it's usually pointless testing range variables using Is)
 
Upvote 0

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