Excel 2010通过插入新行合并列
我有一个大的csv文件(大约10000行),为了上传,我需要适应新的布局 它当前的格式如下:Excel 2010通过插入新行合并列,excel,vba,reorganize,Excel,Vba,Reorganize,我有一个大的csv文件(大约10000行),为了上传,我需要适应新的布局 它当前的格式如下: Index1 Title1 1,2,3,4 Option1 A,B OtherData Index2 Title2 1,2,3 Option2 A,B,C,D OtherData Index3 blank blank blank blank OtherData Index4 Title4 1,2 blank blank OtherData 变成这样的东西 In
Index1 Title1 1,2,3,4 Option1 A,B OtherData
Index2 Title2 1,2,3 Option2 A,B,C,D OtherData
Index3 blank blank blank blank OtherData
Index4 Title4 1,2 blank blank OtherData
变成这样的东西
Index1 Title1 1 Option1 A OtherData
2 B
3
4
Index2 Title2 1 Option2 A OtherData
2 B
3 C
D
Index3 OtherData
Index4 Title4 1 OtherData
2
我只在基本级别上理解VBA,列不一定是B C D,因此,如果宏可以包含注释行来指定列的指定位置,这将非常有用。这应该可以满足您的需要。尽管我假设您的文件是以制表符分隔的。 它使用FileSystemObject读入文件,您需要为其添加对Microsoft脚本运行时的引用,转到工具>引用,并确保选中它。 我已经评论过它在哪里寻找具体的列号,但它应该给你一个想法做什么
Dim fs As New FileSystemObject
Dim ts As TextStream
i = 0
Set ts = fs.OpenTextFile("file.csv")
While Not ts.AtEndOfStream
textline = Split(ts.ReadLine, Chr(9))
Range("a1").Offset(i).Resize(1, 6).Value = textline 'Assumes there are six columns in the file
NewCol1 = Split(textline(2), ",") 'Split the 3rd word into an array (2 as it's zero based)
NewCol2 = Split(textline(4), ",") 'Split the 5rd word into an array
RowCount1 = UBound(NewCol1)
RowCount2 = UBound(NewCol2)
MaxCount = IIf(RowCount1 > RowCount2, RowCount1, RowCount2) 'Find the largest of the two row counters as we need to move down this many rows
If RowCount1 > 0 Then Range("a1").Offset(i, 2).Resize(RowCount1 + 1, 1) = WorksheetFunction.Transpose(NewCol1) 'Put the array vertically in the 3rd column
If RowCount2 > 0 Then Range("a1").Offset(i, 4).Resize(RowCount2 + 1, 1) = WorksheetFunction.Transpose(NewCol2) 'Put the array vertically in the 5th column (4 along from cell A1)
i = i + MaxCount + 1
Wend
你最好先试试,当你陷入困境时再回来,让我重新措辞。我只知道VBA的“hello world”级别。我有点困惑,直到我在空白表中运行它。然后它就像一个符咒。谢谢