Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 剪切特定单词前的字符串并粘贴到不同的范围:VBA_Excel_Vba - Fatal编程技术网

Excel 剪切特定单词前的字符串并粘贴到不同的范围:VBA

Excel 剪切特定单词前的字符串并粘贴到不同的范围:VBA,excel,vba,Excel,Vba,我需要一个VBA代码 在a列的每个单元格中剪切字符串的一部分,直到“HS”一词 将该部分粘贴到上一行的“N”列中 有些字符串在“HS”之前没有任何内容,因此不应为这些字符串运行代码。我正在处理的工作表有数千行。我不知道该怎么做…这里有一些注释代码,可以实现您想要的功能。注意:您可能会发现查找最后一行很有用 此代码避免实际使用剪切和粘贴,因为这很慢。相反,它直接访问单元格的值 Sub CopyBeforeHS() Dim r As Long, HSindex As Long '

我需要一个VBA代码

  • 在a列的每个单元格中剪切字符串的一部分,直到“HS”一词
  • 将该部分粘贴到上一行的“N”列中

有些字符串在“HS”之前没有任何内容,因此不应为这些字符串运行代码。我正在处理的工作表有数千行。我不知道该怎么做…

这里有一些注释代码,可以实现您想要的功能。注意:您可能会发现查找最后一行很有用

此代码避免实际使用剪切和粘贴,因为这很慢。相反,它直接访问单元格的值

Sub CopyBeforeHS()
    Dim r As Long, HSindex As Long
    ' There are many ways to find the last row, or just replace 100 manually here
    ' You must start at row 2, as r-1 is used in the loop
    For r = 2 To 100
        ' Your sheet name here...
        With ThisWorkbook.Sheets("Sheet1")
            ' Get location of "HS" in each cell in column A
            HSindex = InStr(.Range("A" & r).Value, "HS")
            ' If HS was found AND it wasn't the first characters
            If HSindex > 1 Then
                ' Copy cell text from before "HS" to previous row, column N
                .Range("N" & r - 1).Value = Left(.Range("A" & r).Value, HSindex - 1)
                ' Remove the same text from column A
                .Range("A" & r).Value = Mid(.Range("A" & r).Value, HSindex)
            End If
        End With
    Next r
End Sub

到目前为止,你尝试了什么?你被困在哪里?这里的人不会为你写代码,所以不是免费的编码服务。请用您的代码更新您的问题,以便我们可以帮助解决特定问题。也请阅读。我会确保下次是那样的。非常感谢。