我创建了一个基本上可以实现我想要的宏。但是,由于某些原因,它总是在CSV中添加10个额外的空行(显示为逗号)。当最终用户将CSV提供给外部门户时,总会创建10个空记录。应该在宏中删除或调整哪些内容以避免这种情况?
Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("\\chr-fss001\DataJapan\Sagawa\Sagawa_CSV\")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Range("A1:DZ1" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, 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
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Range("I1").Select
Sheets("Sheet1").Select
Columns("I:L").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Blad1").Select
Columns("I:I").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Cells(Lastrow, "A").Select
ActiveCell.Offset(1).Select
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select
ChDir "J:\Sagawa\DailyPortalFile"
ActiveWorkbook.SaveAs Filename:= _
"J:\Sagawa\DailyPortalFile\" & Format(Now, "yyyy-mm-dd hh mm") & " Sagawa", FileFormat:=xlCSV, _
CreateBackup:=False
End Sub
答案 0 :(得分:0)
根据这里的想法refactor (rewrite),How to avoid using Select in Excel VBA完整代码真是个好主意。{/ 3}}。
然而,对于一个非常快速的&丑陋,非常脏的解决方案,改变这一行:
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
到此:
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row - 10
它将删除最后10行,这显然是不需要的。