ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Range.Insert query (https://www.excelbanter.com/excel-programming/339262-range-insert-query.html)

[email protected]

Range.Insert query
 
Hi Guys,

I was trying to understand how to use the Range.Resize property but
after much googling and reading of help files gave up on that and tried
another tack.

That was to create a completely new range by using Range( cells( row,
column), cells( row, column)).

The following sub works as I would expect it to, but one thing I don't
understand, which highlights my ignorance of what I am actually doing,
is this. In the following code there are two commented MsgBoxes, the
first MsgBox produces an expected address, the second MsgBox produces a
different result I would assume because of the shift of cells.

Why does the shift of cells cause the address of my variable to change
though?

Thanks in advance,
Deon.

Sub testresize()

Dim testRange As Range
Dim newRange As Range
Dim addRows As Integer
Dim rngStartColumn As Integer
Dim rngStartRow As Integer
Dim rngEndColumn As Integer
Dim rngEndRow As Integer

addRows = 6
rngStartColumn = 0
rngStartRow = 0
rngEndColumn = 0
rngEndRow = 0

Set testRange = Range("B6:D6")
Set newRange = Range("B6:D6")

rngStartColumn = testRange.Column
rngEndColumn = rngStartColumn + testRange.Columns.Count - 1
rngStartRow = testRange.Row
rngEndRow = rngStartRow + testRange.Rows.Count - 1

With ActiveSheet
Set newRange = .Range(.Cells(rngStartRow, rngStartColumn),
..Cells(rngEndRow + addRows, rngEndColumn))

'' This MsgBox produces the expected result
MsgBox "Address before shift " & newRange.Address
newRange.Name = "xRange"
.Range("xRange").Insert Shift:=xlShiftDown

'' The MsgBox doesn't produce an expected result?
MsgBox "Address after shift " & newRange.Address
End With


End Sub


swiss-jimbo

Range.Insert query
 
Hi

When you use the constant xlShiftDown you are changing the row property
of the object newRange from 6 to 13.

newRange.Name = "xRange"
.Range("xRange").Insert Shift:=xlShiftDown

If you are trying to write a macro that shifts data around and then
produces a report I would first use the xlShiftDown and then reference
the desired range with a new variable.

Hope this helps

/ James



All times are GMT +1. The time now is 12:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com