我正在创建一个程序,可以让农业生产者轻松计算一个罐的体积。我特别希望他们能够为他们的坦克输入多个维度并分别计算每个坦克的体积。尺寸将以逗号分隔,我希望将它们拆分并放入自己的列中。然后,我希望excel能够获取每列数据并应用体积公式来获取圆柱体的体积。我不知道该怎么做,但我觉得需要循环遍历每一列,例如第1列的音量,第2列的音量等。下面是代码。Excel VBA for Loop计算体积
'Seperates values that are seperated by a comma and then puts them in their own column
Public Sub CommaSep()
Selection.TextToColumns _
Destination:=Columns(3), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:=","
End Sub
的罐尺寸的代码是
Public Sub NoInput()
Sheets.Add.Name = "Hidden Information"
Worksheets(2).Activate
Dim tankCount As Integer
tankCount = Application.InputBox("Enter the Number of Tanks that will be in the Secondary Containment", "Known Tank Quantity", 1)
If tankCount = False Then
Call DeleteSheets
Exit Sub
Else
tankTotal = tankCount
End If
Dim knownVol As Variant
knownVol = Application.InputBox("Enter the Known Volume of the Tank in Gallons. If volume is not known then enter 0", "Known Tank Volume", 0)
If knownVol = "" Then
Call DeleteSheets
Exit Sub
ElseIf knownVol > 0 Then
Application.Worksheets(1).Range("A6").Value = "Known Tank Volume"
Application.Worksheets(1).Range("B6").Value = knownVol
' Application.Worksheets(2).Range("A6").Value = "Known Tank Volume"
' Application.Worksheets(2).Range("B6").Value = knownVol
' Call SPCCSizedSecondary
' Exit Sub
Else
End If
Dim diameter As Variant
diameter = Application.InputBox("Enter the Diameter of the Tanks in feet seperated by commas", "Diameter", 1)
If diameter = False Then
Call DeleteSheets
Exit Sub
Else
Application.Worksheets(1).Range("A4").Value = "Diameter"
Application.Worksheets(1).Range("B4").Value = diameter
End If
Dim length As Variant
length = Application.InputBox("Enter the Length of the Tanks in feet seperated by commas", "Length", 1)
If length = False Then
Call DeleteSheets
Exit Sub
Else
Application.Worksheets(1).Range("A5").Value = "Length"
Application.Worksheets(1).Range("B5").Value = length
End If
'Dim knownVol As Variant
'knownVol = Application.InputBox("Enter the Known Volume(s) of the Tank in Gallons seperated by commas. If volume is not known then enter 0", "Known Tank Volume", 0)
'If knownVol = False Then
' Call DeleteSheets
' Exit Sub
'Else
' Application.Worksheets(1).Range("A6").Value = "Known Tank Volume"
' Application.Worksheets(1).Range("B6").Value = knownVol
'End If
Columns(1).AutoFit
Columns(2).AutoFit
'Call DeleteSheets
End Sub
感谢您的回复!我想由于我对整个Excel VBA编程的新颖性,我不太清楚如何在我的例程中实现一个函数。我只习惯使用Sub而不是Function。你可以给我一个如何在宏内使用它的想法吗? – JuliusDariusBelosarius
@JuliusDariusBelosarius我将添加一个编辑来阐明如何将所有内容放在一起。要使用该功能,您只需将其复制并粘贴到您的Sub下;因此将整个函数从Function复制到End Function,并直接粘贴到最后一个End Sub下面。然后,你可以在你的Sub中引用这个函数,比如'calc_cylinder_volume(dblDiameter(i),dblLength(i))' – Etheur
只需添加你可以使用:Application.WorksheetFunction。Pi()获得Pi –