在动态范围内删除单元格中的最后一个逗号

时间:2019-12-30 23:20:02

标签: excel vba

我很欣赏这可能是一个简单的答案,但是我似乎在SO上找不到任何东西。

我的问题: 我试图从动态范围的每个单元格中删除最后一个逗号。宏不会删除逗号,只会选择范围。

这是我尝试过的:

Sub selecting()
Dim sht As Worksheet
Dim LastColumn As Long
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D1")

'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).row
LastColumn = sht.Cells(StartCell.row, sht.Columns.Count).End(xlToLeft).Column

'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

With ActiveCell
    If Right(.Value, 1) = "," Then .Value = Right(.Value, Len(.Value) - 1)

End With

End Sub

这是返回的内容

enter image description here

4 个答案:

答案 0 :(得分:1)

由于您要删除D列中每个单元格的最后一个字符,因此请尝试在braX的注释中使用此变体。它将循环遍历第4列中的每个已用单元格并删除最后一个字符。

With ThisWorkbook.Sheets("Sheet1")
    For Each cel In .Range("D1", .Cells(.Rows.Count, 4).End(xlUp))
        cel.Value = Left(cel, Len(cel) - 1)
    Next cel
End With

答案 1 :(得分:0)

最常规的方法是遍历您的单元格:


Sub Replacing()

Dim lr As Long, lc As Long
Dim rng As Range, cl As Range

With Worksheets("Sheet1")

    'Find Last Row and Column
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    lc = .Cells(1, .Columns.Count).End(xlToLeft).Column

    'Go through range
    Set rng = .Range(.Cells(1, lc), .Cells(lr, lc))
    For Each cl In rng
        If Right(cl.Value, 1) = "," Then cl.Value = Left(cl.Value, Len(cl.Value) - 1)
    Next cl

End With

End Sub

如果您的范围实际上更大(出于性能考虑),那么最好是通过内存

Sub Replacing()

Dim lr As Long, lc As Long, x As Long
Dim arr As Variant

With Worksheets("Sheet1")

    'Find Last Row and Column
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    lc = .Cells(1, .Columns.Count).End(xlToLeft).Column

    'Go through array
    arr = .Range(.Cells(1, lc), .Cells(lr, lc)).Value
    For x = LBound(arr) To UBound(arr)
        If Right(arr(x, 1), 1) = "," Then arr(x, 1) = Left(arr(x, 1), Len(arr(x, 1)) - 1)
    Next x

    'Write array back to range
    .Range(.Cells(1, lc), .Cells(lr, lc)).Value = arr

End With

End Sub

一种更不常用的方式(我想是在较小范围内)是逃避范围并避免迭代。但是,这是以数组公式为代价的:

Sub Replacing()

Dim lr As Long, lc As Long
Dim rng As Range

With Worksheets("Sheet1")

    'Find Last Row and Column
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    lc = .Cells(1, .Columns.Count).End(xlToLeft).Column

    'Evaluate your range
    Set rng = .Range(.Cells(1, lc), .Cells(lr, lc))
    rng.Value = .Evaluate("IF(RIGHT(" & rng.Address & ",1)="","",LEFT(" & rng.Address & ",LEN(" & rng.Address & ")-1)," & rng.Address & ")")

End With

End Sub

答案 2 :(得分:-1)

使用Replace()函数:

For Each Cell in Range.Cells
    Cell.Value = Replace(Cell.Text, ",", "")
Next Cell

编辑:经过测试,将.Text替换为.Value

编辑2:我也想补充一下,为什么选择范围?我的假设是您正在选择它以启用ActiveCell,但是您可以简单地操纵范围而不选择它。选择范围要求产生错误。我的建议是:

Dim rplcRng as Range
Set rplcRange = sht.Range(StartCell, sht.Cells(LastRow, LastColumn))

For Each Cell in rplcRng.Cells
    Cell.Value = Replace(Cell.Text, ",", "")
Next Cell

编辑3:添加了“ s”

答案 3 :(得分:-1)

修改并尝试:

Option Explicit

Sub test()

    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim str As String

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    'Create a With Statement to avoid sh repetition
    With ws

        'Find Lastrow
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Start looping from row 1 until Lastrow
        For i = 1 To LastRow

            str = .Range("A" & i).Value

            'Check if last character is  ","
            If Right(str, 1) = "," Then
                'Replace the last occurance
                .Range("B" & i).Value = StrReverse(Replace(StrReverse(str), StrReverse(","), StrReverse(""), , 1))
            End If

        Next i

    End With

End Sub