PDA

View Full Version : Code understanding



av8tordude
07-11-2011, 04:24 PM
Can some explain what these two pieces of code are accomplishing?

Range("intl_rates").Columns(1).Cells(1, 1).Insert Shift:=xlDown

Range("intl_rates").Columns(1).Cells(1, 1).Offset(-1).Delete Shift:=xlUp

GTO
07-11-2011, 11:23 PM
Hi Aviator,

For example, in a new/blank workbook, create the named range to = C3:E14.

Plunk this in the sheet's module, and with the immediate window displayed, step through the code. If you keep VBE about half screen size, you can see it make the changes as you execute each line of code; and see what is being referred to in the immediate window.

Sub exa()
'Let "intl_rates" reference C3:E14"

Debug.Print Range("intl_rates").Address(0, 0) ' Returns C3:E14
Debug.Print Range("intl_rates").Columns(1).Cells(1, 1).Address(0, 0) 'Returns C3, thus
Range("intl_rates").Columns(1).Cells(1, 1).Insert Shift:=xlDown ' inserts a cell at C3, pushing down
Debug.Print Range("intl_rates").Columns(1).Cells(1, 1).Offset(-1).Address(0, 0) 'Returns C2, thus
Range("intl_rates").Columns(1).Cells(1, 1).Offset(-1).Delete Shift:=xlUp 'deletes C2, shifting the cells back up
End Sub
Hope that helps,

Mark