简体   繁体   中英

VBA Outlook to excel via userform

I'm having some trouble with a macro that should move from an Outlook userform to Excel. For some reason the evaluate part is not giving the right answer, any ideas?

 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 

Without looking at the actual formula:

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

You need to have Excel run the Evaluate , but you should use the Worksheet form and not the Application form, otherwise there's a chance the formula will be evaluated in the context of a different worksheet from the one you're working with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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