2016-07-22 6 views
0

저는 농업 생산자가 연료 탱크의 보조 격납 건물을 만들도록 안내하는 Excel 프로그램을 만들어야하는 프로젝트를 진행하고 있습니다. 저는 프로그램에 참여하여 많은 것을 만들었지 만 문제가 발생했습니다. 나는 그들이 직경과 길이가 주어진 볼륨을 계산할 것입니다 여러 탱크가 있다면 섹션이 있습니다. 여기서는 그 코드 그때 최대 음량을 사용하고, 수직 또는 수평 탱크 인 경우에 기초하여 수식을 해결하고자Excel VBA 정렬 기능 출력

Option Explicit 

Sub NoInput() 
    Dim strInputDiameter As String 
    strInputDiameter = Application.InputBox("Tank Diameter") 'get diameter inputs 

    Dim strInputLength As String 
    strInputLength = Application.InputBox("Tank Length") 'get length inputs 

    'convert comma separated inputs to arrays of Doubles 
    Dim dblDiameter() As Double 
    dblDiameter() = str_to_double_array(csv_to_string_array(strInputDiameter)) 
    Dim dblLength() As Double 
    dblLength() = str_to_double_array(csv_to_string_array(strInputLength)) 


    Dim rngCurrCell As Range 
    Set rngCurrCell = ActiveSheet.Range("A1") 

    'set number of containers to whichever input had the least values 
    Dim intContainerCount As Integer 
    intContainerCount = WorksheetFunction.Min(UBound(dblDiameter), UBound(dblLength)) 

    'calculate volume for each container, output to sheet 
    Dim i As Integer 
    For i = 1 To intContainerCount 
     rngCurrCell.Value = "Diameter " & i 
     rngCurrCell.Offset(0, 1).Value = dblDiameter(i) 

     rngCurrCell.Offset(1, 0).Value = "Length " & i 
     rngCurrCell.Offset(1, 1).Value = dblLength(i) 

     rngCurrCell.Offset(2, 0).Value = "Volume " & i 
     rngCurrCell.Offset(2, 1).Value = calc_cylinder_volume(dblDiameter(i), dblLength(i)) 

     Set rngCurrCell = rngCurrCell.Offset(0, 3) 
    Next i 

Call Largest 

End Sub 

Function csv_to_string_array(strCSV As String) As String() 
    csv_to_string_array = Split("," & strCSV, ",") 'don't know why, but needs a leading comma otherwise it skips the first input 
End Function 

Function str_to_double_array(strArray() As String) As Double() 
    Dim tempDblArray() As Double 
    ReDim tempDblArray(UBound(strArray)) 

    Dim i As Integer 
    For i = 1 To UBound(strArray) 
     tempDblArray(i) = CDbl(strArray(i)) 
    Next i 

    str_to_double_array = tempDblArray() 
End Function 

Function calc_cylinder_volume(dblDiameter As Double, dblLength As Double) As Double 
    calc_cylinder_volume = (Application.WorksheetFunction.Pi() * ((dblDiameter/2)^2) * dblLength) 
End Function 

Sub Largest() 
'Cells with dates also return a value, and get covered for determining largest value. Percentages will convert and return numerics. 

Dim rng As Range 
Dim dblMax As Double 

'Set range from which to determine largest value 
Set rng = Sheet1.Range("A1:Z100") 

'Worksheet function MAX returns the largest value in a range 
dblMax = Application.WorksheetFunction.Max(rng) 

'Displays largest value 
MsgBox dblMax 


End Sub 

의 단편이다. 수평 또는 수직 탱크의 경우 볼륨 공식이 다릅니다. msgbox의 형태로 가로 또는 세로로 물어보고 그 결과를 그 탱크에 첨부 할 수있는 방법이 필요합니다. 그래서 예. 열 1은 다음과 같습니다. "직경 : 12 길이 : 12 방향 : 수직". 열 2는 "직경 : 8 길이 : 12 방향 : 수평"입니다. 일단 탱크의 방향을 알게되면 방정식을 푸는데 사용할 적절한 공식을 결정할 수 있습니다.

+3

