简体   繁体   English

如何在一个范围内的列中找到第一个空单元格,并在后续公式中使用行引用?

[英]How can I find the first empty cell in a column within a range, and use the row-reference in subsequent formulas?

I am currently working on a code to import variables from different TABs to a MASTER sheet (within one Excel file) using VBA. 我目前正在使用VBA将代码从不同的TAB导入到MASTER工作表(在一个Excel文件中)的代码中。 I want to insert information from these tabs into the first empty cell of a column on the master sheet, within a specific range of rows. 我想将这些选项卡中的信息插入到主表中特定行范围内的列的第一个空单元格中。

Let me be more specific: Every 20 rows get the same identifier (MASTER sheet), the next 20 rows get the second identifier (not part of the attached code). 让我更具体一点:每20行获得相同的标识符(主表),接下来的20行获得第二个标识符(不属于所附代码的一部分)。 If the TAB identifier is the same as the MASTER identifier, I want the information in the TAB row to be imported in the first empty MASTER row with the correct identifier. 如果TAB标识符与MASTER标识符相同,我希望使用正确的标识符将TAB行中的信息导入第一个空MASTER行中。

As you can see below, I am currently using an incredibly convoluted IF function to get this done. 正如您在下面看到的那样,我目前正在使用令人费解的IF函数来完成此操作。 It works fine, but as I am importing lots of information from lots of tabs, my code doesn't fit inside 1 Module and I've had to create 13 separate Modules. 它工作正常,但是由于我要从许多选项卡中导入大量信息,因此我的代码不适合1个模块,因此我不得不创建13个单独的模块。 More importantly, if I want to add a column to one of the sheets, I have to go through lots of manual labor (time-consuming and error-prone) to adapt the code. 更重要的是,如果我想在其中一张纸上添加一列,我必须经历大量的体力劳动(既费时又容易出错)以适应代码。

My question: Is there a way to simplify the attached code? 我的问题:有没有一种方法可以简化所附代码?

I could imagine there being an IFLOOP or ELSEIFLOOP, or there is a clever way to avoid the IF function altogether, and find the first empty cell within the 20-row-range? 我可以想象有一个IFLOOP或ELSEIFLOOP,或者有一种巧妙的方法可以完全避免IF函数,并找到20行范围内的第一个空单元格?

Thanks so much! 非常感谢!

CODE: 码:

Sub Import_IATI_data()

Dim PGTS_row as Integer
Dim IATI_row as integer

