【问题标题】:Excel VBA: is it possible to create a macro that types a character into a cell and lets the user continue typing?Excel VBA:是否可以创建一个在单元格中输入字符并让用户继续输入的宏?
【发布时间】:2021-07-18 00:43:34
【问题描述】:

我想编写一个宏,当通过按钮调用它时,将一个字符插入一个单元格并将焦点保持在该单元格上,让用户输入。例如,按下按钮后,在单元格 A1 中输入字母“A”,用户可以直接在“A”之后键入。当用户按下 enter 时,单元格存储“A”加上用户写的任何内容。

编辑:

就我而言,我正在尝试 Gary 的 Student 方法。我实际上想把符号“=”让用户更容易输入算术表达式。我编写了这个有效的函数,但我想清除单元格,以防用户按下按钮触发该函数,但只需按下回车键而不在“=”右侧输入任何内容。我的这部分功能什么都不做。你们能帮帮我吗?

Sub UserFormula()

CurrentCell = ActiveCell.Address
CurrentCellValue = ActiveCell.Value

If CurrentCellValue = "=" Or Not IsNumeric(CurrentCellValue) Then CurrentCellValue = ""

ActiveSheet.Range(CurrentCell).Value = "=" & CurrentCellValue
ActiveSheet.Range(CurrentCell).Select
Application.SendKeys "{F2}"

'THIS DOESN'T WORK:
If ActiveCell.Address <> CurrentCell Then    'user pressed enter and moved to next cell
     
    CurrentCellValue = ActiveSheet.Range(CurrentCell).Value 'read again the value
    
    If CurrentCellValue = "=" Or Not IsNumeric(CurrentCellValue) Then
        CurrentCellValue = ""
        ActiveSheet.Range(CurrentCell).Value = CurrentCellValue
    End If
End If

结束子

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这会将 A 放置在单元格 A1 中,并在 A 之后使用光标离开编辑状态强>

    Sub PushMe()
        Range("A1").Value = "A"
        Range("A1").Select
        Application.SendKeys "{F2}"
    End Sub
    

    【讨论】:

    • 就我而言,我正在尝试这种方法。我其实是想把符号“=”让用户更容易输入算术表达式。
    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2011-11-13
    • 2018-06-28
    • 1970-01-01
    • 2017-09-06
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多