如何重新定义循环内的最后一行

以下代码在遇到col B中的空白单元格时会插入行,而col A中存在特定值。根据条件,插入的行将使用数组中的值填充。 每插入一行,我都需要改变最后一行的值,所以代码可以计算col B中的下一个空白单元格,但我不知道如何重新定义最后一行。 我该如何做这项工作?

  1. 我需要使用什么样的循环?
  2. 我在哪里重新定义最后一行?
  3. 如何移动到列B中的下一个空白单元格?

非常感谢你,

Sub MissingValues() Dim zArr As Variant Dim yArr As Variant Dim LastRow As Long Dim sht As Worksheet Dim i As Long Set sht = ThisWorkbook.Worksheets("QC") Application.EnableEvents = False Application.ScreenUpdating = False yArr = Array(Array("HD300 Removed Alt", "EGFR", "", "", "L861Q", "5"), _ Array("HD300 Removed Alt", "EGFR", "", "", "KELRE745delinsK", "5"), _ Array("HD300 Removed Alt", "EGFR", "", "", "L858R", "5"), _ Array("HD300 Removed Alt", "EGFR", "", "", "T790M", "5"), _ Array("HD300 Removed Alt", "EGFR", "", "", "G719S", "5")) zArr = Array(Array("HD200 Removed Alt", "BRAF", "", "", "V600E", "10.5"), _ Array("HD200 Removed Alt", "KIT", "", "", "D816V", "10"), _ Array("HD200 Removed Alt", "EGFR", "", "", "KELRE745delinsK", "2"), _ Array("HD200 Removed Alt", "EGFR", "", "", "L858R", "3"), _ Array("HD200 Removed Alt", "EGFR", "", "", "T790M", "1"), _ Array("HD200 Removed Alt", "EGFR", "", "", "G719S", "24.5"), _ Array("HD200 Removed Alt", "KRAS", "", "", "G13D", "15"), _ Array("HD200 Removed Alt", "KRAS", "", "", "G12D", "6"), _ Array("HD200 Removed Alt", "NRAS", "", "", "Q61K", "12.5"), _ Array("HD200 Removed Alt", "PIK3CA", "", "", "H1047R", "17.5"), _ Array("HD200 Removed Alt", "PIK3CA", "", "", "E545K", "9")) LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row i = 3 Do While i <= LastRow Debug.Print i Debug.Print LastRow If IsEmpty(Cells(i, 2).Value) = True Then Cells(i, 2).Value = "header" If InStr(1, Cells(i, 1).Value, "HD200", 1) > 0 Then Rows(i & ":" & i + 10).Insert Shift:=xlDown 'Insert 11 rows Worksheets("QC").Range("A" & i & ":F" & i + 11).Value _ = Application.Index(zArr, 0) 'Recalculate LastRow i = i + 12 LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row ElseIf InStr(1, Cells(i, 1).Value, "HD300", 1) > 0 Then Rows(i & ":" & i + 4).Insert Shift:=xlDown 'Insert 5 rows Worksheets("QC").Range("A" & i & ":F" & i + 5).Value _ = Application.Index(yArr, 0) i = i + 6 'Recalculate LastRow LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row Else i = i + 1 End If 'LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row End If 'LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row Loop Application.EnableEvents = True Application.ScreenUpdating = True End Sub 

每当你插入或删除Excel工作表中的行,你必须从最后一行。

 For i = LastRow To 3 Step -1 'Your Code Goes Here... Next i 

自定义这个伪代码到你的特定语法应该工作 –

  if ( i+1 = LastRow) custom code here... increment i