저는 농업 생산자가 쉽게 탱크의 양을 계산할 수있는 프로그램을 만들고 있습니다. 나는 그들에게 탱크의 여러 치수를 입력하고 각 탱크의 부피를 개별적으로 계산할 수 있어야한다. 치수는 쉼표로 구분되며 필자는 분할하여 고유 한 열에 넣기를 원합니다. 그런 다음 데이터의 각 열을 가져 와서 볼륨 공식을 적용하여 실린더의 볼륨을 얻는 것이 좋습니다. 그래도 어떻게해야할지 모르겠다. 각 열, 즉 열 1 볼륨, 열 2 볼륨 등을 순환하는 데 필요한 루프가 있다고 느낀다. 그 코드는 아래에있다.Excel VBA for Loop for Volume Calculation
'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 프로그래밍에 대한 나의 새로운 기능으로 인해, 내 루틴에 함수를 구현하는 방법을 너무 확신하지 못합니다. 나는 Function 대신 Sub를 사용하는 것에 만 익숙하다. 매크로 생성 과정에서 어떻게 사용하는지 아이디어를 주시겠습니까? – JuliusDariusBelosarius
@JuliusDariusBelosarius 여기에 모든 것을 집어 넣는 방법을 명확히하기 위해 편집을 추가하겠습니다. 이 함수를 사용하려면 Sub 아래에 복사하여 붙여 넣기 만하면됩니다. Function에서 End Function으로 전체 함수를 복사하고 마지막 End Sub 바로 아래에 붙여 넣으십시오. 그럼, 당신은 단지'calc_cylinder_volume (dblDiameter (i), dblLength (i))' – Etheur
처럼 하위 내에서 함수를 참조 할 수 있습니다. 그냥 사용하면 추가 할 수 있습니다 : Application.WorksheetFunction.Pi()를 얻으려면 –