For PGTS_row = 2 to 2000
For IATI_row = 2 To 2000 Step 20

    If Cells(IATI_row, 1) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value Then

        If ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value > 4 Then

            'This part needs to be simplified
            If Cells(IATI_row, 49) = "" Then
                Cells(IATI_row, 43) = "3"
                Cells(IATI_row, 46) = "2013-12-31"
                Cells(IATI_row, 47) = "2013-12-31"
                Cells(IATI_row, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 1, 49) = "" Then
                Cells(IATI_row + 1, 43) = "3"
                Cells(IATI_row + 1, 46) = "2013-12-31"
                Cells(IATI_row + 1, 47) = "2013-12-31"
                Cells(IATI_row + 1, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 1, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 1, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 1, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 1, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 1, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 2, 49) = "" Then
                Cells(IATI_row + 2, 43) = "3"
                Cells(IATI_row + 2, 46) = "2013-12-31"
                Cells(IATI_row + 2, 47) = "2013-12-31"
                Cells(IATI_row + 2, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 2, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 2, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 2, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 2, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 2, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 3, 49) = "" Then
                Cells(IATI_row + 3, 43) = "3"
                Cells(IATI_row + 3, 46) = "2013-12-31"
                Cells(IATI_row + 3, 47) = "2013-12-31"
                Cells(IATI_row + 3, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 3, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 3, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 3, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 3, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 3, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 4, 49) = "" Then
                Cells(IATI_row + 4, 43) = "3"
                Cells(IATI_row + 4, 46) = "2013-12-31"
                Cells(IATI_row + 4, 47) = "2013-12-31"
                Cells(IATI_row + 4, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 4, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 4, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 4, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 4, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 4, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 5, 49) = "" Then
                Cells(IATI_row + 5, 43) = "3"
                Cells(IATI_row + 5, 46) = "2013-12-31"
                Cells(IATI_row + 5, 47) = "2013-12-31"
                Cells(IATI_row + 5, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 5, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 5, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 5, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 5, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 5, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 6, 49) = "" Then
                Cells(IATI_row + 6, 43) = "3"
                Cells(IATI_row + 6, 46) = "2013-12-31"
                Cells(IATI_row + 6, 47) = "2013-12-31"
                Cells(IATI_row + 6, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 6, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 6, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 6, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 6, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 6, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 7, 49) = "" Then
                Cells(IATI_row + 7, 43) = "3"
                Cells(IATI_row + 7, 46) = "2013-12-31"
                Cells(IATI_row + 7, 47) = "2013-12-31"
                Cells(IATI_row + 7, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 7, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 7, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 7, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 7, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 7, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 8, 49) = "" Then
                Cells(IATI_row + 8, 43) = "3"
                Cells(IATI_row + 8, 46) = "2013-12-31"
                Cells(IATI_row + 8, 47) = "2013-12-31"
                Cells(IATI_row + 8, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 8, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 8, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 8, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 8, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 8, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 9, 49) = "" Then
                Cells(IATI_row + 9, 43) = "3"
                Cells(IATI_row + 9, 46) = "2013-12-31"
                Cells(IATI_row + 9, 47) = "2013-12-31"
                Cells(IATI_row + 9, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 9, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 9, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 9, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 9, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 9, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 10, 49) = "" Then
                Cells(IATI_row + 10, 43) = "3"
                Cells(IATI_row + 10, 46) = "2013-12-31"
                Cells(IATI_row + 10, 47) = "2013-12-31"
                Cells(IATI_row + 10, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 10, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 10, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 10, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 10, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 10, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 11, 49) = "" Then
                Cells(IATI_row + 11, 43) = "3"
                Cells(IATI_row + 11, 46) = "2013-12-31"
                Cells(IATI_row + 11, 47) = "2013-12-31"
                Cells(IATI_row + 11, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 11, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 11, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 11, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 11, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 11, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 12, 49) = "" Then
                Cells(IATI_row + 12, 43) = "3"
                Cells(IATI_row + 12, 46) = "2013-12-31"
                Cells(IATI_row + 12, 47) = "2013-12-31"
                Cells(IATI_row + 12, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 12, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 12, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 12, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 12, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 12, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 13, 49) = "" Then
                Cells(IATI_row + 13, 43) = "3"
                Cells(IATI_row + 13, 46) = "2013-12-31"
                Cells(IATI_row + 13, 47) = "2013-12-31"
                Cells(IATI_row + 13, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 13, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 13, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 13, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 13, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 13, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 14, 49) = "" Then
                Cells(IATI_row + 14, 43) = "3"
                Cells(IATI_row + 14, 46) = "2013-12-31"
                Cells(IATI_row + 14, 47) = "2013-12-31"
                Cells(IATI_row + 14, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 14, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 14, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 14, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 14, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 14, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 15, 49) = "" Then
                Cells(IATI_row + 15, 43) = "3"
                Cells(IATI_row + 15, 46) = "2013-12-31"
                Cells(IATI_row + 15, 47) = "2013-12-31"
                Cells(IATI_row + 15, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 15, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 15, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 15, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 15, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 15, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 16, 49) = "" Then
                Cells(IATI_row + 16, 43) = "3"
                Cells(IATI_row + 16, 46) = "2013-12-31"
                Cells(IATI_row + 16, 47) = "2013-12-31"
                Cells(IATI_row + 16, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 16, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 16, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 16, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 16, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 16, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            ElseIf Cells(IATI_row + 17, 49) = "" Then
                Cells(IATI_row + 17, 43) = "3"
                Cells(IATI_row + 17, 46) = "2013-12-31"
                Cells(IATI_row + 17, 47) = "2013-12-31"
                Cells(IATI_row + 17, 48) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 7).Value
                Cells(IATI_row + 17, 49) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 11).Value
                Cells(IATI_row + 17, 52) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 4).Value
                Cells(IATI_row + 17, 53) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 5).Value
                Cells(IATI_row + 17, 55) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 1).Value
                Cells(IATI_row + 17, 56) = ThisWorkbook.Worksheets("PGTS").Cells(PGTS_row, 3).Value
            End If
        End If
    End If
Next IATI_row
Next PGTS_row
End Sub

You could use something like this, in a function or not, depending on it's frequency of use. 您可以根据功能的使用频率在功能中使用或不使用此类功能。 Hope it helps 希望能帮助到你

Public Function GET_LAST_ROW(strSheetName As String, strColumnName As String) As Long
    Dim ws As Worksheet
    Set ws = Worksheets(strSheetName)
    GET_LAST_ROW = ws.Range(strColumnName & ws.Rows.Count).End(xlUp).Row
End Function

If I got you right, you have an identifier like "keyword xyz" which covers a 20 row range. 如果我说对了,您会发现一个标识符,例如“ keyword xyz”,它覆盖20行范围。 To get the first cell you could simply use: 要获得第一个单元格,您可以简单地使用:

Application.Match(keywordVariable, Sheets("Master").Range("A:A"), 0)

This will return the first row (as long) which holds the keyword. 这将返回包含关键字的第一行(只要很长)。 If you a signet it to a variable like firstRow = ... the you can check for the first empty cell in column B like this: 如果将其签名给诸如firstRow = ...之类的变量,则可以像这样检查B列中的第一个空单元格:

For i = FirstRow to firstRow + 19
  If Len(Sheets("Master").Range("B" & i).Value) = 0 Then Exit For
Next

Now i will be the first row with the keyword which is empty... 现在,我将是关键字为空的第一行...

But to directly adopt it to your code you also could use: 但是要将其直接采用到您的代码中,您还可以使用:

