我有一个 ms-access 按钮,用于获取特定的 excel 文件,该按钮后面是一个 vba 脚本。我把文件当作一个对象。这个 excel 文件有 9 张。我只关心 2,3,4,5。我不想更改原始的第二张表,我想做的是创建第二张表的副本并将其放在 Excel 表的末尾(暂时)。这样我就可以访问合并后的工作表并对其运行一组数据验证。一旦这些验证完成,我就可以删除它,就好像文件从未被更改过一样。
我想执行左内连接或类似的操作,以便基于共享连接键“唯一事务 ID”将工作表 2 与 3、4、5 中的所有列连接起来,即使与仍应添加列。如果找到与键的匹配项,则将该行数据添加到正确的行和相应的列中。
床单是这样布置的:
工作表 2:
A | |
---|---|
唯一交易ID | some_header |
321 | d1 |
123 | d2 |
333 | d3 |
231 | d4 |
908 | d5 |
111 | d6 |
367 | d7 |
工作表 3:
A | |
---|---|
唯一交易ID | 另一个标题 |
333 | q |
231 | w |
908 | e |
第 4 张:
A | |
---|---|
唯一交易ID | x_header |
321 | h |
123 | t |
第 5 张:
A | |
---|---|
唯一交易ID | z_header |
321 | |
123 | |
333 | |
231 | |
908 | |
111 | b_2 |
367 | a_1 |
每张纸的范围是:
Sheet 2: A to AU
Sheet 3: A to I
Sheet 4: A to H
Sheet 5: A to D`
因为我只需要表 2 中的 A 列“唯一交易 ID”,我认为其他范围将从“B”开始。
因此合并后的工作表应该是范围:A 到 BM
当水平合并工作表的范围时:
Sheet 2: A to AU
Sheet 3: AV to BC
Sheet 4: BD to BJ
Sheet 5: BK to BM
合并的结果应该产生一个看起来像这样的表:
A
Unique Transaction ID | some_header | another_header | x_header | z_header |
321 | d1 | | h | |
123 | d2 | | t | |
333 | d3 | q | | |
231 | d4 | w | | |
908 | d5 | e | | |
111 | d6 | | | b_2 |
367 | d7 | | | a_1 |
当然,这些列之间会有很多列。
我的推理有错误吗?更直接的方法?
感谢任何帮助 谢谢
这是我尝试过的方法,似乎没有一个能完全正确地进行合并。我可以得到工作表 2 及其所有列/数据的副本,但工作表 3、4、5 中没有任何内容。
例子:
`Public Sub Init(filePath As Variant)
' Connect to original file
Dim xlApp As Excel.Application, objFile As Excel.Workbook
Set xlApp = CreateObject("Excel.Application")
Set objFile = xlApp.Workbooks.Open(filePath)
' Get the number of sheets
numSheets = objFile.Worksheets.Count
If numSheets = 9 Then
' Define variables for Sheet 2 and the other sheets
Dim sht2 As Worksheet, sht3 As Worksheet, sht4 As Worksheet, sht5 As Worksheet
Dim lastCol As Long, copyRange As Range, pasteRange As Range
' Set variables to the correct worksheets
Set sht2 = objFile.Worksheets("Sheet2")
Set sht3 = objFile.Worksheets("Sheet3")
Set sht4 = objFile.Worksheets("Sheet4")
Set sht5 = objFile.Worksheets("Sheet5")
' Get the last column in Sheet 2
lastCol = sht2.Cells(1, sht2.Columns.Count).End(xlToLeft).Column
' Loop through each column in Sheet 3, 4, and 5 and paste the values into Sheet 2
For i = 1 To lastCol
' Set the copy and paste ranges for each column
Set copyRange = Union(sht3.Cells(1, i), sht4.Cells(1, i), sht5.Cells(1, i)).EntireColumn
Set pasteRange = sht2.Cells(1, sht2.Columns.Count).End(xlToLeft).Offset(0, 1)
' Copy and paste the values
pasteRange.Resize(copyRange.Rows.Count, copyRange.Columns.Count).Value = copyRange.Value
Next i
End If
' Close the original file and clean up
objFile.Close SaveChanges:=False
xlApp.Quit
Set objFile = Nothing
Set xlApp = Nothing
End Sub`
我试过了,但不能在 vba 中使用联合功能。
这个添加了工作表 2 中的所有内容,但是工作表 3、4、5 中的列/数据不存在:
' Initialize object and set default values
Public Sub Init(filePath As Variant)
' Connect to original file
Dim xlApp As Excel.Application, objFile As Excel.Workbook
Set xlApp = CreateObject("Excel.Application")
Set objFile = xlApp.Workbooks.Open(filePath)
' Get the number of sheets
numSheets = objFile.Worksheets.Count
If numSheets = 9 Then
' Get references to the relevant sheets
Dim sheet2 As Excel.Worksheet, sheet3 As Excel.Worksheet, sheet4 As Excel.Worksheet, sheet5 As Excel.Worksheet
Set sheet2 = objFile.Worksheets("sheet2")
Set sheet3 = objFile.Worksheets("sheet3")
Set sheet4 = objFile.Worksheets("sheet4")
Set sheet5 = objFile.Worksheets("sheet5")
' Make a copy of sheet 2 and set it as the active sheet
sheet2.Copy After:=objFile.Worksheets(numSheets)
Dim mergedSheet As Excel.Worksheet
Set mergedSheet = objFile.Worksheets(numSheets + 1)
mergedSheet.Activate
' Add a header row to the merged sheet
Dim headerRange As Excel.Range
Set headerRange = Range("A1:AU1")
headerRange.Copy
Range("AV1").PasteSpecial xlPasteValues
Range("BD1").PasteSpecial xlPasteValues
Range("BK1").PasteSpecial xlPasteValues
' Loop through the unique transaction IDs in sheet 2 and find matching rows in sheets 3, 4, and 5
Dim lastRow As Long, i As Long
lastRow = sheet2.Cells(sheet2.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow ' Start at row 2 to skip the header row
' Get the unique transaction ID from sheet 2
Dim uniqueID As String
uniqueID = sheet2.Cells(i, "A").Value
' Find matching row in sheet 3 and copy values to merged sheet
Dim matchRange As Excel.Range
Set matchRange = sheet3.Columns("A").Find(uniqueID)
If Not matchRange Is Nothing Then
matchRange.Resize(1, 8).Copy
mergedSheet.Cells(i, "AV").PasteSpecial xlPasteValues
End If
' Find matching row in sheet 4 and copy values to merged sheet
Set matchRange = sheet4.Columns("A").Find(uniqueID)
If Not matchRange Is Nothing Then
matchRange.Resize(1, 7).Copy
mergedSheet.Cells(i, "BD").PasteSpecial xlPasteValues
End If
' Find matching row in sheet 5 and copy values to merged sheet
Set matchRange = sheet5.Columns("A").Find(uniqueID)
If Not matchRange Is Nothing Then
matchRange.Resize(1, 3).Copy
mergedSheet.Cells(i, "BK").PasteSpecial xlPasteValues
End If
Next i
' Auto-fit the columns in the merged sheet and save the file
mergedSheet.Columns.AutoFit
objFile.Save
End If
' Close the file and quit Excel
objFile.Close SaveChanges:=True
xlApp.Quit`
第一个程序不会编译。它会触发“未找到方法或数据成员”。
copyRange.Value
的错误。第二个程序有效。数据被复制,但列标题只是重复工作表 2。如果您想要其他工作表的标题,请在每次粘贴之前引用每个工作表标题和副本。但是,选择/复制/粘贴不是必需的。考虑:
' Add a header row to the merged sheet
Range("AV1:BC1").Value = sheet3.Range("A1:I1").Value
Range("BD1:BJ1").Value = sheet4.Range("A1:H1").Value
Range("BK1:BM1").Value = sheet5.Range("A1:D1").Value