簡體   English   中英

Excel VBA InputBox和MsgBox輸出

[英]Excel VBA InputBox and MsgBox Output

我正在開發一個程序,用戶將在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,將如下所示填充單元格。

任何幫助是極大的贊賞!

問題的形象

你為什么不試試這個:
-創建一個名為“ 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

-從您的原始工作表到您的新輔助工作表進行參考!

確保您可以改進代碼以每次創建一個輔助工作表,執行任務,將值傳輸到原始工作表並刪除該輔助工作表。

為了拆分輸入,您可以使用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

這將從第一列開始將值插入第六行。 如果您希望它從第三列開始,那么

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

UserInput函數易於出現許多輸入和讀取錯誤,因此應遵循相當強大的輸入處理例程

但是要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

但您仍然會遇到用戶輸入方面的問題,成為“輸入用戶”是銀河系中最混蛋和最有創造力的物種。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM