vba 如何从单元格创建一个弹出式输入框并将值调整到下一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22861953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:39:52  来源:igfitidea点击:

How can I Create A popup InputBox from a cell and right the value to the next one?

excelvbaexcel-vba

提问by Xaratsarhs

Hello this is my first question here and I'm new in VBA programming. I'm looking for a VBA code that would allows me when I select a cell on a range of cells to pop-up an input box to put some values. For example when I select a cell from column B an input box pops up, requiring a value that goes to next cell in column C. That's because in the column B I have stored a formula (& the cell is locked) and I would not like to be deleted or changed by the user although I need his Input Value to be calculated by the formula, so I choose to store this value in a hidden cell next to it and make the reference into my formula. How is this possible with VBA? Thanks in advance, Harris

您好,这是我在这里的第一个问题,我是 VBA 编程的新手。我正在寻找一个 VBA 代码,当我在一系列单元格上选择一个单元格时,它允许我弹出一个输入框来输入一些值。例如,当我从 B 列中选择一个单元格时,会弹出一个输入框,需要一个值转到 C 列中的下一个单元格。那是因为在 BI 列中存储了一个公式(& 单元格被锁定),我不喜欢由用户删除或更改,尽管我需要通过公式计算他的输入值,所以我选择将此值存储在它旁边的隐藏单元格中,并将引用添加到我的公式中。VBA怎么可能做到这一点?提前致谢,哈里斯

采纳答案by Gary's Student

This technique uses double clickrather than single click. Your protection must allow the user to double click cells in column B

此技术使用双击而不是单击。您的保护必须允许用户双击B列中的单元格

Enter the following Event macro in the worksheet code area:

在工作表代码区域中输入以下事件宏:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim t As Range, B As Range
    Set t = Target
    Set B = Range("B:B")
    If Intersect(t, B) Is Nothing Then Exit Sub
    Cancel = True
    t.Offset(0, 1).Value = Application.InputBox(Prompt:="Enter data value", Type:=1)
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

因为是工作表代码,所以很容易安装,自动使用:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window
  1. 右键单击 Excel 窗口底部附近的选项卡名称
  2. 选择查看代码 - 这会打开一个 VBE 窗口
  3. 粘贴内容并关闭 VBE 窗口

If you have any concerns, first try it on a trial worksheet.

如果您有任何疑虑,请先在试用工作表上尝试。

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

如果您保存工作簿,宏将与它一起保存。如果您使用的是 2003 年以后的 Excel 版本,则必须将文件另存为 .xlsm 而不是 .xlsx

To remove the macro:

要删除宏:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window
  1. 调出如上的 VBE 窗口
  2. 清除代码
  3. 关闭 VBE 窗口

To learn more about macros in general, see:

要了解有关一般宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

要了解有关事件宏(工作表代码)的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/event.htm

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

必须启用宏才能使其工作!

回答by EngJon

I would suggest using the following Code:

我建议使用以下代码:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
            Cells(Target.Row, Target.Column + 1).Value = Application.InputBox("Insert your value please")
        End If
    End If
End Sub

This pops an inputbox up if you select any cell in column B and puts the value to the Cell in C. You have to put the Code directly into the particular Sheet in the VBA Window.

如果您选择 B 列中的任何单元格并将值放入 C 中的单元格,则会弹出一个输入框。您必须将代码直接放入 VBA 窗口中的特定工作表中。