Cell() vs. Offset(): ActiveCell.Offset(0,MyVar-1).select works. ActiveCell.Cell(0,MyVar).select does not.

JohnKauffman

New Member
Joined
Nov 1, 2012
Messages
36
I made a simple macro to move a row up one row (cut, selection Insert). I wanted to leave the active cell in the same col as at the start, so I save the start col# in intStartCol, getting it from ActiveCell.column. This is One Based

At the end when I set the active cell back to the original column this works: ActiveCell.Offset(0,intStartCOl-1) but zero based. But can't get ActiveCell.Cell(0,intStartCOl) to work (one based and therefore cleaner)

Why won't ActiveCell.Cell() work for last line?

Sub MoveRowUpOne()
'Action: move up one row Active Cell's row
'End with same col selected as at start
' n.b.if >1 row selected to start, only top row moves up
' n.b.if >1 col selected to start, ends w/ selection in left col
'''''''''''''''''''''''
' save col of the selection at start
Dim intStartCol As Integer
intStartCol = ActiveCell.Column
' select entire current row, cut
ActiveCell.Rows("1:1").EntireRow.Cut
'select the row one up
'since insert goes above row,only need to move up one.
ActiveCell.Offset(-1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
' after insert, entire row will be selected. Change to original col
' saved from ActiveCell.Column = 1 base, Offset 0-base
ActiveCell.Offset(0, intStartCol - 1).Select
' !!! why not works: ActiveCell.Cell(0,inStartCol).select?
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
You are missing a t
Code:
[COLOR=#333333]
ActiveCell.Cell(0,in[/COLOR][COLOR=#ff0000]t[/COLOR][COLOR=#333333]StartCol).select

[/COLOR]
 
Last edited:
Upvote 0
Now you can see the benefit of using Option Explicit if you are declaring variables (which most people recommend).

If you had put Option Explicit at the start of your code it would have highlighted "inStartCol" as being a variable not declared indicating an issue as you had declared "intStartCol" already(it showed up straight away when I posted it in my module).

Anyway glad it helped.
 
Upvote 0

Forum statistics

Threads
1,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

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