vba中的高级整合

时间:2017-10-17 13:04:40

标签: excel vba excel-vba consolidation

我创建了一个宏来组合两个不同的工作表并应用各种公式。现在,我想进行整合。我对VBA很新,所以我不太了解它的功能。是否可以执行合并(对于特定列,比如列D)并选择合并后要求求和的列,保持相同或平均值?更详细地说,我希望第一列保持不变,第二列要求相加,第三列要求平均值,第四列是合并列,第五列要求和,等等等等!我提供了迄今为止构建的代码,尽管它与整合过程完全无关

Sub Budget_Report()

Application.ScreenUpdating = False

Worksheets.Add.Name = "Budget_Report"

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim lastrow1 As Long
Dim lastrow2 As Long
Dim lastrow3 As Long
Dim lastrow4 As Long
Dim i As Long
Dim cell1 As Range
Dim cell2 As Range
Set ws1 = Worksheets("SAPBW_DOWNLOAD")
Set ws2 = Worksheets("Total_Lane_Level")
Set ws3 = Worksheets("Budget_Report")
lastrow1 = ws2.Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row

With ws1
    For i = lastrow2 To 2 Step -1
        If .Cells(i, "K").Value = "Result" Then
            .Rows(i).EntireRow.Delete
        End If
    Next i
End With

lastrow3 = ws1.Range("A" & Rows.Count).End(xlUp).Row

ws1.Range("AD2:AD" & lastrow3).Formula = "=C2&""-""&H2&""-""&B2&""-""&F2&""-""&D2&""-""&I2"

ws1.Range("AE2:AE" & lastrow3).Formula = "=INDEX('Map'!H:H,MATCH('SAPBW_DOWNLOAD'!AD2,'Map'!G:G,FALSE))"

