需要宏将逗号添加到文本字符串的开头

时间:2017-12-22 20:59:16

标签: excel-vba vba excel

我需要一个宏来将逗号附加到文本字符串列的开头。我自己录制了这个动作,但是它仅限于C列(通常,我需要执行此操作的文本字符串出现在不同的列中),并且还将范围的应用限制为工作表中的特定行数I记录下来(在这种情况下,114)。

这是原始的录制宏输出:

Sub AddCommaToESIID()
'
' AddCommaToESIID Macro
'

'
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("C2").Select
    ActiveCell.FormulaR1C1 = ","
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C114")
    Range("C2:C114").Select
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E114")
    Range("E2:E114").Select
    Selection.Copy
    Range("D2").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Columns("E:E").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("C:C").Select
    Selection.Delete Shift:=xlToLeft
End Sub

我想修改它以实现以下目的:

  1. 使宏应用于我选择的任何列,而不是列C

  2. 一旦我选择了一个列,请将宏应用于我正在处理的特定工作表中的许多行。

  3. 提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这将更改您选择的任何范围内的所有非公式

Sub AddCommaToESIID()

    Dim rCell As Range

    If TypeName(Selection) = "Range" Then
        For Each rCell In Selection.Cells
            If Not rCell.HasFormula Then
                rCell.Value = "," & rCell.Value
            End If
        Next rCell
    End If

End Sub
相关问题