Excel VBA InputBox和MsgBox输出

我正在处理一个程序,在这个程序中,用户将通过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 

一旦完成,将会提示半径,长度,方向和偏移量。 值将被input一个逗号和一个空格,例如N1,N2,N3,…我很难写VBAmacros来根据逗号分隔input,然后在单元格中input。 所有的条目应该在相应的列中。 例如

Rad:40,30,26,23,24,20 <—用户input

Len:60,40,96,82,72,48 <—用户input

方向:H,H,V,V,V,V,H <—用户input

偏移量:2,2,4,1,2,1 <—用户input

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

任何帮助是极大的赞赏!

问题的形象

为了分割input,你可以使用Split()函数

分割(text_string,分隔符,限制,比较)

例如:

 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 

这将从第一列开始的第六行插入值。 如果你想从第三列开始,那么

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

为什么不试试这个:
– 创build一个名为“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 

– 参考你的原始表单到你的新的表单!

当然,你可以改进代码,每次创build一个辅助表单,完成任务,将值传输到原始表单上,然后删除这个表单。

UserInput函数容易出现很多input和读取错误,所以应该遵循一个非常强大的input处理例程

但是要开始,您可以使用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 

但你仍然会遇到用户input方面的问题,作为“input用户”是银河系中最混蛋和有创造力的物种。