ws3.Range("A1").Value = "Transportation Planning Point"
ws3.Range("B1").Value = "Origin"
ws3.Range("C1").Value = "Destination"
ws3.Range("D1").Value = "T-Lane"
ws3.Range("E1").Value = "Full FY Total $M Cost"
ws3.Range("F1").Value = "Total FY MSU"
ws3.Range("G1").Value = "Monthly Cost FCST"
ws3.Range("H1").Value = "Monthly Vol FCST"
ws3.Range("I1").Value = "Monthly $/SU FCST"
ws3.Range("J1").Value = "Total Transportation Costs"
ws3.Range("K1").Value = "Currency Rate (to dolars)"
ws3.Range("L1").Value = "SU"
ws3.Range("M1").Value = "Monthly Cost"
ws3.Range("N1").Value = "Monthly Vol"
ws3.Range("O1").Value = "Monthly $/SU"
ws3.Range("P1").Value = "Monthly Cost"
ws3.Range("Q1").Value = "Monthly Vol"
ws3.Range("R1").Value = "Monthly $/SU"
ws3.Range("S1").Value = "%"                  ''''
ws3.Range("U1").Value = "Total FY Loads"
ws3.Range("V1").Value = "Total FY Pall/Load"
ws3.Range("W1").Value = "Monthly #Loads FCST"
ws3.Range("X1").Value = "Pal/Load FCST"
ws3.Range("Y1").Value = "Shipments"
ws3.Range("Z1").Value = "Pallets"
ws3.Range("AA1").Value = "Monthly #Loads"
ws3.Range("AB1").Value = "Pal/Load "

ws3.Range("A2:A" & lastrow2).Value = ws1.Range("C2:C" & lastrow2).Value
ws3.Range("B2:B" & lastrow2).Value = ws1.Range("B2:B" & lastrow2).Value

ws3.Range("C2:C" & lastrow2).Value = ws1.Range("F2:F" & lastrow2).Value
ws3.Range("D2:D" & lastrow2).Value = ws1.Range("AE2:AE" & lastrow2).Value

ws3.Range("J2:J" & lastrow2).Value = ws1.Range("W2:W" & lastrow2).Value
ws3.Range("J2:J" & lastrow2).NumberFormat = "0.00"

ws3.Range("L2:L" & lastrow2).Value = ws1.Range("S2:S" & lastrow2).Value
ws3.Range("L2:L" & lastrow2).NumberFormat = "0.00"

ws3.Range("Y2:Y" & lastrow2).Value = ws1.Range("V2:V" & lastrow2).Value
ws3.Range("Y2:Y" & lastrow2).NumberFormat = "0.00"

ws3.Range("Z2:Z" & lastrow2).Value = ws1.Range("L2:L" & lastrow2).Value
ws3.Range("Z2:Z" & lastrow2).NumberFormat = "0.00"

lastrow4 = ws3.Range("A" & Rows.Count).End(xlUp).Row

ws3.Range("E2:E" & lastrow4).Formula = "=INDEX('Total_Lane_Level'!M:M,MATCH('Budget_Report'!D2,'Total_Lane_Level'!B:B,FALSE))"
ws3.Range("E2:E" & lastrow4).NumberFormat = "0.00"

ws3.Range("F2:F" & lastrow4).Formula = "=INDEX('Total_Lane_Level'!F:F,MATCH('Budget_Report'!D2,'Total_Lane_Level'!B:B,FALSE))"
ws3.Range("F2:F" & lastrow4).NumberFormat = "0.00"

ws3.Range("G2:G" & lastrow4).Formula = "=E2/12"
ws3.Range("G2:G" & lastrow4).NumberFormat = "0.00"

ws3.Range("H2:H" & lastrow4).Formula = "=F2/12"
ws3.Range("H2:H" & lastrow4).NumberFormat = "0.00"

ws3.Range("I2:I" & lastrow4).Formula = "=G2/H2"
ws3.Range("I2:I" & lastrow4).NumberFormat = "0.00"

For Each cell1 In ws3.Range("K2:K" & lastrow4)
    If cell1.Offset(0, -8).Value = "ROMANIA" Then
        cell1.Value = 4.24790790535661
    ElseIf cell1.Offset(0, -8).Value = "WEST BANK & GAZ" Or cell1.Offset(0, -8).Value = "ISRAEL" Then
        cell1.Value = 1
    Else
        cell1.Value = 1.07174
    End If
Next cell1
ws3.Range("K2:K" & lastrow4).NumberFormat = "0.00"

For Each cell2 In ws3.Range("M2:M" & lastrow4)
    If cell2.Offset(0, -10).Value = "ROMANIA" Then
        cell2.Value2 = cell2.Offset(0, -3).Value2 / cell2.Offset(0, -2).Value2
    Else
        cell2.Value2 = cell2.Offset(0, -3).Value2 * cell2.Offset(0, -2).Value2
    End If
Next cell2

ws3.Range("N2:N" & lastrow4).Formula = "=L2/1000"
ws3.Range("N2:N" & lastrow4).NumberFormat = "0.00"

ws3.Range("O2:O" & lastrow4).Formula = "=M2/L2"
ws3.Range("O2:O" & lastrow4).NumberFormat = "0.00"

ws3.Range("P2:P" & lastrow4).Formula = "=G2-M2"
ws3.Range("P2:P" & lastrow4).NumberFormat = "0.00"

ws3.Range("Q2:Q" & lastrow4).Formula = "=H2-N2"
ws3.Range("Q2:Q" & lastrow4).NumberFormat = "0.00"

ws3.Range("R2:R" & lastrow4).Formula = "=I2-O2"
ws3.Range("R2:R" & lastrow4).NumberFormat = "0.00"

ws3.Range("U2:U" & lastrow4).Formula = "=INDEX('Total_Lane_Level'!J:J,MATCH('Budget_Report'!D2,'Total_Lane_Level'!B:B,FALSE))"
ws3.Range("U2:U" & lastrow4).NumberFormat = "0.00"

ws3.Range("V2:V" & lastrow4).Formula = "=INDEX('Total_Lane_Level'!I:I,MATCH('Budget_Report'!D2,'Total_Lane_Level'!B:B,FALSE))"
ws3.Range("V2:V" & lastrow4).NumberFormat = "0.00"

ws3.Range("W2:W" & lastrow4).Formula = "=U2/12"
ws3.Range("W2:W" & lastrow4).NumberFormat = "0.00"

ws3.Range("X2:X" & lastrow4).Formula = "=V2"
ws3.Range("X2:X" & lastrow4).NumberFormat = "0.00"

ws3.Range("AA2:AA" & lastrow4).Formula = "=Y2"
ws3.Range("AA2:AA" & lastrow4).NumberFormat = "0.00"

ws3.Range("AB2:AB" & lastrow4).Formula = "=Z2/Y2"
ws3.Range("AB2:AB" & lastrow4).NumberFormat = "0.00"

Application.ScreenUpdating = True

End Sub

0 个答案:

没有答案