0

如果字符串在第 9 个字符中有 - 符号,我需要一个宏来将字符串的一部分从 A 列剪切并粘贴到 B 列。我在 stackoverflow 上找到了一些代码,它们会复制/粘贴文本部分,但不会剪切/粘贴。我的数据看起来像这样

SUBIAIUP-456253
SUBIAIUP-254

这是我到目前为止的代码:

Public Sub LeftSub()
 Dim cell As Range
 Dim sourceRange As Range

 Set sourceRange = Sheet1.Range("A1:A180")

 For Each cell In sourceRange

     If Mid(cell.Value, 9, 1) = "-" Then

     'this code should cut/paste the value from col A to col B, not copy/paste
     Sheets("Sheet1").Range("B" & cell.Row).Value = Mid(cell.Value, 9, 20)

     End If
  Next
 End Sub

任何帮助深表感谢。
谢谢你。

4

1 回答 1

3

如果要从 A 列中删除要粘贴到 B 列的值,请使用以下命令:

If Mid(cell.Value, 9, 1) = "-" Then
    cell.Offset(, 1).Value = Mid(cell.Value, 9)
    cell.Value = Left(cell, 8)
End If

正如 Sid 和 Brett 指出的那样,如果我们从中点开始获取其余值,我们不需要 Mid 函数的字符数参数。如果要在 B 列值的开头使用破折号,请将中点设置为 9。如果要省略它,请将其设置为 10。

如果要将 A 列中的整个值剪切/粘贴到 B 列,请使用以下命令:

If Mid(cell.Value, 9, 1) = "-" Then
    'this code WILL cut/paste the value from col A to col B, not copy/paste
    cell.Cut cell.Offset(, 1)
End If
于 2013-02-17T01:32:46.110 回答