简体   繁体   English

如何在VBA中使用If-Then有条件跨工作簿?

[英]How to use If-Then Conditional Across Workbooks in VBA?

I want to use if-then statement across two workbooks. 我想在两个工作簿中使用if-then语句。

I've defined x as Long but x does not appear to hold the value of the sum of all cells in column B 我已将x定义为Long,但是x似乎不保存B列中所有单元格的和值

But the code looks right, any thoughts? 但是代码看起来正确,有什么想法吗?

Sub mycode()


       Dim x As Long


       myRange = Workbooks("Book2").Sheets("Sheet1").Range("B1", _ 
       Range("B1").End(xlDown))


       x = WorksheetFunction.Sum(myRange)    '<<does not seem to hold value



      If Workbooks("Book1").Sheets("Sheet1").Range("A1").Value = x Then

         MsgBox ("values equal")

      Else

         MsgBox ("please review values")

      End If



End Sub

As an example: 举个例子:

Sub MyCode()
    Dim myRange As Range
    Set myRange = Workbooks("Book2").Sheets("Sheet1").Columns(2)

    If Workbooks("Book1").Sheets("Sheet1").Range("A1").Value = WorksheetFunction.Sum(myRange) Then
        MsgBox "Values equal"
    Else
        MsgBox "Please review values"
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM