2015-06-26 42 views
1

我將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 
+0

是否要檢索列名? –

+0

請勿將[宏]用於MS Office或VBA。 [宏標籤維基](http://stackoverflow.com/tags/macros/info) –

回答

0

而不是有100個按鈕,我可能只用一個,然後根據選定的單元格移動它。這樣每次光標移動時,該按鈕就會移動,然後您可以使用activecell.column方法對該列進行求和。

你可以使用的代碼會是這樣的:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 


ActiveSheet.Shapes("Button 1").Left = Cells(40, ActiveCell.Column).Left 
End Sub 

在您正在使用的工作簿的表。第40行中的按鈕將移動到選定的列。

+0

我很抱歉,我無法完全得到您的答案。我知道我應該創建一個按鈕(將一個宏分配給它),並在每列的末尾複製粘貼相同的按鈕(使用相同的宏)。 我可以使用active.cell方法修改我當前的代碼,但是我想知道的是「如果按鈕放置在單元格」A7「中,那麼僅針對列」A「採取的操作的代碼。對於如 範圍( 「A1:A6」)。選擇 Selection.Copy 表( 「Sheet2的」)選擇 範圍( 「A1」)選擇 ActiveSheet.Paste 我想採取上述類似的行動。如果按鈕放置在單元格「B7」中,則對列B。 –

+0

嗨Abhimat。不幸的是,如果不使用座標系(這是一場噩夢,尤其是如果列大小可能改變),那麼這樣做並不是真的有可能 - 因此,我建議您使用一個按鈕來適用於所有列和移動每當你選擇他們。它對於同樣的事情是一種不同的方法,但不是有100個對象 - 而是有一個,它服務於所有的列。 – Trum

+0

嗨Trum,感謝您的前進之路。我會盡力處理您提供的方法。 –