我目前正在研究使用VBA将变量从不同的TAB导入到MASTER表(在一个Excel文件中)的代码。我想将这些选项卡中的信息插入主工作表中列的第一个空单元格中,在特定的行范围内。
让我更具体一点: 每20行获得相同的标识符(MASTER表),接下来的20行获取第二个标识符(不是附加代码的一部分)。如果TAB标识符与MASTER标识符相同,我希望将TAB行中的信息导入到具有正确标识符的第一个空MASTER行中。
正如您在下面所看到的,我目前正在使用令人难以置信的错综复杂的IF功能来完成这项工作。它工作正常,但由于我从许多选项卡导入大量信息,我的代码不适合1模块,我不得不创建13个独立的模块。更重要的是,如果我想在其中一个工作表中添加一个列,我必须经历大量的手工操作(耗时且容易出错)以适应代码。
我的问题:有没有办法简化附加的代码?
我可以想象有IFLOOP或ELSEIFLOOP,或者有一种聪明的方法可以完全避免IF功能,并找到20行范围内的第一个空单元?
非常感谢!
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
答案 0 :(得分:0)
根据它的使用频率,您可以在功能中使用这样的功能。希望它有所帮助
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
答案 1 :(得分:0)
如果我找对你,你就会有一个像“关键字xyz”这样的标识符,它涵盖20行范围。要获得第一个单元格,您可以使用:
Application.Match(keywordVariable, Sheets("Master").Range("A:A"), 0)
这将返回包含关键字的第一行(长)。如果您将signet转换为firstRow = ...
之类的变量,则可以检查B列中的第一个空单元格,如下所示:
For i = FirstRow to firstRow + 19
If Len(Sheets("Master").Range("B" & i).Value) = 0 Then Exit For
Next
现在我将成为关键字为空的第一行......
但要直接将其用于您的代码,您也可以使用:
i = 0
While i < 20 And Len(Cells(IATI_row + i, 49)) > 0
i = i + 1
Wend
然后将其与Cells(IATI_row + i, **)
一起使用或运行另一个For
For i = IATI_row to IATI_row + 19
If Cells(IATI_row + i, 49) = "" Then Exit For
Next
在这种情况下,使用Cells(i, **)
作为ref:)
答案 2 :(得分:0)
关于最后一行,每个工作表上的某些列对主要条件很重要。由于主要标准永远不会匹配,因此无需超过其中任何一个中的最后一个填充值。
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
明确提供父工作表引用(在本例中为With ... End With statement s)总是一件好事。 ActiveSheet property只是不可靠。
Range.SpecialCells method可以在接下来的18行中找到所有真正空白的单元格。这些中的第一个(例如(1)
)将是第一个空白行。
正如我的评论所述,我不确定你是否应该在任何时候退出循环。
我建议先用[F8]逐步完成这些修改后的修改。在编译时,我没有要测试的样本数据。