VBAoutlook通过用户表单优秀

我遇到了一些问题,应该从Outlook用户窗体移到Excel。 出于某种原因,评估部分没有给出正确的答案,有什么想法?

Private Sub btnOK_Click() Dim ctrtype As String Dim client As String Dim terminal As String Dim ctr As String Dim week As String Dim vMatch As Variant Dim sformula As String Dim xlApp As Object, xlBook As Object ctrtype = ComboBox1.Value client = ComboBox2.Value ctr = TextBox1.Value terminal = ComboBox3.Value week = ComboBox4.Value Set xlApp = GetObject(, "Excel.Application") If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application") End If Set xlBook = xlApp.Workbooks.Open("C:\Users\kkovask\Desktop\RsA2015.xlsx") xlBook.Sheets(client).Activate sformula = "MATCH(1,(A:A=" & week & ")*(B:B=" & ctrtype & ")*(C:C=""""),0)" vMatch = Evaluate(sformula) If IsNumeric(vMatch) Then MsgBox Range("C" & vMatch).Address Else: MsgBox "Customer has reached their limit" xlBook.Close SaveChanges:=True Set xlBook = Nothing Set xlApp = Nothing End Sub 

不用看实际的公式:

  vMatch = xlBook.Sheets(client).Evaluate(sformula) 

您需要让Excel运行“ Evaluate ,但是您应该使用“ Worksheet表单而不是“ Application表单,否则有可能会在与您正在使用的工作表不同的工作表的上下文中评估公式。