目标是让循环通过" Source"工作簿。满足以下条件的地方
If Source_Sheet.Range("B" & Z) = "LCA" And Source_Sheet.Range("D" & Z) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & Z) = "9007230"
然后它将触发下一个if / then语句(在满足第一个if / then的那些语句中),这将比较Workbook(source).Sheet1中的A列和Workbook(受影响的).Dados中的Q列。如果它们相同,那么它将从工作簿(受影响的).Dados中的T列中减去相应F列单元格(在Source中)的值。
我已粘贴下面的整个代码。目前正在发生的是它从T列中正确地减去了值。但是在减去一次之后它并没有停止。它减去EIGHT次,然后停止。
Dim Source As Workbook
Dim Affected As Workbook
Dim Dados As Worksheet
Dim Source_Sheet As Worksheet
Dim LastRow As Long
Dim i As Long
Dim j As Long
Dim v As Variant
Dim N As Long
Dim M As Long
Dim FinalRow As Long
Set Source = Workbooks("ResgatesEmissões.xlsb")
Set Affected = Workbooks.Open("\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\LCA_LCI Macro Writing\New - Macro Writing - Controle de Lastro LCA.xlsm")
Set Dados = Affected.Sheets("Dados")
Set Source_Sheet = Source.Sheets("Sheet1")
LastRow = Source_Sheet.Cells(Rows.Count, "A").End(xlUp).Row
N = Dados.Cells(Rows.Count, "Q").End(xlUp).Row
M = Source_Sheet.Cells(Rows.Count, "A").End(xlUp).Row
For Z = 1 To LastRow
If Source_Sheet.Range("B" & Z) = "LCA" And Source_Sheet.Range("D" & Z) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & Z) = "9007230" Then
For i = 1 To M
v1 = Source_Sheet.Cells(i, "A").Value
v2 = Source_Sheet.Cells(i, "F").Value
For j = 1 To N
If v1 = Dados.Cells(j, "Q").Value Then
Dados.Cells(j, "T").Value = Dados.Cells(j, "T").Value - v2
Exit For
End If
Next j
Next i
Else: End If
Next Z
答案 0 :(得分:1)
想出来。它只是继续保持循环。新循环看起来像这样,效果很好。
For Z = 1 To LastRow
If Source_Sheet.Range("B" & Z) = "LCA" And Source_Sheet.Range("D" & Z) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & Z) = "9007230" Then
v1 = Source_Sheet.Cells(Z, "A").Value
v2 = Source_Sheet.Cells(Z, "F").Value
For j = 1 To N
If v1 = Dados.Cells(j, "Q").Value Then
Dados.Cells(j, "T").Value = Dados.Cells(j, "T").Value - v2
Exit For
End If
Next j
Else: End If
Next Z
答案 1 :(得分:0)
如果你想只对一个满足条件的单元格执行此操作(根据上面的注释,则需要退出ALL For ... Next循环,而不仅仅是内部循环:
Dim bStop As Boolean
For Z = 1 To LastRow
bStop = False
If Source_Sheet.Range("B" & Z) = "LCA" And Source_Sheet.Range("D" & Z) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & Z) = "9007230" Then
For i = 1 To M
v1 = Source_Sheet.Cells(i, "A").Value
v2 = Source_Sheet.Cells(i, "F").Value
For j = 1 To N
If v1 = Dados.Cells(j, "Q").Value Then
Dados.Cells(j, "T").Value = Dados.Cells(j, "T").Value - v2
bStop = True
Exit For
End If
Next j
If bStop Then Exit For
Next i
If bStop Then Exit For
End If
Next Z