0

我编写了一个宏,它插入几行,然后将一个单元格中存在的文本转换为一个单元格中的文本,这些文本由文本到列的分隔符分隔,然后将其转置并复制为执行特殊粘贴的行,这些行首先插入。我只能在一个单元格上运行宏来获得结果。但现在我想在其他 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
4

1 回答 1

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
于 2013-09-20T21:37:38.790 回答