我有一個電子表格拆分成部分和每個部分有一個'添加新行',它添加到該部分中的現有行。添加一個新行到選定的行中的一個部分vba
我已經在Currentregion函數中進行了計數行,但堅持選擇最後一行,然後在下面添加一行。
到目前爲止的代碼是我在哪裏可以添加一個新行,但我在尋找與CurrentRegion每個部分更加乾淨準確的解決方案 - 這可能與傳入的參數sBudgetLine做
Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = c_Alloc2SpendSheetName)
'Adds new line to the list of allocated to spend
Dim c As Range
Dim s As String
Worksheets(sSheetName).Activate
'get the budget line position
Set c = Worksheets(sSheetName).Range("A:A").Find(sBudgetLine, LookIn:=xlValues)
If Not (c Is Nothing) Then
s = Trim(str(c.Row))
Range("B" & Trim(str(c.Row))).Select
Selection.End(xlDown).Select
If Selection.Value = "Period" Then
s = Trim(str(Selection.Row + 2))
Else
s = Trim(str(Selection.Row + 1))
End If
s = s & ":" & s
Rows(s).Select
Selection.Insert Shift:=xlDown
s = Trim(str(Selection.Row))
Range("E10").Copy
Cells(Selection.Row, 5).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A" & Trim(str(c.Row))).Select
End If
End Sub
好的,謝謝。但是會不會有一種將CurrentRegion分配給.Rows(lastRow + 2)的方法。插入Shift:= xlDown來計算節中的行的列表,因爲它們將是一個隨機數。 – user3565164
我不確定我明白你的意思。 'lastRow = .Cells(c.Row,2).End(xlDown).Row'處理可變數量的行。我沒有理由計算當前區域的行數,因爲您只對該區域的末端感興趣。 – arcadeprecinct
是的,但它意味着當你添加一個新行時,它總是會添加到跳過的2個(標題,列標題)下面,而不管列表中是否有任何行數。 – user3565164