VBA将角色移动到下一列

时间:2017-05-16 23:57:52

标签: excel vba excel-vba

我想编写一个代码,如果[self bounds]包含 REG ,请将其移至column A并将其从column B中删除。

例如:

如果, A A1 = tsf - REG

然后,结果,我想看到我的细胞如下:

B1 is empy A1=tsf

如何为此类目的编写代码?

1 个答案:

答案 0 :(得分:-1)

Option Explicit
----------------------------------------------------
Sub moveReg()

Dim wb As Workbook
Dim ws As Worksheet
Dim lastRow As Long

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
lastRow = ws.Cells(Rows.Count, "A").End(3).Row

Dim i As Long
For i = 1 To lastRow

    Range("A" & i).Replace What:=" ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    Dim str As String, reg As String
    str = Range("A" & i).Value
    reg = "REG"

    Dim indexOfREG As Integer
    indexOfREG = InStr(1, str, reg, vbTextCompare)

    Dim finalString As String
    finalString = Left(str, Len(str) - indexOfREG + 1)

    Dim REGString As String
    REGString = Right(str, (Len(reg)))

    Range("A" & i).Value = finalString
    Range("B" & i).Value = REGString

    i = i + 1

Next

End Sub