让用户使用 VBA 单击单元格作为 Excel 输入框的输入
我有一个将用户输入存储到变量中的输入框。用户输入的输入是手机号码。
例如,弹出输入框询问用户“您想从哪里开始?”然后,用户可以输入 A4 或他们想要启动的任何单元格。
我的问题是,有没有办法允许用户物理单击单元格 A4 而不是键入它?
预先感谢您的任何帮助
更新:因此,基本上我们有很长的水平跨越的转置数据列表。我们希望这些列表水平堆叠在一起,这就是这段代码应该做的事情。
之前一切正常,但用户必须在输入框中手动输入单元格编号。输入框询问用户要从哪里开始剪切,第二个框询问用户要从哪里开始粘贴。我将这些输入值存储到字符串变量中,一切都非常顺利。
从那时起,我希望用户能够实际单击该单元格,因为很难查看它实际上是哪个行号。下面的代码已更新,以反映尝试用于允许用户单击单元格的更改。我添加了 Application.InputBox 方法并将变量声明更改为 Range。
我一次一个地进入该程序,看看发生了什么,这就是我发现的。之前,如果用户想要从 B4 开始粘贴到 A16,则会选择 B(B4:B15) 的数据范围,剪切它,然后粘贴到 A16。然后,按照我编写代码的方式,它将返回到 B4 用户输入点并使用 for 循环来递增我的 x 变量,它将偏移到右侧的下一列。因此,它会重复剪切列 C(C4:C15) 的过程并将其粘贴到 A28(使用 xldown),依此类推以进行后续列。
当我进入当前代码时,现在发生的情况是,我没有在 Range 变量中看到任何记录的值。它执行第一步剪切 B4:B15 并将其粘贴到 A16,但是当它运行下一个循环时,它不是从 B4 开始并偏移,而是从 A16 开始然后偏移。它应该回到用户选择的B4作为起始点,然后进行偏移。
抱歉,解释很长,但我希望这有助于澄清问题。
使用Application.InputBox的当前代码
Dim x As Integer
Dim strColumnStart As Range
Dim strColumnEnd As Range
On Error Resume Next
Application.DisplayAlerts = False
Set strColumnStart = Application.InputBox("What cell would you like to start at?", "Starting position","Please include column letter and cell number", Type:=8)
On Error GoTo 0
Set strColumnEnd = Application.InputBox("Where would you like to paste the cells to?", "Pasting position", "Please include column letter and cell number", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If strColumnStart = "What cell would you like to start at?" Or _
strColumnEnd = "Please include column letter and cell number" Then
Exit Sub
Else
For x = 0 To strColumnStart.CurrentRegion.Columns.Count
strColumnStart.Select
ActiveCell.Offset(0, x).Select
If ActiveCell.Value = Empty Then
GoTo Message
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut strColumnEnd.Select
ActiveCell.Offset(-2, 0).Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
strColumnStart.Select
End If
Next x
End If
Message:
MsgBox ("Finished")
strColumnEnd.Select
ActiveSheet.Columns(ActiveCell.Column).EntireColumn.AutoFit
Application.CutCopyMode = False
End Sub
I have an InputBox that stores user input into a variable. The input the user is inputting is a cell number.
For example, the input box pops up and asks the user, "Where would you like to start?" The user would then type in A4, or whichever cell they would want to start.
My question is, is there a way to allow the user to physically click on cell A4 instead of typing it in?
Thanks in advance for any help
Update: So, basically we have long lists of transposed data that span horizontally. We want those lists to stacked on top of each other horizontally, which is what this code is supposed to do.
Everything worked fine before, but the user would to have to manually type in the cell number into the InputBox. The input box asks the user where they want to start cutting and the second box asks the user where they want to start pasting. I would store those input values into string variables and everything worked like a charm.
Since then, I wanted the user to be able to physically click on the cell since it can be difficult to look at which row number it actually is. The code below is updated to reflect the changes trying to be used to allow the user to click on the cell. I added the Application.InputBox method and changed my declarations of the variables to Range.
I stepped into the program one at a time to see what was going on and this is what I found. Before, if the User wanted to start at B4 and paste to A16, it would select the data range for B(B4:B15), cut it, and paste it to A16. Then, the way I had the code, it would go back to the B4 user input spot and using a for loop to increment my x variable, it would offset to the next column over to the right. So, it would then repeat the process of cutting column C(C4:C15) and paste it this time to A28(using xldown), and so on for proceeding columns.
What is happening now when I stepped into this current code is that I don't see any recorded values into my Range variables. It does the first step of cutting B4:B15 and pasting it to A16, but when it goes to run the next loop, instead of starting back at B4 and offsetting, it starts off on A16 and then offsets. It should be going back to B4, which the user selected as the starting spot, and then offsetting.
Sorry, for the long explanations, but I hope this helped to clear the situation up.
Current code using Application.InputBox
Dim x As Integer
Dim strColumnStart As Range
Dim strColumnEnd As Range
On Error Resume Next
Application.DisplayAlerts = False
Set strColumnStart = Application.InputBox("What cell would you like to start at?", "Starting position","Please include column letter and cell number", Type:=8)
On Error GoTo 0
Set strColumnEnd = Application.InputBox("Where would you like to paste the cells to?", "Pasting position", "Please include column letter and cell number", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If strColumnStart = "What cell would you like to start at?" Or _
strColumnEnd = "Please include column letter and cell number" Then
Exit Sub
Else
For x = 0 To strColumnStart.CurrentRegion.Columns.Count
strColumnStart.Select
ActiveCell.Offset(0, x).Select
If ActiveCell.Value = Empty Then
GoTo Message
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut strColumnEnd.Select
ActiveCell.Offset(-2, 0).Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
strColumnStart.Select
End If
Next x
End If
Message:
MsgBox ("Finished")
strColumnEnd.Select
ActiveSheet.Columns(ActiveCell.Column).EntireColumn.AutoFit
Application.CutCopyMode = False
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自:http://www.ozgrid.com/VBA/inputbox.htm
更新为OP要求:
From: http://www.ozgrid.com/VBA/inputbox.htm
Updated with OP's requirements: