2017-08-17 66 views
0

我已經啓動了一個Excel宏,在選擇後插入一個空行,但我想要空白rowheight = 6。我是VBA新手,但很想學習。如何插入特定高度的空行進行選擇?

這是針對正在工作的項目。

Dim Rng As Range 
Dim WorkRng As Range 
Dim FirstRow As Integer, xRows As Integer, xCols As Integer 
On Error Resume Next 
Set WorkRng = Application.Selection 
Set WorkRng = Application.InputBox("Range", WorkRng.Address, Type:=8) 
FirstRow = WorkRng.Row 
xRows = WorkRng.Rows.count 
xCols = WorkRng.Columns.count 
Application.ScreenUpdating = False 
WorkRng.Cells(xRows, 1).Resize(1, xCols).Select 

Do Until Selection.Row = FirstRow 
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
    Selection.Offset(-1, 0).Select 

Loop 

Application.ScreenUpdating = True 
End Sub 
+2

從右腳開始。停止使用錯誤恢復下一步,直到你知道如何。 http://www.cpearson.com/Excel/ErrorHandling.htm – niton

回答

0

中插入行,使用如下代碼rows(3).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove並更改列寬使用.ColumnWidth屬性。下面的代碼插入您提到的間隔柱。

Option Explicit 
Sub insertSpacerColumns() 
Dim r As Range, i As Integer, startCol As Integer, numSpacers As Integer 
Set r = Application.InputBox("Select columns", , , Type:=8) 
i = r(, 1).Column + 1 
numSpacers = r(, r.columns.Count).Column - i + 1 
Do 
    columns(i).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    columns(i).ColumnWidth = 1 
    i = i + 2 
    numSpacers = numSpacers - 1 
Loop Until numSpacers = 0 
End Sub 
1

你想要的.RowHeight()屬性:

Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
Selection.Offset(1,0).RowHeight = 6 
Selection.Offset(-1, 0).Select 

...但是請注意,這是最好的Avoid using .Select/.Activate

+0

謝謝!在選擇一組列後,我也無法添加間隔列,而且我還希望它們是特定的寬度。 – Wendi