2016-06-22 27 views
0

我有一個電子表格拆分成部分和每個部分有一個'添加新行',它添加到該部分中的現有行。添加一個新行到選定的行中的一個部分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 

回答

0

舉例來說,如果你確信你的範圍在A1,利用具有數據下面的代碼:

所有的
Dim lastrow as Integer 

lastrow = Worksheets(sSheetName).Range("A1").CurrentRegion.Rows.Count 
Rows(lastrow + 1).Select 
Selection.Insert Shift:=xlDown 
0

首先,擺脫所有的選擇。它們很慢並且容易導致錯誤。

例如,而不是

Rows(s).Select 
Selection.Insert Shift:=xlDown 

使用

Rows(s).Insert Shift:=xlDown 

其次,你不需要投行號作爲字符串和修剪。 &會導致它們被轉換爲字符串。

我刪除了不必要的行'並添加了評論與''

Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = "Sheet3") 'c_Alloc2SpendSheetName) 
'Adds new line to the list of allocated to spend 

Dim c As Range 
Dim lastRow As Long 'I renamed s so it's more obvious what it does 

''this is to make sure we're always on the right sheet 
With Worksheets(sSheetName) 

    'get the budget line position 
    ''range("A:A") or columns(1) is really just a matter of taste 
    Set c = .Columns(1).Find(sBudgetLine, LookIn:=xlValues) 
    If Not (c Is Nothing) Then 
     ''instead of selecting the cell in the last row, we find the index of the last row and use that instead 
     's = Trim(Str(c.Row)) 'you don't use s before resetting it? 
     'Range("B" & Trim(Str(c.Row))).Select 
     'Selection.End(xlDown).Select 
     lastRow = .Cells(c.Row, 2).End(xlDown).Row 'see how you can skip all the selecting? 

     ''just insert the lines directly 
     'If Selection.Value = "Period" Then 
     If .Cells(lastRow, 2).Value = "Period" Then 
      .Rows(lastRow + 2).Insert Shift:=xlDown 
      lastRow = lastRow + 2 
     Else 
      .Rows(lastRow + 1).Insert Shift:=xlDown 
      lastRow = lastRow + 1 
     End If 

     ''what is this for? Rows("4:4") works but it's unnecessary 
     's = s & ":" & s 

     ''remove selection 
     'Rows(s).Select 
     'Selection.Insert Shift:=xlDown 
     ''this is what you'd do if you didn't insert the row above 
     'Rows(s).Insert Shift:=xlDown 

     ''instead of copying, just assign the range 
     's = Trim(Str(Selection.Row)) 'why? you don't use it anymore 
     'Range("E10").Copy 
     'Cells(Selection.Row, 5).Select 
     'ActiveSheet.Paste 
     'Application.CutCopyMode = False 
     .Cells(lastRow, 5) = .Cells(10, 5) 

     ''is this really necessary? 
     'Range("A" & Trim(Str(c.Row))).Select 
     .Cells(c.Row, 1).Select 
    End If 
End With 
End Sub 
+0

好的,謝謝。但是會不會有一種將CurrentRegion分配給.Rows(lastRow + 2)的方法。插入Shift:= xlDown來計算節中的行的列表,因爲它們將是一個隨機數。 – user3565164

+0

我不確定我明白你的意思。 'lastRow = .Cells(c.Row,2).End(xlDown).Row'處理可變數量的行。我沒有理由計算當前區域的行數,因爲您只對該區域的末端感興趣。 – arcadeprecinct

+0

是的,但它意味着當你添加一個新行時,它總是會添加到跳過的2個(標題,列標題)下面,而不管列表中是否有任何行數。 – user3565164