【问题标题】:Excel VBA InputBox and MsgBox OutputExcel VBA InputBox 和 MsgBox 输出
【发布时间】:2016-07-26 02:15:02
【问题描述】:

我正在开发一个程序,在该程序中,用户将从 InputBox 提示符中提供单元格 D3 和 E3 中墙的长度信息。

Public Sub dimensionInput()

Dim wallWidth As Double 'Get Wall Width Input
wallWidth = Application.InputBox("Input Desired Secondary Containment Wall Width in Inches", "Wall Width", 1)
If wallWidth = False Then
    Exit Sub
Else
Application.Worksheets("Sheet1").Range("D3").Value = wallWidth
End If

Dim wallLen As Variant 'Get Wall Length Input
wallWidth = Application.InputBox("Input Desired Secondary Containment Wall Width in Inches", "Wall Width", 1)
If wallLen = False Then
    Exit Sub
Else
Application.Worksheets("Sheet1").Range("D3").Value = wallWidth
End If

End Sub

完成后,系统会提示您输入半径、长度、方向和偏移量。这些值将使用逗号和空格输入,例如N1,N2,N3,...我很难编写 VBA 宏来根据逗号分隔输入,然后在单元格中输入。所有条目都应放在相应的列中。 例如

Rad: 40, 30, 26, 23, 24, 20

Len: 60, 40, 96, 82, 72, 48

方向:H、H、V、V、V、V、H

偏移量:2、2、4、1、2、1

然后根据该 VBA 它将填充单元格,如下所示。

非常感谢任何帮助!

【问题讨论】:

  • 如果您希望用户输入多个值,要么将它们输入到工作表的特定 [命名] 单元格中,要么为其创建一个专用的 UserForm。弹出几个输入框很烦人。

标签: vba excel


【解决方案1】:

为了拆分输入,您可以使用 Split() 函数

拆分(文本字符串、分隔符、限制、比较)

例如:

Dim xarray() As String
Dim RAD As Variant 'Get Rad Input
RAD = Application.InputBox("Input Desired RAD", "RAD", 1)
xarray() = Split(RAD, ",")
For i = LBound(xarray) To UBound(xarray)
    Cells(6, i + 1).Value = xarray(i)
Next i

这将在第 6 行从第 1 列开始插入值。如果你希望它从第 3 列开始,那么

Cells(6, i + 3).Value = xarray(i)

【讨论】:

    【解决方案2】:

    你为什么不试试这个:
    - 创建一个名为“Sheet2”的辅助工作表
    - 编辑你的代码:

    Public Sub dimensionInput()
    
    Dim wallWidth As Double 'Get Wall Width Input
    wallWidth = Application.InputBox("Input Desired Secondary Containment Wall Width in Inches", "Wall Width", 1)
    If wallWidth = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet1").Range("D3").Value = wallWidth
    End If
    
    Dim wallLen As Variant 'Get Wall Length Input
    wallWidth = Application.InputBox("Input Desired Secondary Containment Wall Width in Inches", "Wall Width", 1)
    If wallLen = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet1").Range("D3").Value = wallWidth
    End If
    
    Dim RAD As Variant 'Get Rad Input
    RAD = Application.InputBox("Input Desired RAD", "RAD", 1)
    If RAD = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet2").Range("A1").Value = RAD
    End If
    
    Dim LENN As Variant 'Get Len Input
    LENN = Application.InputBox("Input Desired LENN", "LEN", 1)
    If LENN = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet2").Range("A2").Value = LENN
    End If
    
    Dim Orient As Variant 'Get Wall Length Input
    Orient = Application.InputBox("Input Desired Orient", "Orient", 1)
    If Orient = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet2").Range("A3").Value = Orient
    End If
    
    Dim Offset As Variant 'Get Wall Length Input
    Offset = Application.InputBox("Input Desired Offset", "Offset", 1)
    If Offset = False Then
        Exit Sub
    Else
    Application.Worksheets("Sheet2").Range("A4").Value = Offset
    End If
    
    Application.Worksheets("Sheet2").Select
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Comma:=True, Space:=True
    
    
    End Sub
    

    -从您的原始表中引用您的新辅助表!

    您可以改进代码以每次创建一张辅助表,执行任务,将值传输到您的原始表,然后删除此辅助表。

    【讨论】:

      【解决方案3】:

      UserInput 函数容易出现许多输入和读取错误,因此应在其后进行相当健壮的输入处理例程

      但为了抢占先机,您可以使用Range 对象的TextToColumns() 方法来尝试处理一些转换,并使用Replace() 函数来消除空格:

      Option Explicit
      
      Public Sub dimensionInput()
          With Worksheets("InputSheet1")
              GetUserInput .Range("D3"), "Input Desired Secondary Containment Wall Length in Inches", "Wall Width", 1
              GetUserInput .Range("E3"), "Input Desired Secondary Containment Wall Length in Inches", "Wall Length", 1
              GetUserInput .Range("C6"), "Input Desired radii for all tanks in Inches", "Tanks radius", 1
              GetUserInput .Range("C7"), "Input Desired lengths for all tanks in Inches", "Tanks lenghts", 1
              GetUserInput .Range("C9"), "Input Desired orientations for all tanks [H/V]", "Tanks orientations", "H"
              GetUserInput .Range("C10"), "Input Desired offsets for all tanks", "Tanks offsets", 1
          End With
      End Sub
      
      Sub GetUserInput(rng As Range, prompt As String, title As String, defVal As Variant)
          With rng
              .Value = Replace(CStr(Application.InputBox(prompt, title, defVal)), " ", "", vbTextCompare)
              If InStr(.Value, ",") > 0 Then .TextToColumns DataType:=xlDelimited, Comma:=True
          End With
      End Sub
      

      但您仍然会遇到用户输入问题,因为“输入用户”是银河系中最混蛋和最有创造力的物种。

      【讨论】:

        猜你喜欢
        • 2021-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多