Excel VBA InputBox和MsgBox输出

时间:2016-07-26 02:15:03

标签: excel vba excel-vba

我正在开发一个程序,用户将从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宏来根据逗号分隔输入,然后在单元格中输入。所有条目都应该在相应的列中。 E.G。

Rad:40,30,26,23,24,20< ---用户输入

Len:60,40,96,82,72,48< ---用户输入

定向:H,H,V,V,V,V,H <---由用户输入

偏移:2,2,4,2,2,&lt; ---用户输入

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

非常感谢任何帮助!

Image of the problem

3 个答案:

答案 0 :(得分:1)

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

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

- 从你的原始纸张到你的新辅助纸张参考!

确保你可以改进代码,每次创建一个辅助工作表,执行任务,将值传输到原始工作表,然后删除这个辅助工作表。

答案 1 :(得分:1)

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

拆分(text_string,delimiter,limit,compare)

例如:

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

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

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

答案 2 :(得分:0)

UserInput函数容易出现许多输入和读取错误,因此应该遵循相当强大的输入处理例程

但是要先行,您可以使用TextToColumns()对象的Range方法尝试处理一些转换,并使用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

但您仍然会遇到用户输入问题,他们是&#34;输入用户&#34;银河系中最卑鄙和最具创造力的物种。