循环从单独的工作簿中的一个工作簿减去值

这个想法是这样的…如果列A中的值(工作簿“源”)等于列Q中的值(工作簿“受影响”),那么将相应列F(工作簿“源”)中的值从列T(工作簿“受影响”)。 但它不工作。 我没有收到任何types的错误,只是不做我想要的。 我感谢任何帮助。

Sub Subtract_Between_workbooks() 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 Source = Workbooks.Open("\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\LCA_LCI Macro Writing\ResgatesEmissões.xlsb") Affected = Workbooks.Open("\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\LCA_LCI Macro Writing\New - Macro Writing - Controle de Lastro LCA.xlsm") Set Dados = Workbooks(Affected).Sheets("Dados") Set Source_Sheet = Workbooks(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" & i) = "LCA" And Source_Sheet.Range("D" & i) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & i) = "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 End Sub 

您在代码中声明了source并将其作为工作簿进行了affected 。 您需要使用Set关键字为对象创build赋值语句,例如:

 Set Source = Workbooks.Open("\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\LCA_LCI Macro Writing\ResgatesEmissões.xlsb") 

如果您的代码不像预期的那样运行,那么使用debugging器总是很好,并且逐行检查代码。

在@Jeanno回答的顶部,请检查您的第一个循环variables。

 For Z = 1 To LastRow If Source_Sheet.Range("B" & i) = "LCA" And Source_Sheet.Range("D" & i) = "Resgate Final Passivo Cliente" And Source_Sheet.Range("H" & i) = "9007230" Then 

你可能需要用zreplace我? 因为我在那个时候没有定义。

只是想让你们都知道我是这样工作的。 我粘贴下面的代码。 如果没有故障排除帮助,我不会想出来的。 我很感激!

Sub Subtract_Between_workbooks()'=== Para menos Resgate ===等待堆栈溢出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 

结束小组