Selecting the Active Column

D

dolphinv4

Hi,

I need a macro such that the macro will copy the
particular column that my active cell is. Currently, I
recorded a macro that looks like this:

Sub Macro1()
Columns("C:C").Select
Selection.Copy
Selection.Insert Shift:=xlToLeft
End Sub

1) How can I change the above such that i'll click on a
cell that I want the macro to copy the column, then run
the macro.

2) Is it possible for the macro to track the last
column, copy the column and insert to the left of the
column?

Thanks!

Regards,
Val
 
T

Tom Ogilvy

You would have to use the selectionchange event with a global or static
variable to record what the last column selected was.

At the top of a general module put in

Public lastrng as Range
Public oldrng as Range

Sub Insertcolumn()
dim rng as Range
set rng = oldrng.EntireColumn
rng.copy
activeCell.EntireRow.Insert Shift:=xlToRight
Application.CutCopyMode = False
End Sub


Right click on the sheet tab and select view code

put in code like this

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If lastrng Is Nothing Then
Set lastrng = Target(1)
End If

Set oldrng = lastrng
Set lastrng = Target(1)

End Sub


Lightly tested.
This may interfere with your ability to copy and paste or use undo.
 
Top