我將excel表格準備爲名爲「INPUT SHEET」的數據輸入表。數據被添加到固定的特定編號的各個列中。這個「INPUT SHEET」的行。 在每行的末尾,我提供了一個宏按鈕,它從每列中選取值並創建另一個新工作表。適用於一列的宏可適用於excel中的所有列VB
問題是我有100個這樣的列,我想避免編輯每個宏來對付每列。我想要一個單獨的宏,它標識按下按鈕的列,並相應地只在該列上工作。 COLUMN U的示例宏如下所示:我希望對此表進行一些修改,以便相同的代碼可以適用於所有coulmns。
' Macro1 Macro===ROW U
'
' Create new sheet copying from DATASHEET 1 before last sheet
'
Worksheets("DATASHEET 1").Copy before:=Sheets(Worksheets.Count)
Set wks = ActiveSheet
Sheets("INPUT").Select
Range("U10").Select
Selection.Copy
' Retaining the name of sheet
'
Range("U150").Select
ActiveSheet.Paste
wks.Name = Range("U10").Value
' Copying the notes
'
Worksheets(Range("u10").Value).Activate
Range("D62:BF87").Select
Selection.ClearContents
Range("AY6").Value = "2"
Range("A7:BF7").Select
ActiveCell.FormulaR1C1 = "=INPUT!R[3]C[20]"
Dim i As Integer, j As Integer
j = 61
For i = 63 To 88
Sheets("INPUT").Select
If Cells(i, 21).Value = "YES" Then
j = j + 1
Worksheets(Range("U10").Value).Activate
Range(Cells(j, 4), Cells(j, 58)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Sheets("INPUT").Select
Cells(i, 2).Copy
Worksheets(Range("U10").Value).Activate
Cells(j, 4).PasteSpecial Paste:=xlPasteValues
Range(Cells(j, 4), Cells(j, 58)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
End If
Next i
是否要檢索列名? –
請勿將[宏]用於MS Office或VBA。 [宏標籤維基](http://stackoverflow.com/tags/macros/info) –