我有2本工作簿。一个wb充当数据库(分为用户),而另一个用于根据日期从该wb中提取数据。我循环宏,起初它似乎只从第一张表中提取数据。 请参阅下面的注释
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
'MsgBox wbLog.Sheets(I).Name
Next I
编辑:我想我可能会有更多有用的信息。似乎宏不会跳过。当我尝试向数据库文件添加更多条目时,它会提取数据,但它会从上一页上结束的行开始。例如,如果它在sh1上的第9行结束,则代码从sh2上的第10行开始。
如何在转到下一张纸之前重置增量?
答案 0 :(得分:0)
这是编辑后的代码。我选择在循环结束后添加它,因为这样我就会记住它以供将来参考。谢谢@PatricK!
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
y = 3 'to reset increment before moving on to next sheet
Next I