VBA 检查输入框值来源

问题描述 投票:0回答:1

我有一段代码要求用户输入要在我的工作表上写入的文本,但是,当用户键入纯文本(只需键入所需的文本)或用户引用工作表单元格来获取时,我想执行单独的操作它的值作为输入。

我能做什么?

Dim mensagem As Variant
mensagem = Application.InputBox("Qual o motivo da pendência?", "O protocolo geral precisa saber...")
If Not (mensagem = False Or mensagem = vbNullString) Then
    If xxxx Then '(1) reference cell
        ActiveCell.EntireRow.Columns(17).Value = mensagem
        'do stuff
    ElseIf yyyyyy Then '(2) plain text
        ActiveCell.EntireRow.Columns(17).Value = mensagem
        ' do different stuff
    Else
        Exit Sub
    End If
End If
ActiveCell.Offset(1, 0).Select

_______

excel vba inputbox
1个回答
0
投票

你可以尝试这样的事情:

Sub Tester()
    
    Dim mensagem As Variant, v
    mensagem = Application.InputBox("Qual o motivo da pendência?", "O protocolo geral precisa saber...")
    If mensagem = False Or mensagem = vbNullString Then Exit Sub
    
    v = ActiveSheet.Evaluate(Trim(mensagem)) 'try to evaluate the entry
    With ActiveCell
        'if was evaluated, use the result, else use the original entry
        .EntireRow.Columns(17).Value = IIf(IsError(v), mensagem, v)
        .Offset(1, 0).Select
    End With
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.