2011-03-08 29 views
0

我有代碼,移動列左,根據按鍵的權利,但列寬不保留,因爲我移動列。我想我要搬到保留其寬度列,其通過移動列還保留它們的寬度。Excel VBA中移動列,並保留寬度

Public Sub moveColumnleft() 

    ActiveCell.EntireColumn.Select 
    Selection.EntireColumn.Cut 
    If ActiveCell.Column = 1 Then Exit Sub 

    ActiveCell.offset(0, -1).Insert Shift:=xlLeft 
    ActiveCell.offset(0, -1).Select 


End Sub 

Public Sub moveColumnRight() 

    ActiveCell.EntireColumn.Select 
    Selection.EntireColumn.Cut 
    ActiveCell.offset(0, 2).Insert Shift:=xlRight 
    ActiveCell.offset(0, 1).Select 

End Sub 

回答

2

存儲移動的列與witdth爲我工作(Excel 2007)。見下面的代碼

Public Sub moveColumnleft() 
    ActiveCell.EntireColumn.Select 
    Selection.EntireColumn.Cut 
    '' store column width in variable **tmp** 
    Dim tmp As Double 
    tmp = ActiveCell.EntireColumn.ColumnWidth 
    If ActiveCell.Column = 1 Then Exit Sub 

    ActiveCell.Offset(0, -1).Insert Shift:=xlLeft 
    ActiveCell.Offset(0, -1).Select 
    '' apply the stored width to the moved column 
    Range(ActiveCell.Address).ColumnWidth = tmp 
End Sub