Excel 2016 复制第一个在 excel 中重复的字符后的所有内容 VBA

Excel 2016 copy everything after the first character which repeats itself in excel VBA

我有每行的数字序列,我想 copy and paste 该单元格旁边的每个序列的最后一个数字。该序列包含两个点,我想复制并粘贴出现在第一个点之后的所有内容。

检查以下示例:

Sequence: "12345678.11.11"
Result should be: "11.11"

在不同的单元格中。

对此有什么想法吗? VBA 编码或正常的 excel sheet 编码都可以。我稍后会插入 VBA

感谢您的帮助。 问候, 蒂博

试试这个(注释)代码

Option Explicit

Sub main()
    Dim cell As Range
    With Worksheets("MySheet") '<--| change "MySheet" to your actual sheet name
        For Each cell In .Range("A1:A10").SpecialCells(xlCellTypeConstants) '<--| change "A1:A10" to your actual range with dotted numbers
            cell.Offset(, 1) = "'" & Split(cell, ".")(1) & "." & Split(cell, ".")(2)'<--| write last to dotted numbers in cell adjacent to current one
        Next cell
    End With
End Sub

如果它始终是第一个句点,那么您可以在电子表格中使用以下函数代替 VBA(单元格 A1 上的示例):

=RIGHT(A1, (LEN(A1) - FIND(".", A1)))

我不建议 VBA,因为工作表函数非常适合这种事情:

=MID(A1,FIND(".",A1)+1,100))
=REPLACE(A1,1,FIND(".",A1),"") 

...只是两个尚未给出的示例


如果您想尝试 VBA,请将此函数添加到 VBA 编辑器中的模块:

Function GetRight(rng As Range)
    GetRight = Right(rng.value, Len(rng.value) - InStr(rng.value, "."))
End Function

...然后像这样简单地使用工作表上的函数

=GetRight(A1)

您可以像正常功能一样向下拖动它

编辑

如果您希望 VBA 查找 A 列中的所有值并且 return 需要输出到 B 列

,那么这里是完整代码
Sub GetAllRight()
    Dim rng As Range, r As Range

    With ThisWorkbook.Worksheets("Sheet1")
        Set rng = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp))
    End With
    For Each r In rng
        r.Offset(0, 1).Value = GetRight(r)
    Next r
End Sub

Function GetRight(rng As Range) As String
    GetRight = Right(rng.Value, Len(rng.Value) - InStr(rng.Value, "."))
End Function