Re :' '왜 그런지 모르지만 선행 쉼표가 필요하다면 첫 번째 입력을 건너 뜁니다.'- Split은 0을 기반으로 배열을 반환합니다. 루프는 인덱스 1에서 시작합니다. – Comintern

+0

사용자가 5 번째 탱크의 방향을 입력하기를 원한다면 "탱크의 방향이 무엇입니까?"& TankNo'라는 메시지가있는 InputBox 만 있으면됩니다. ** 모든 ** 탱크의 방향을 알아야 할 경우 지름과 길이를 요구할 때 정보를 얻을 수 있습니다 (즉, 쉼표로 구분 된 입력이있는 또 다른 InputBox [아마도 "H"와 "V 사용자가 쉽게 입력 할 수 있도록]). – YowE3K

답변

0

나는 그것을 할 방법을 찾았습니다. 여기에 코드가 있습니다. 그것은 꽤 좋지 않을 수도 있습니다 (무역으로 훌륭한 코더가 아닙니다, 더 나은 기술자 일 것입니다). 그러나 그것은 효과적입니다.

Option Explicit 

Sub NoInput() 
    Dim strInputDiameter As String 
    strInputDiameter = Application.InputBox("Tank Diameter") 'get diameter inputs 

    Dim strInputLength As String 
    strInputLength = Application.InputBox("Tank Length") 'get length inputs 

    Dim strInputOrientation As String 
    strInputOrientation = Application.InputBox("Orientation") 

    'convert comma separated inputs to arrays of Doubles 
    Dim dblDiameter() As Double 
    dblDiameter() = str_to_double_array(csv_to_string_array(strInputDiameter)) 
    Dim dblLength() As Double 
    dblLength() = str_to_double_array(csv_to_string_array(strInputLength)) 
    Dim strOrientationArray() As String 
    strOrientationArray() = Split(strInputOrientation, ",") 


    Dim rngCurrCell As Range 
    Set rngCurrCell = ActiveSheet.Range("A1") 

    'set number of containers to whichever input had the least values 
    Dim intContainerCount As Integer 
    intContainerCount = WorksheetFunction.Min(UBound(dblDiameter), UBound(dblLength)) 

    'calculate volume for each container, output to sheet 
    Dim i As Integer 
    For i = 1 To intContainerCount 
     rngCurrCell.Value = "Diameter " & i 
     rngCurrCell.Offset(0, 1).Value = dblDiameter(i) 

     rngCurrCell.Offset(1, 0).Value = "Length " & i 
     rngCurrCell.Offset(1, 1).Value = dblLength(i) 

     rngCurrCell.Offset(2, 0).Value = "Volume " & i 
     rngCurrCell.Offset(2, 1).Value = calc_cylinder_volume(dblDiameter(i), dblLength(i)) 

     rngCurrCell.Offset(3, 0).Value = "Orientation " & i 
     rngCurrCell.Offset(3, 1).Value = strOrientationArray(i - 1) 

     Set rngCurrCell = rngCurrCell.Offset(0, 3) 
    Next i 

Call Largest 

End Sub 

Function csv_to_string_array(strCSV As String) As String() 
    csv_to_string_array = Split("," & strCSV, ",") 'don't know why, but needs a leading comma otherwise it skips the first input 
End Function 

Function str_to_double_array(strArray() As String) As Double() 
    Dim tempDblArray() As Double 
    ReDim tempDblArray(UBound(strArray)) 

    Dim i As Integer 
    For i = 1 To UBound(strArray) 
     tempDblArray(i) = CDbl(strArray(i)) 
    Next i 

    str_to_double_array = tempDblArray() 
End Function 

Function calc_cylinder_volume(dblDiameter As Double, dblLength As Double) As Double 
    calc_cylinder_volume = (Application.WorksheetFunction.Pi() * ((dblDiameter/2)^2) * dblLength) 
End Function 

Sub Largest() 
'Cells with dates also return a value, and get covered for determining largest value. Percentages will convert and return numerics. 

Dim rng As Range 
Dim dblMax As Double 

'Set range from which to determine largest value 
Set rng = Sheet1.Range("A1:Z100") 

'Worksheet function MAX returns the largest value in a range 
dblMax = Application.WorksheetFunction.Max(rng) 

'Displays largest value 
MsgBox dblMax 


End Sub