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
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