根据另一个工作簿中的单个条件查找一个工作簿中的值的总和

时间:2018-04-19 15:11:18

标签: excel vba excel-vba

我一直在编写一个使用3个工作簿的代码 - 但是我遇到了最终输出的问题。

  • 工作簿1(wb1 - 此工作簿 - 运行宏的位置 - 将显示最终代码)
  • 工作簿2(wb2),它是产品订单的客户数据库
  • 工作簿3(wb3),它是权重的参考文件(在工作簿2中进行操作)

wb1打开wb2和wb3,交叉引用(使用VLOOKUP)wb3中的权重,将它们复制到wb2中的相应客户地址,然后将权重乘以wb2地址行中的数量。

整个代码按照我的计划工作,但最终输出除外。 wb2现在在Q栏中有最终权重。

  • 剩下的就是wb1(K列)中的“PO编号”来查找wb2中的多个“PO编号”(C列也是如此)
  • 总和wb2的权重(列Q),其中有匹配
  • 将这笔钱还给wb1。我已经尝试了sumif,但无济于事。

这是最终输出代码(目前不返回任何值),下面张贴了整个代码供参考。

'Enter in the weights data into the final sheet
tempCount = 0
lastCount = lastRow1

For tempCount = 1 To lastCount
    Set lookFor = wb1.Sheets(1).Cells(tempCount + 1, 11) ' value to find
    Set lookForRange = wb2.Sheets(1).Range("$C$2:$C$" & lastRow2)   'Range of values to lookup
    Set srchRange = wb2.Sheets(1).Range("$Q$7:$Q$" & lastRow2) 

wb1.Sheets(1).Activate
    ActiveSheet.Cells(tempCount + 1, 12).Value = Application.WorksheetFuction.SumIf(lookForRange, lookFor, srchRange)
        On Error Resume Next

Next

以下是整个代码供参考。

'Define workbooks
Dim wb2FileName As Variant
Dim wb3FileName As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb3 As Workbook

'Count last rows in columns
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim lastRow3 As Long

'Variables
Dim lookFor As Range
Dim lookForRange As Range
Dim srchRange As Range
Dim tempCount As Integer
Dim lastCount As Integer


'Open up all workbooks to work on
 Set wb1 = ThisWorkbook

wb2FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Customer Order Data Worksheet", MultiSelect:=False)
If wb2FileName <> False Then
    Workbooks.Open Filename:=wb2FileName
End If
    Set wb2 = Workbooks.Open(wb2FileName)

wb3FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Source Reference File (Weights)", MultiSelect:=False)
If wb3FileName <> False Then
    Workbooks.Open Filename:=wb3FileName
End If
    Set wb3 = Workbooks.Open(wb3FileName)

'Find the last row in the customer data workbook and the source weights workbook
wb2.Sheets(1).Activate
    lastRow2 = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row

wb3.Sheets(1).Activate
    lastRow3 = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row

'Use VLOOKUP to enter in weights from the reference sheet into the customer order data sheet, then multiply by the quantity
tempCount = 0
lastCount = lastRow2

For tempCount = 1 To lastCount
    Set lookFor = wb2.Sheets(1).Cells(tempCount + 6, 10) ' value to find
    Set srchRange = wb3.Sheets(1).Range("$B$2:$C$" & lastRow3)    'source

    wb2.Sheets(1).Activate
        ActiveSheet.Cells(tempCount + 6, 16).Value = Application.WorksheetFunction.VLookup(lookFor, srchRange, 2, False)
        ActiveSheet.Cells(tempCount + 6, 17).Value = ActiveSheet.Cells(tempCount + 6, 11).Value * ActiveSheet.Cells(tempCount + 6, 16).Value
            On Error Resume Next
Next

'Delete top 5 rows from the final sheet and insert new header
wb1.Sheets(1).Activate
    ActiveSheet.Rows("1:5").Delete
    ActiveSheet.Cells(1, 12).Value = "Weights"

'Find the last row on the final sheet
lastRow1 = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

'Enter in the weights data into the final sheet
tempCount = 0
lastCount = lastRow1

For tempCount = 1 To lastCount
    Set lookFor = wb1.Sheets(1).Cells(tempCount + 1, 11) ' value to find
    Set lookForRange = wb2.Sheets(1).Range("$C$2:$C$" & lastRow2)   'Range of values to lookup
    Set srchRange = wb2.Sheets(1).Range("$Q$7:$Q$" & lastRow2) 


    wb1.Sheets(1).Activate
         ActiveSheet.Cells(tempCount + 1, 12).Value = Application.WorksheetFuction.SumIf(lookForRange, lookFor, srchRange)


