|
公式虽然好用,但是如果sheet太多了,文件就变大了,运行速度就变得很慢,还容易死机。我在做电脑之供应链成本分析时,常常要将2~3百个Model之物料清单于价格库同时做整理分析。原来我用公式,仅仅1个excel File之体重就达38.4M(并且还去掉了所有无关的格式并不保存数据连接),我刚换的P4 2.0G的PC也不堪重负。更别说做多表和多文件的数据条件汇总了。最近我自己编了一个宏,更新数据时只保留数值,并达到多表条件汇总的目的(功能描述)[在File1中包含N个Model(一个ModelName即为一张Sheet.name,每个Model有M张线路板,File2中集中了所有460多张线路板之物料清单,每张线路板名为sheet.name.单独有一张sheet为每个Model所包含的线路板清单,我们的目的是要将所有物料清单分类价格汇总<包括每颗物料之详细价格波动情况>),我的程序仅仅在于提供参考。
Sub FPP_Cha()
'更新Chassis FPP
Dim i, j As Integer
Dim a, b, c, d, e, f, g, h, a1, b1, c1, d1, e1, f1, g1, h1 As Long
Dim DutyMB, DutyVB, DutyCB, DutyHB, DutyTB, DutyDB, DutyLED, DutyMisc As Long
Dim MB, VB, CB, HB, TB, DB, LED, MISC, modelName, fppFileName, detailFileName As String
fppFileName = Application.InputBox(prompt:="请输入FPP文件名", Title:="输入文件名", Default:="FPP_Brand_Exp.xls", Type:=2)
detailFileName = Application.InputBox(prompt:="请输入Detail之Chassis文件名", Title:="输入文件名", Default:="Cha_Exp.xls", Type:=2)
Windows(fppFileName).Activate '激活FPP_Brand_Exp.xls
Sheets("Model List").Select '选择Model List Sheet
For j = 2 To 2000
modelName = Sheets("Model List").cells(j, 1).Value '读取Model Name
If modelName = "" Or 0 Then Exit For
MB = Sheets("Model List").cells(j, 3).Value '定义各Model包含之Chassis
VB = Sheets("Model List").cells(j, 4).Value
CB = Sheets("Model List").cells(j, 5).Value
HB = Sheets("Model List").cells(j, 6).Value
TB = Sheets("Model List").cells(j, 7).Value
DB = Sheets("Model List").cells(j, 8).Value
LED = Sheets("Model List").cells(j, 9).Value
MISC = Sheets("Model List").cells(j, 10).Value
For i = 8 To 37 '读取Chassis各Group Qty值
Windows(detailFileName).Activate '激活detailFileName
If MB = "" Then
a = 0
a1 = 0
DutyMB = 0
Else
a = Val(Sheets(MB).cells(i - 6, 15).Value)
If VarType(Sheets(MB).cells(i - 6, 16).Value) = 5 Then
a1 = Val(Sheets(MB).cells(i - 6, 16).Value)
Else
a1 = 500
End If
If VarType(Sheets(MB).Range("Q35").Value) = 5 Then
DutyMB = Sheets(MB).Range("Q35").Value
Else
DutyMB = 500
End If
End If
If VB = "" Then
b = 0
b1 = 0
DutyVB = 0
Else
b = Val(Sheets(VB).cells(i - 6, 15).Value)
If VarType(Sheets(VB).cells(i - 6, 16).Value) = 5 Then
b1 = Val(Sheets(VB).cells(i - 6, 16).Value)
Else
b1 = 500
End If
If VarType(Sheets(VB).Range("Q35").Value) = 5 Then
DutyVB = Sheets(VB).Range("Q35").Value
Else
DutyVB = 500
End If
End If
If CB = "" Then
c = 0
c1 = 0
DutyCB = 0
Else
c = Val(Sheets(CB).cells(i - 6, 15).Value)
If VarType(Sheets(CB).cells(i - 6, 16).Value) = 5 Then
c1 = Val(Sheets(CB).cells(i - 6, 16).Value)
Else
c1 = 500
End If
If VarType(Sheets(CB).Range("Q35").Value) = 5 Then
DutyCB = Sheets(CB).Range("Q35").Value
Else
DutyCB = 500
End If
End If
If HB = "" Then
d = 0
d1 = 0
DutyHB = 0
Else
d = Val(Sheets(HB).cells(i - 6, 15).Value)
If VarType(Sheets(HB).cells(i - 6, 16).Value) = 5 Then
d1 = Val(Sheets(HB).cells(i - 6, 16).Value)
Else
d1 = 500
End If
If VarType(Sheets(HB).Range("Q35").Value) = 5 Then
DutyHB = Sheets(HB).Range("Q35").Value
Else
DutyHB = 500
End If
End If
If TB = "" Then
e = 0
e1 = 0
DutyTB = 0
Else
e = Val(Sheets(TB).cells(i - 6, 15).Value)
If VarType(Sheets(TB).cells(i - 6, 16).Value) = 5 Then
e1 = Val(Sheets(TB).cells(i - 6, 16).Value)
Else
e1 = 500
End If
If VarType(Sheets(TB).Range("Q35").Value) = 5 Then
DutyTB = Sheets(TB).Range("Q35").Value
Else
DutyTB = 500
End If
End If
If DB = "" Then
f = 0
f1 = 0
DutyDB = 0
Else
f = Val(Sheets(DB).cells(i - 6, 15).Value)
If VarType(Sheets(DB).cells(i - 6, 16).Value) = 5 Then
f1 = Val(Sheets(DB).cells(i - 6, 16).Value)
Else
f1 = 500
End If
If VarType(Sheets(DB).Range("Q35").Value) = 5 Then
DutyDB = Sheets(DB).Range("Q35").Value
Else
DutyDB = 500
End If
End If
If LED = "" Then
g = 0
g1 = 0
DutyLED = 0
Else
g = Val(Sheets(LED).cells(i - 6, 15).Value)
If VarType(Sheets(LED).cells(i - 6, 16).Value) = 5 Then
g1 = Val(Sheets(LED).cells(i - 6, 16).Value)
Else
g1 = 500
End If
If VarType(Sheets(LED).Range("Q35").Value) = 5 Then
DutyLED = Sheets(LED).Range("Q35").Value
Else
DutyLED = 500
End If
End If
If MISC = "" Then
h = 0
h1 = 0
DutyMisc = 0
Else
h = Val(Sheets(MISC).cells(i - 6, 15).Value)
If VarType(Sheets(MISC).cells(i - 6, 16).Value) = 5 Then
h1 = Val(Sheets(MISC).cells(i - 6, 16).Value)
Else
h1 = 500
End If
If VarType(Sheets(MISC).Range("Q35").Value) = 5 Then
DutyMisc = Sheets(MISC).Range("Q35").Value
Else
DutyMisc = 500
End If
End If
Windows(fppFileName).Activate '激活FPP_Brand_Exp.xls
Worksheets(modelName).Select '选择Model Name值对应之FPP Sheet
cells(i, 27).Value = a + b + c + d + e + f + g + h '计算FPP值
If a1 = 500 Or b1 = 500 Or c1 = 500 Or d1 = 500 Or e1 = 500 Or f1 = 500 Or g1 = 500 Or h1 = 500 Then
cells(i, 28).Value = "=NA()"
Else
cells(i, 28).Value = a1 + b1 + c1 + d1 + e1 + f1 + g1 + h1
End If
If DutyMB = 500 Or DutyCB = 500 Or DutyVB = 500 Or DutyHB = 500 Or DutyTB = 500 Or DutyTB = 500 Or DutyDB = 500 Or DutyLED = 500 Or DutyMisc = 500 Then
Range("CO22").Value = "=NA()"
Else
Range("CO22").Value = DutyMB + DutyVB + DutyCB + DutyHB + DutyTB + DutyDB + DutyLED + DutyMisc
End If
Next
ActiveSheet.Calculate
Next
End Sub |
|