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

时间:2016-02-08 03:27:53

标签: excel excel-vba vba

当遇到col B中的空白单元格并且col A中存在特定值时,以下代码会插入行。插入的行将根据条件填充数组中的值。每次插入行时,我都需要更改最后一行值,因此代码可以评估col B中的下一个空白单元格,但我无法弄清楚如何重新定义最后一行。我如何使这项工作?

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

        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
    

2 个答案:

答案 0 :(得分:2)

无论何时在Excel工作表中插入或删除行,都必须从最后一行开始。

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

答案 1 :(得分:0)

根据您的特定语法自定义此伪代码应该有效 -

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