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

时间:2015-01-30 13:50:43

标签: excel vba loops excel-vba

这个想法就是这个...... 如果A列(工作簿"来源")中的值等于Q列中的值(工作簿"受影响")则相应列F中的值(工作簿"从列T中减去源")(工作簿"受影响")。但它不起作用。我没有收到任何类型的错误,它只是没有做我想要的。我感谢任何帮助。

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

3 个答案:

答案 0 :(得分:1)

您已在代码中声明sourceaffected作为工作簿。您需要使用Set关键字为对象创建赋值语句,例如:

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

如果您的代码行为不符合预期,那么使用调试器总是好的,并逐行检查代码。

答案 1 :(得分:1)

在@Jeanno回答之上,请检查你的第一个for循环变量。

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

您可能需要用z替换i?因为我当时没有定义。

答案 2 :(得分:0)

只是想让你们都知道我让它上班了。我粘贴了下面的代码。没有故障排除帮助,我不会想出来的。我非常感激!

Sub Subtract_Between_workbooks() '=== Para menos Resgate ===等待堆栈溢出     昏暗的来源作为工作簿     昏暗受影响的工作簿

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

End Sub