VBA 将文本转换为多张工作表中的列,一张除外
VBA to convert text to columns in multiple sheets except one
我是 VBA 编码的新手,我正在尝试将除一个以外的所有 sheet 中的文本转换为文本,但没有取得成功。我在每个 sheet 的 A 列中都有文本,行数可能不同。
这就是我的代码的样子
Sub text_to_column()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim arr() As Variant, i As Long, nrCol As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
ws.Select
nrCol = 20
ReDim arr(1 To nrCol) As Variant
For i = 1 To nrCol
arr(i) = Array(i, 1)
Next
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="^", _
FieldInfo:=arr, _
TrailingMinusNumbers:=True
End If
Next ws
End Sub
请指导。
试试这个代码
Sub Test()
Dim a, x, ws As Worksheet, r As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
For r = 1 To ws.Cells(Rows.Count, 1).End(xlUp).Row
x = Split(ws.Cells(r, 1).Value, "^")
ws.Cells(r, 2).Resize(, UBound(x) + 1).Value = x
Next r
End If
Next was
End Sub
而你的方法,可以使用这样的代码
Sub TextToColumns()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
Application.DisplayAlerts = False
ws.Columns(1).TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="^", FieldInfo:=Array(Array(1, 1)), TrailingMinusNumbers:=True
Application.DisplayAlerts = True
End If
Next was
End Sub
我是 VBA 编码的新手,我正在尝试将除一个以外的所有 sheet 中的文本转换为文本,但没有取得成功。我在每个 sheet 的 A 列中都有文本,行数可能不同。
这就是我的代码的样子
Sub text_to_column()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim arr() As Variant, i As Long, nrCol As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
ws.Select
nrCol = 20
ReDim arr(1 To nrCol) As Variant
For i = 1 To nrCol
arr(i) = Array(i, 1)
Next
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="^", _
FieldInfo:=arr, _
TrailingMinusNumbers:=True
End If
Next ws
End Sub
请指导。
试试这个代码
Sub Test()
Dim a, x, ws As Worksheet, r As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
For r = 1 To ws.Cells(Rows.Count, 1).End(xlUp).Row
x = Split(ws.Cells(r, 1).Value, "^")
ws.Cells(r, 2).Resize(, UBound(x) + 1).Value = x
Next r
End If
Next was
End Sub
而你的方法,可以使用这样的代码
Sub TextToColumns()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
Application.DisplayAlerts = False
ws.Columns(1).TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="^", FieldInfo:=Array(Array(1, 1)), TrailingMinusNumbers:=True
Application.DisplayAlerts = True
End If
Next was
End Sub