i = 0
While i < 20 And Len(Cells(IATI_row + i, 49)) > 0
  i = i + 1
Wend

Then use it with Cells(IATI_row + i, **) or run another For 然后用使用它Cells(IATI_row + i, **)或运行另一个For

For i = IATI_row to IATI_row + 19
  If Cells(IATI_row + i, 49) = "" Then Exit For
Next

In this case use Cells(i, **) as ref :) 在这种情况下,请使用Cells(i, **)作为参考:)

With regard to the last rows, there are certain columns on each worksheet that are important to primary criteria. 关于最后一行,每个工作表上都有某些列对于主要条件很重要。 There is no need to go past the last populated value in either of these since the primary criteria will never match. 由于主要条件永远不会匹配,因此无需超过这两个值中的最后一个填充值。

Sub Import_IATI_data()
    Dim PGTS_row As Long, IATI_row As Long, brw As Long, vPGTS_A As Variant, vPGTSs As Variant

    ReDim vPGTSs(1 To 3, 1 To 1)

    With ThisWorkbook.Worksheets("PGTS")
        For PGTS_row = 2 To .Cells(Rows.Count, 11).End(xlUp).Row
            If .Cells(PGTS_row, 11).Value > 4 Then
                vPGTS_A = .Cells(PGTS_row, 1).Value2
                Erase vPGTSs
                vPGTSs(1, 1) = Array(.Cells(PGTS_row, 7).Value2, .Cells(PGTS_row, 11).Value2)
                vPGTSs(2, 1) = Array(.Cells(PGTS_row, 4).Value2, .Cells(PGTS_row, 5).Value2)
                vPGTSs(2, 1) = Array(.Cells(PGTS_row, 1).Value2, .Cells(PGTS_row, 3).Value2)

                With ThisWorkbook.Worksheets("IATI")
                    For IATI_row = 2 To .Cells(Rows.Count, 1).End(xlUp).Row Step 20

                        If Cells(IATI_row, 1) = vPGTS_A Then
                            If CBool(Application.CountBlank(.Cells(IATI_row, 49).Resize(18, 1))) Then
                                brw = .Cells(IATI_row, 49).Resize(18, 1).SpecialCells(xlCellTypeBlanks)(1).Row
                                .Cells(brw, 43) = 3
                                .Cells(brw, 46).Resize(1, 2) = DateSerial(2013, 12, 31)
                                .Cells(brw, 48).Resize(1, 2) = vPGTSs(1, 1)
                                .Cells(brw, 52).Resize(1, 2) = vPGTSs(2, 1)
                                .Cells(brw, 55).Resize(1, 2) = vPGTSs(3, 1)

                                'SHOULD YOU USE EXIT FOR TO GET OUT OF THE LOOP HERE?
                                'YOU'VE MET THE CONDITION. SHOULD YOU EXIT OR TRY FOR ANOTHER?
                            End If
                        End If

                    Next IATI_row

                End With
            End If
        Next PGTS_row
    End With
End Sub

Explicitly providing parent worksheet references (in this case though With ... End With statement s) is always a good thing. 明确提供父级工作表引用(在本例中为With ... End With语句 s)始终是一件好事。 The ActiveSheet property is just not reliable. ActiveSheet属性只是不可靠。

The Range.SpecialCells method can find all of the truly blank cells within the next 18 rows. Range.SpecialCells方法可以在接下来的18行中找到所有真正的空白单元格。 The first (eg (1) ) of these would be the first blank row. 其中的第一个(例如(1) )将是第一个空白行。

As noted in my comments, I'm not sure if you should be exiting the loops at any point. 如我的评论中所述,我不确定您是否应该随时退出循环。

I would suggest stepping through this proposed modifications with [F8] first. 我建议首先使用[F8]逐步完成建议的修改。 While it compiles, I have no sample data to test on. 在编译时,我没有要测试的示例数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当后续行不为空时如何在Excel中查找第一个空行 - How to find the first empty row in Excel when subsequent rows are not empty 如何查找一行中第一个空单元格的列名? - How to find the column name of first empty cell in a row? 查找同一列/行中的第一个空单元格 - Find the first empty cell in the same column/row 如何返回一系列公式中的第一个非空白单元格? - How can I return the first non-blank cell in a range of formulas? 在行中查找一个单元格,然后使用该单元格引用在该单元格引用范围内查找垂直单元格 - Finding a cell in row, then using that cell reference to find vertical cell within range of cell reference 如何在一个范围内找到MAX()的电子表格单元格引用? - How can I find the spreadsheet cell reference of MAX() in a range? xlwings:如何引用单元格并将该单元格中的值用作行范围值 - xlwings: how to reference a cell and use the value in that cell as a row range value 如何在 F 列的第一个空单元格中放置公式? - How can I place a formula in the first empty cell on Column F? 使用VBA在每三行中查找包含空单元格的第一列 - Find first column with empty cell in every third row using VBA 如何用某个范围内的公式填充一列 - How can I fill a column with formulas from a range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM