我已经录制了一个宏,现在我想让它循环抛出所有工作表而不是写代码 30 次,有人可以帮我吗
I have recorded a macro, now i want it to loop throw all worksheets and not write the code 30 times, can someone help me
Sub Test()
'
' Test Macro
'
'
Sheets("test").Select
Columns("A:D").Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("G1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("J:K").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("H1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("O:O").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("J1").Select
ActiveSheet.Paste
Rows("1:4").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Product_Id"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Category"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Brand"
Range("D1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Model"
Range("E1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "EAN"
Range("F1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "UPC"
Range("G1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "SKU"
Range("H1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Supplier_Shop_Price"
Range("I1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "In_Voice"
Range("J1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "In_Stock"
Range("A1").Select
End Sub
只是简单的循环:你可以这样做,例如:
Sub RUN()
For Each wk In ThisWorkbook.Worksheets
wk.Activate
Test
Next wk
End Sub
添加:
Set wk = ActiveSheet
在测试宏的开头
并使用 wk 而不是 Sheets("Sheet1")
例如
而不是
Sheets("test").Select Columns("A:D").Select
这个
wk.Select Columns("A:D").Select
请修正这个问题的格式。
Sub Test()
'
' Test Macro
'
'
Sheets("test").Select
Columns("A:D").Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("G1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("J:K").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("H1").Select
ActiveSheet.Paste
Sheets("test").Select
Columns("O:O").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("J1").Select
ActiveSheet.Paste
Rows("1:4").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Product_Id"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Category"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Brand"
Range("D1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Model"
Range("E1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "EAN"
Range("F1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "UPC"
Range("G1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "SKU"
Range("H1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "Supplier_Shop_Price"
Range("I1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "In_Voice"
Range("J1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.FormulaR1C1 = "In_Stock"
Range("A1").Select
End Sub
只是简单的循环:你可以这样做,例如:
Sub RUN()
For Each wk In ThisWorkbook.Worksheets
wk.Activate
Test
Next wk
End Sub
添加:
Set wk = ActiveSheet
在测试宏的开头
并使用 wk 而不是 Sheets("Sheet1") 例如 而不是
Sheets("test").Select Columns("A:D").Select
这个
wk.Select Columns("A:D").Select
请修正这个问题的格式。