VBA 宏成功运行然后冻结

VBA 宏成功运行然后冻结

我正在尝试获取合并到一个单元格中的数据并保留该单元格的前 6 个字符,然后将其余字符移到下一列,然后继续处理工作表。我编写了以下宏,它成功运行,然后将 Excel 冻结到硬关闭的程度。任何帮助都将不胜感激,因为我需要处理数千行数据

    Sub SplitCell()
         Dim ws As Worksheet
         Dim cell As Range
         Dim originalValue As String
         Dim firstSixChars As String
         Dim remainingChars As String

         ' Set the worksheet where your data is located 
           Set ws = Worksheets("Sheet1") 

         ' Loop through each cell in the specified column (e.g., Column A)
         For Each cell In ws.Range("A:A").Cells
             originalValue = cell.Value
             firstSixChars = Left(originalValue, 6)
             remainingChars = Mid(originalValue, 7) ' Extract characters after the first 6

         ' Write the extracted values to adjacent columns
           cell.Value = firstSixChars
           cell.Offset(0, 1).Value = remainingChars
         Next cell
     End Sub 

答案1

也许是这样的代码?

Sub Split6()
    Dim ws As Worksheet
    ' Set the worksheet where your data is located 
    Set ws = Worksheets("Sheet1") 
    Dim src As Range
    Set src = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
    src.TextToColumns Destination:=ws.Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(6, 1))
End Sub

答案2

解决方案,分配特定范围的单元格而不是整个列...呃

相关内容