標題:
[發問]
IF函數,代碼簡化問題
[打印本頁]
作者:
fusayloveme
時間:
2012-4-3 14:54
標題:
IF函數,代碼簡化問題
請問各位大大,以下這些代碼是否能簡化呢!? 一直找不到感覺用不同的方法寫,讓我感覺有點挫折阿 ><,先謝過大大們....:)
Private Sub CommandButton2_Click()
Range("S1").Select
ActiveCell.FormulaR1C1 = "編號"
Range("S2").Select
ActiveCell.FormulaR1C1 = "=IF(R[1]C[-17]=R[0]C[-17],1,0)"
Selection.AutoFill Destination:=Range("S2:S600"), Type:=xlFillDefault
Range("S2:S600").Select
Range("T1").Select
ActiveCell.FormulaR1C1 = "製程"
Range("T2").Select
ActiveCell.FormulaR1C1 = "=IF(R[1]C[-16]=R[0]C[-16],1,0)"
Selection.AutoFill Destination:=Range("T2:T600"), Type:=xlFillDefault
Range("T2:T600").Select
Range("U1").Select
ActiveCell.FormulaR1C1 = "物料名稱"
Range("U2").Select
ActiveCell.FormulaR1C1 = "=IF(R[1]C[-9]=R[0]C[-9],1,0)"
Selection.AutoFill Destination:=Range("U2:U600"), Type:=xlFillDefault
Range("U2:U600").Select
Range("V1").Select
ActiveCell.FormulaR1C1 = "代碼"
Range("V2").Select
ActiveCell.FormulaR1C1 = "=IF(R[1]C[-13]=R[0]C[-13],1,0)"
Selection.AutoFill Destination:=Range("V2:V600"), Type:=xlFillDefault
Range("V2:V600").Select
Range("W1").Select
ActiveCell.FormulaR1C1 = "總加"
Range("W2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-1])"
Selection.AutoFill Destination:=Range("W2:W600"), Type:=xlFillDefault
Range("W1").Select
Selection.AutoFilter
ActiveSheet.Range("$S$1:$W$600").AutoFilter Field:=5, Criteria1:="4"
Columns("E:H").Select
Selection.EntireColumn.Hidden = True
End Sub
複製代碼
作者:
GBKEE
時間:
2012-4-3 15:15
回復
1#
fusayloveme
Option Explicit
Private Sub CommandButton2_Click()
Range("S1").Resize(, 5) = Array("編號", "製程", "物料名稱", "代碼", "總加")
'Range("S1:W1") = Array("編號", "製程", "物料名稱", "代碼", "總加") '加上註解 系統不會執行
With Range("S2:W600")
.Columns(1) = "=IF(R[1]C[-17]=R[0]C[-17],1,0)"
.Columns(2) = "=IF(R[1]C[-16]=R[0]C[-16],1,0)"
.Columns(3) = "=IF(R[1]C[-9]=R[0]C[-9],1,0)"
.Columns(4) = "=IF(R[1]C[-13]=R[0]C[-13],1,0)"
.Columns(5) = "=SUM(RC[-4]:RC[-1])"
End With
'Range("S2:S600") = "=IF(R[1]C[-17]=R[0]C[-17],1,0)"
'Range("T2:T600") = "=IF(R[1]C[-16]=R[0]C[-16],1,0)"
'Range("U2:U600") = "=IF(R[1]C[-9]=R[0]C[-9],1,0)"
'Range("V2:V600") = "=IF(R[1]C[-13]=R[0]C[-13],1,0)"
'Range("W2:W600") = "=SUM(RC[-4]:RC[-1])"
Range("W1").AutoFilter
ActiveSheet.Range("$S$1:$W$600").AutoFilter Field:=5, Criteria1:="4"
Columns("E:H").EntireColumn.Hidden = True
End Sub
複製代碼
作者:
fusayloveme
時間:
2012-4-3 16:09
回復
2#
GBKEE
感謝GBKEE大!!!,又讓我學到不少! 超感恩^^
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)