標題:
[發問]
如何簡化巨集
[打印本頁]
作者:
luke
時間:
2012-3-31 23:13
標題:
如何簡化巨集
本帖最後由 luke 於 2012-3-31 23:15 編輯
各位先進!
我錄製了乙個巨集, 煩請先進簡化.
Sub Macro1()
Range("A17").Select
ActiveCell.FormulaR1C1 = _
"=IF(R[-2]C="""","""",VLOOKUP(R[-2]C,sheet2!R2C1:R[5]C[5],3,0))"
Range("A18").Select
ActiveCell.FormulaR1C1 = _
"=IF(R[-3]C="""","""",VLOOKUP(R[-3]C,sheet2!R2C1:R[4]C[5],4,0))"
Range("A19").Select
ActiveCell.FormulaR1C1 = _
"=IF(R[-4]C="""","""",VLOOKUP(R[-4]C,sheet2!R2C1:R[3]C[5],5,0))"
Range("A20").Select
ActiveCell.FormulaR1C1 = _
"=IF(R[-5]C="""","""",VLOOKUP(R[-5]C,sheet2!R2C1:R[2]C[5],6,0))"
Range("A17:A20").Select
Selection.Copy
Range("a17:K20").Select
ActiveSheet.Paste
Range("a24:K27").Select
ActiveSheet.Paste
Range("a31:K34").Select
ActiveSheet.Paste
Range("a38:K41").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
作者:
register313
時間:
2012-4-1 19:25
回復
1#
luke
請上傳EXCEL壓縮檔,並說明所需之功能要求
作者:
luke
時間:
2012-4-1 22:10
本帖最後由 luke 於 2012-4-1 22:15 編輯
回復
2#
register313
這是小弟所錄製的原始檔
煩請先進指導
[attach]10268[/attach]
作者:
register313
時間:
2012-4-1 23:05
本帖最後由 register313 於 2012-4-1 23:14 編輯
回復
3#
luke
Sub XX()
Set Rng = Union([A15:K15], [A22:K22], [A29:K29], [A36:K36])
For Each R In Rng
Set s = Sheets("Sheet2").[A:A].Find(R, , , xlWhole)
If Not s Is Nothing Then
R.Resize(6, 1) = Application.Transpose(s.Resize(1, 6))
End If
Next
End Sub
複製代碼
作者:
Hsieh
時間:
2012-4-1 23:15
回復
3#
luke
Sub nn()
Set d = CreateObject("Scripting.Dictionary")
With sheet2
For Each a In .Range(.[A1], .[A1].End(xlDown))
d(a.Value) = a.Offset(, 1).Resize(, 5).Value
Next
End With
With sheet1
For Each a In .UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers)
a.Offset(1).Resize(UBound(d(a.Value), 2), 1) = Application.Transpose(d(a.Value))
Next
End With
End Sub
複製代碼
作者:
register313
時間:
2012-4-1 23:30
本帖最後由 register313 於 2012-4-1 23:36 編輯
回復
5#
Hsieh
若儲存格為數字但並非字典裡的關鍵字,該如何防錯
Sub nn()
Set d = CreateObject("Scripting.Dictionary")
With sheet2
For Each a In .Range(.[A1], .[A1].End(xlDown))
d(a.Value) = a.Offset(, 1).Resize(, 5).Value
Next
End With
With sheet1
For Each a In .UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers)
If d.Exists(a.Value) Then
a.Offset(1).Resize(UBound(d(a.Value), 2), 1) = Application.Transpose(d(a.Value))
End If
Next
End With
End Sub
複製代碼
作者:
GBKEE
時間:
2012-4-2 07:51
回復
6#
register313
Sub Ex()
Dim A As Range, F As Range
For Each A In sheet1.UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers)
Set F = sheet2.[A:A].Find(A, lookat:=xlWhole)
With A.Offset(1).Resize(5)
.Value = ""
If Not F Is Nothing Then .Value = Application.Transpose(F.Offset(, 1).Resize(, 5).Value)
End With
Next
End Sub
Sub Ex1()
Dim A As Range, F As Variant
For Each A In sheet1.UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers)
F = Application.Match(A, sheet2.[A:A], 0)
With A.Offset(1).Resize(5)
.Value = ""
If Not IsError(F) Then .Value = Application.Transpose(sheet2.Cells(F, "A").Offset(, 1).Resize(, 5).Value)
End With
Next
End Sub
複製代碼
作者:
Hsieh
時間:
2012-4-2 08:59
回復
6#
register313
With sheet1
For Each a In Union(.[A15:K15], .[A22:K22], .[A29:K29], .[A36:K36]).SpecialCells(xlCellTypeConstants)
a.Offset(1).Resize(UBound(d(a.Value), 2), 1) = Application.Transpose(d(a.Value))
Next
End With
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)