我正在尝试创建一个宏,它将下面的宏拆分为第1行的多列。 A,B,C,D,E,F,G,H,I,J,K,L,M
,但是我有很多行可以做到这一点,到目前为止,我只设法做到了第一行,而其他所有行都保持不变。
这是宏运行之前每一行的外观。所有行都有不同的数据:
BB1300TN1,"TRNSDOA2JA","A32LF4MQ122016","003261761195","D12MP-100C-
R","","AD10920010","0","","","777777",06/20/2018,"kbktqf"
这就是我需要的所有行,但所有行都需要的样子:
A B C D E F G H I J K L M
BB1300TN1 TRNSDOA2JA xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
这是我当前正在尝试使用的代码,但是就像我说的那样,它仅执行第1行并停止。我需要它来遍历所有行(通常在200行左右)并更改所有行。
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Data" Then
Sheet.Delete
End If
Next Sheet
Const strFileName = "C:\Jabil\Jabil.TXT"
Dim wbkS As Workbook
Dim wshS As Worksheet
Dim wshT As Worksheet
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count), Count:=1,
Type:=xlWorksheet)
wshT.Name = "Data"
On Error Resume Next
Set wbkS = Workbooks.Open(fileName:=strFileName)
Set wshS = wbkS.Worksheets(1)
wshS.UsedRange.Copy Destination:=wshT.Range("A1")
wbkS.Close SaveChanges:=False
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4
,1), Array(5, _
1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1),
Array(11, 1), Array(12 _
, 1), Array(13, 1)), TrailingMinusNumbers:=True
Cells.Select
Cells.EntireColumn.AutoFit
答案 0 :(得分:0)
您可以使用数组中的每个单元格值,而不是使用文本作为列,然后使用'split'。 试试下面的代码。如果源数据在A列中,它将在同一工作表中显示从B列开始的结果。 以后,如果要删除双引号,可以随时通过vba查找并替换。
Dim cel As Range
Dim splitarr As Variant
Dim j As Long
Dim str1 As String
Set rng = ThisWorkbook.Sheets("test").Range("A1:A10") 'change sheet and range as required
For Each cel In rng
splitarr = Split(cel.Text, ",")
For j = 0 To UBound(splitarr)
str1 = splitarr(j)
ThisWorkbook.Sheets("test").Cells(cel.Row, (cel.Column + j + 1)).Value = str1
Next
Next