简体   繁体   中英

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. 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). 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.

As you can see below, I am currently using an incredibly convoluted IF function to get this done. 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. 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?

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. 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:

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

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 :)

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. The ActiveSheet property is just not reliable.

The Range.SpecialCells method can find all of the truly blank cells within the next 18 rows. The first (eg (1) ) of these would be the first blank row.

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. While it compiles, I have no sample data to test on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM