首页 > 解决方案 > 如何在 VBA excel 中的 If 语句中使用组合框值?

问题描述

在此处输入图像描述

我需要在 Excel 中更新 VBA 上现有的票号

但是,我多次尝试将数据从工作表带到票号的组合框,从“A2”到 lastRow 添加,但它告诉我它的语法错误。

如何取 ComboBox 的值?如何将其插入到 IF 语句中?请帮我做以下条件

  1. 门票表中获取门票号码
  2. 将combBox Close byTicket Status中的更新值写入两个Ticket Sheet,如果Ticket 状态为Solved 或Closed,则将它们从PendingTickets表中删除

测试工单状态是否为待处理或进行中的代码

Dim Ws As Worksheet
Dim lastRow As Long
Dim openOn As Date


'Declare openBy to date Now function'
openOn = Now()
'set format function on time var'
openTimeAmPM = Format(openOn, "m.d.yy h:mm AM/PM")


 'copy ticket data to Pending tickst sheet if the ticket status is Pending or On progress'
  '  If SieraForum.CombTicketStatus.Value = "Pending" Or "On Progress" Then
        Sheets("PendingTickets").Cells(lastRow + 1, 1).Value = lastRow
        Sheets("PendingTickets").Cells(lastRow + 1, 2).Value = SieraForum.txtTicketName.Value
        
        ElseIf SieraForum.ErrorOption = True Then
         Sheets("PendingTickets").Cells(lastRow + 1, 3).Value = "Error"
        ElseIf SieraForum.OrderOption = True Then
         Sheets("PendingTickets").Cells(lastRow + 1, 3).Value = "Order"
        
            Sheets("PendingTickets").Cells(lastRow + 1, 4).Value = SieraForum.CombSeverity.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 5).Value = SieraForum.CombLocation.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 6).Value = SieraForum.txtTicketDetails.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 7).Value = SieraForum.CombOpenBy.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 8).Value = SieraForum.CombCloseBy.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 9).Value = SieraForum.CombTicketStatus.Value
            Sheets("PendingTickets").Cells(lastRow + 1, 10).Value = openTimeAmPM
    
    Else
         MsgBox "The Ticket is Added Successfully"
    End If
         
    

点击更新票证上的保存按钮代码

    Private Sub btnSave2_Click()
 
    'declare close on date function'
     Dim closeOn As Date
     
     ' set format function on time var'
     closeOn = Now()
     closeTimeAmPM = Format(openOn, "m.d.yy h:mm AM/PM")
    
     
    'To enter new line'
    lastRow = WorksheetFunction.CountA(Sheets("Tickets").Range("A:A"))
    Sheets("Tickets").Cells(lastRow + 1, 1).Value = lastRow
     
    Sheets("Tickets").Cells(lastRow + 1, 8).Value = SieraForum.CombCloseBy.Value
    Sheets("Tickets").Cells(lastRow + 1, 9).Value = SieraForum.CombTicketStatus2.Value
    'write the update statement'
    'Sheets("Tickets").Cells(lastRow + 1, 12).Value = "Update Statement > " + SieraForum.txtTicketUpdate.Value
    Sheets("Tickets").Cells(lastRow + 1, 11).Value = closeTimeAmPM
            
    'Clear the data from the form'
    SieraForum.CombTicketNum.Value = ""
    SieraForum.CombCloseBy = ""
    SieraForum.CombTicketStatus2 = ""
    SieraForum.txtTicketUpdate.Value = ""

End Sub

标签: excelvbaif-statement

解决方案


第一个If被注释掉。语法错误是由有和ElseIf没有匹配引起的。ElseEnd IfIf


推荐阅读