我对我提出的前一个问题有一个部分解决方案,我希望可以开一个新问题。
我有什么:
.csv-file 1
00|G1|G2|
K4|__|X_|
K5|X_|X_|
.csv-file 2
00|G3|G7|G9|G12
K6|X_|__|X_|__|
K7|__|X_|X_|__|
K8|__|__|__|X_|
我想要的是什么:
最终.csv(或.xls)文件
00|G1|G2|G3|G7|G9|G12
K4|__|X_|__|__|__|__|
K5|X_|X_|__|__|__|__|
K6|__|__|X_|__|X_|__|
K7|__|__|__|X_|X_|__|
K8|__|__|__|__|__|X_|
因此文件2的顶行 - 应添加到文件1顶行的右侧。
文件2左侧的第一列 - 应添加到文件1的第一列下面。
然后,相应的Xes将被复制并粘贴到文件1的Xes的右下角。
我有很多.csv文件,不知道如何使用VBA。
我非常感谢你的帮助!
的问候,
汤姆
答案 0 :(得分:0)
试试这个有点令人费解,但我认为这就是你想要的。我已经把它放在同一个工作簿中,但很容易改为打开文件,并在Workbook中设置工作簿。开放()
编辑以显示我如何处理打开的csv。 (你有很多方法可以做到以下所有)
Sub simplePaste()
Dim lastRow0 As Long
Dim lastColumn0 As Long
Dim lastColumn1
Dim ws0 As Worksheet
Dim ws1 As Worksheet
Dim wb1 As Workbook
Dim CopyRange As Range
Dim pasteRange As Range
Set ws0 = ThisWorkbook.Sheets("Sheet1")
Set wb1 = Workbooks.Open("C:\Users\james\Desktop\source.csv")
Set ws1 = wb1.Sheets(1)
lastRow0 = ws1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastColumn0 = ws1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
lastColumn1 = ws0.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set CopyRange = ws1.Range(ws1.Cells(1, 1), ws1.Cells(lastRow0, lastColumn0))
Set pasteRange = ws0.Cells(1, lastColumn1 + 1)
CopyRange.Copy
pasteRange.PasteSpecial
wb1.Close
End Sub
答案 1 :(得分:0)
所以这段代码应该可以解决问题,它只是错过某种功能来爬行你的目录并逐个打开文件。但这似乎很难写。
Sub simplePaste()
Dim lastRow0 As Long
Dim lastColumn0 As Long
Dim lastColumn1
Dim ws0 As Worksheet
Dim ws1 As Worksheet
Dim CopyRange As Range
Dim pasteRange As Range
Set ws0 = ThisWorkbook.Sheets("Sheet1")
Set ws1 = ThisWorkbook.Sheets("Sheet2")
lastRow0 = ws0.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastColumn0 = ws0.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
lastRow1 = ws1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
vlastColumn1 = ws1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set CopyRange = ws1.Range(ws1.Cells(1, 2), ws1.Cells(1, lastColumn1))
Set pasteRange = ws0.Cells(1, lastColumn0 + 1)
CopyRange.Copy
pasteRange.PasteSpecial
Set CopyRange = ws1.Range(ws1.Cells(2, 1), ws1.Cells(lastRow1, 1))
Set pasteRange = ws0.Cells(lastRow0 + 1, 1)
CopyRange.Copy
pasteRange.PasteSpecial
Set CopyRange = ws1.Range(ws1.Cells(2, 2), ws1.Cells(lastRow1 - 1, lastColumn1))
Set pasteRange = ws0.Cells(lastRow0 + 1, lastColumn0 + 1)
CopyRange.Copy
pasteRange.PasteSpecial
End Sub
它的作用:
糊
将复制范围设置为工作表2中的第一列
糊
将工作表2中的复制范围从B2设置为具有坐标的单元格(lastColumn | lastRow)((这就是所有Xes所在的位置)
感谢您的帮助!
汤姆