在几个单元格上运行单个宏

时间:2013-09-20 21:04:58

标签: vba

我写了一个宏,它插入几行,然后转换一个单元格中存在的文本,这些文本由分隔符从文本到列分隔,然后转置它并被复制为执行首先插入的特殊粘贴的行。我只能在一个单元格上运行宏来获得结果。但现在我想在其他50个单元格上运行宏。我该怎么办?

我的代码在下面

Sub Newsroom()
'
' Macro
' By Ganesh
'
' Keyboard Shortcut: Ctrl+Shift+G
'
    Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
        Semicolon:=True, Comma:=True, Space:=True, Other:=True, FieldInfo:= _
        Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7 _
        , 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array _
        (14, 1)), TrailingMinusNumbers:=True
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    Selection.Insert Shift:=xlDown
    ActiveCell.Offset(-1, 3).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    ActiveCell.Offset(1, -1).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    ActiveCell.Offset(-1, 1).Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.ClearContents
    ActiveCell.Offset(21, -1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "ALLNEWSPLUS"
    With ActiveCell.Characters(Start:=1, Length:=11).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .Color = -16777216
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    ActiveCell.Offset(-21, -2).Range("A1:B1").Select
    Selection.AutoFill Destination:=ActiveCell.Range("A1:B22"), Type:= _
        xlFillDefault
    ActiveCell.Range("A1:B22").Select
End Sub

1 个答案:

答案 0 :(得分:0)

如果不重写所有代码(你应该删除SelectionActiveCell引用以支持更多面向对象的编程),你需要实现一个循环。

假设您最初选择了要操作的单元格范围(1列):

Sub foo()
    Dim rng as Range
    Dim r as Long
    Set rng = Range(Selection.Address)

    For r = rng.Cells.Count to 1 Step -1
        rng.Cells(r).Select

        '''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''
        ' ALL OF YOUR CODE BELONGS HERE
        '
        '
        '
        '''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''

    Next
End Sub