让用户使用VBA单击单元格作为Excel InputBox的输入

Leo*_*eon 3 excel vba excel-2007 excel-vba

我有一个InputBox,用于将用户输入存储到变量中.用户输入的输入是单元号.

例如,弹出输入框并询问用户"你想从哪里开始?" 然后,用户将键入A4,或者他们想要启动的任何一个单元格.

我的问题是,有没有办法让用户物理点击A4格式而不是输入?

在此先感谢您的帮助

更新:所以,基本上我们有一长串水平跨越的转置数据列表.我们希望这些列表水平堆叠在一起,这就是这段代码应该做的事情.

之前一切正常,但用户必须手动输入单元格编号到InputBox中.输入框询问用户他们想要开始切割的位置,第二个框询问用户他们想要开始粘贴的位置.我会将这些输入值存储到字符串变量中,一切都像魅力一样.

从那时起,我希望用户能够物理地点击单元格,因为很难查看它实际上是哪个行号.下面的代码已更新,以反映尝试用于允许用户单击单元格的更改.我添加了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
Run Code Online (Sandbox Code Playgroud)

Tim*_*ams 6

来自:http://www.ozgrid.com/VBA/inputbox.htm

Sub RangeDataType()

    Dim rRange As Range

    On Error Resume Next

        Application.DisplayAlerts = False

            Set rRange = Application.InputBox(Prompt:= _
                "Please select a range with your Mouse to be bolded.", _
                    Title:="SPECIFY RANGE", Type:=8)

    On Error GoTo 0

        Application.DisplayAlerts = True

        If rRange Is Nothing Then
           Exit Sub
        Else
          rRange.Font.Bold = True
        End If
End Sub
Run Code Online (Sandbox Code Playgroud)

更新了OP的要求:

Sub Test2()

     Dim x As Integer
     Dim rngColumnStart As Range
     Dim rngColumnEnd As Range
     Dim rngCopy As Range
     Dim numRows As Long, numCols As Long


     On Error Resume Next
     Set rngColumnStart = Application.InputBox( _
            "Select the cell you'd like to start at", _
            "Select starting position", , Type:=8)

     If rngColumnStart Is Nothing Then Exit Sub

     Set rngColumnEnd = Application.InputBox( _
             "Select where you'd like to paste the cells to", _
             "Select Pasting position", , Type:=8)

     On Error GoTo 0

     If rngColumnEnd Is Nothing Then Exit Sub

     Set rngColumnEnd = rngColumnEnd.Cells(1) 'in case >1 cell was selected

     Set rngCopy = rngColumnStart.CurrentRegion
     numRows = rngCopy.Rows.Count
     numCols = rngCopy.Columns.Count

     For x = 1 To numCols
       rngCopy.Columns(x).Copy _
                rngColumnEnd.Offset((x - 1) * numRows, 0)
     Next x

     rngCopy.ClearContents

     MsgBox ("Finished")
     rngColumnEnd.EntireColumn.AutoFit
     Application.CutCopyMode = False

End Sub
Run Code Online (Sandbox Code Playgroud)