Next

1 个答案:

答案 0 :(得分:1)

好的,我对你的代码做了一些补充/修改,所以请耐心等待。

  1. 我在您的模块顶部添加了Option Explicit(您可能已经拥有它但是您没有包含Sub/End Sub,所以我们无法告诉您。 / p>

  2. 摆脱Activate&amp; ActiveSheet。这只会导致大量可能的错误和可读性的损失。改为使用显式引用。

  3. 如果Exit Subwb2之一返回wb3,您需要False。如果他们这样做,他们就会抛出错误。现在您将获得MsgBox,子程序将正确退出。

  4. 摆脱On Error Resume Next。你不应该在这里需要它。如果您 使用它,请至少在不久之后使用On Error GoTo 0重新启用错误。

  5. 在相应的Set语句中移动了一些If s,并在循环外移动了几个静态Set(如果它总是相同的话,为什么把它放在循环中?)。

  6. 现在,对于SumIf的问题 - 我相信您遇到此问题,因为您的条件范围和总和范围相同的大小。如果他们不是,你可以得到0,因为他们没有正确排队。我已将Range("$Q$7:$Q$" & lastRow2)更改为Range("$Q$2:$Q$" & lastRow2),希望能够解决问题(但如果这是您的预期范围,则可能需要将Range("$C$2:$C$" & lastRow2)更改为Range("$C$7:$C$" & lastRow2)

    希望这有帮助!

    Option Explicit
    Sub Test()
    'Define workbooks
    Dim wb2FileName As Variant, wb3FileName As Variant
    Dim wb1 As Workbook, wb2 As Workbook, wb3 As Workbook
    
    'Count last rows in columns
    Dim lastRow1 As Long, lastRow2 As Long, lastRow3 As Long
    
    'Variables
    Dim lookFor As Range, lookForRange As Range, srchRange As Range
    Dim tempCount As Integer, lastCount As Integer
    
    'Open up all workbooks to work on
    Set wb1 = ThisWorkbook
    
    wb2FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Customer Order Data Worksheet", MultiSelect:=False)
    
    If wb2FileName <> False Then
        Set wb2 = Workbooks.Open(wb2FileName)
    Else
        MsgBox "No wb2, exiting"
        Exit Sub
    End If
    
    wb3FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Source Reference File (Weights)", MultiSelect:=False)
    
    If wb3FileName <> False Then
        Set wb3 = Workbooks.Open(wb3FileName)
    Else
        MsgBox "No wb3, exiting"
        Exit Sub
    End If
    
    'Find the last row in the customer data workbook and the source weights workbook
    lastRow2 = wb2.Sheets(1).Cells(Rows.Count, 3).End(xlUp).Row
    lastRow3 = wb3.Sheets(1).Cells(Rows.Count, 3).End(xlUp).Row
    
    'Use VLOOKUP to enter in weights from the reference sheet into the customer order data sheet, then multiply by the quantity
    lastCount = lastRow2
    
    For tempCount = 1 To lastCount
        Set lookFor = wb2.Sheets(1).Cells(tempCount + 6, 10) ' value to find
        Set srchRange = wb3.Sheets(1).Range("$B$2:$C$" & lastRow3)    'source
    
        wb2.Sheets(1).Cells(tempCount + 6, 16).Value = Application.WorksheetFunction.VLookup(lookFor, srchRange, 2, False)
        wb2.Sheets(1).Cells(tempCount + 6, 17).Value = wb2.Sheets(1).Cells(tempCount + 6, 11).Value * wb2.Sheets(1).Cells(tempCount + 6, 16).Value
    Next
    
    'Delete top 5 rows from the final sheet and insert new header
    wb1.Sheets(1).Rows("1:5").Delete
    wb1.Sheets(1).Cells(1, 12).Value = "Weights"
    
    'Find the last row on the final sheet
    lastRow1 = wb1.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
    
    'Enter in the weights data into the final sheet
    lastCount = lastRow1
    
    Set lookForRange = wb2.Sheets(1).Range("$C$2:$C$" & lastRow2)   'Range of values to lookup
    Set srchRange = wb2.Sheets(1).Range("$Q$2:$Q$" & lastRow2)
    
    For tempCount = 1 To lastCount
    
        Set lookFor = wb1.Sheets(1).Cells(tempCount + 1, 11) ' value to find
    
        wb1.Sheets(1).Cells(tempCount + 1, 12).Value = Application.WorksheetFuction.SumIf(lookForRange, lookFor, srchRange)
    
    Next
    
    End Sub