简体   繁体   English

Excel VBA InputBox和MsgBox输出

[英]Excel VBA InputBox and MsgBox Output

I am working on a program in which the user will supply the information for the length of a wall in cell D3 and E3 from a InputBox prompt. 我正在开发一个程序,用户将在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

Once that is done there will be prompts for the radius, length, orientation, and offset. 完成后,将提示您输入半径,长度,方向和偏移。 The values will be enter with a comma and a space eg N1, N2, N3, ... I am having a hard time writing the VBA macro to separate the inputs based on the comma and then enter then in the cells. 将以逗号和空格(例如N1,N2,N3等)输入值。我很难编写VBA宏以基于逗号分隔输入,然后再在单元格中输入。 All the entries should go in the respective column. 所有条目都应放在相应的列中。 EG 例如

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

Len: 60, 40, 96, 82, 72, 48 <---Input by user Len:60、40、96、82、72、48 <-用户输入

Orient: H, H, V, V, V, V, H <---Input by user 方向:H,H,V,V,V,V,H <-用户输入

Offset: 2, 2, 4, 1, 2, 1 <---Input by user 偏移量:2,2,4,1,2,1 <-用户输入

Then according to that VBA it will fill in the cells as shown below. 然后根据该VBA,将如下所示填充单元格。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

问题的形象

why dont u try this: 你为什么不试试这个:
-Create one auxiliar worksheet with the name "Sheet2" -创建一个名为“ Sheet2”的辅助工作表
-Edit ur code with that: -编辑您的代码:

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

-Make reference from ur original sheet to ur new auxiliar sheet! -从您的原始工作表到您的新辅助工作表进行参考!

sure u can improve the code to create one auxiliar sheet every time, do the tasks, transport the values to ur original sheet and than delete this auxiliar sheet. 确保您可以改进代码以每次创建一个辅助工作表,执行任务,将值传输到原始工作表并删除该辅助工作表。

Inorder to split the input you can use the Split() function 为了拆分输入,您可以使用Split()函数

Split (text_string, delimiter, limit, compare) 拆分(文本字符串,定界符,限制,比较)

For example: 例如:

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

This will insert the values in the 6th row starting from the 1st column. 这将从第一列开始将值插入第六行。 If you want it to start from 3rd column, then 如果您希望它从第三列开始,那么

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

UserInput function is prone to many input and reading errors so that it should be followed by quite a robust input treatment routine UserInput函数易于出现许多输入和读取错误,因此应遵循相当强大的输入处理例程

but for a head start you could use TextToColumns() method of Range object to try and take care of some conversions, along with a Replace() function to eliminate spaces: 但是要TextToColumns()您可以使用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

but still you'll HAVE problems with user inputs, being the "input user" the most bastard and inventive species in the galaxy. 但您仍然会遇到用户输入方面的问题,成为“输入用户”是银河系中最混蛋和最有创造力的物种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM