gpt4 book ai didi

excel - 根据单元格值从多个工作簿复制行

转载 作者:行者123 更新时间:2023-12-04 20:01:44 25 4
gpt4 key购买 nike

如何根据单元格值从工作簿中复制单元格。
在需要填写的excel文件中,B列包含了可以找到数据的文件名的一部分。

B2 contains 312123-145

B3 contains 312123-195

etc,
通常大约 18 行,但如果找到循环/步骤可以一直到一个空单元格
数据包含的工作簿是 cell-value.xlsm
312123-145.xlsm 中有一张名为 Yield 的工作表
我想从该表中复制 A2:N2,并将该数据粘贴到主 Excel 表中对应行的 E:R 列中。
我不知道如何开始查找单元格值并找到具有正确数据的文件,然后如何进入下一行。
这是我开始使用的代码,我首先对所有单元格值进行了硬编码以保持简单。
Sub ImportWorksheet() 
' This macro will import a file into this workbook
Sheets("Sheet1").Select
PathName = "C:\Documents\test\"
Filename = "312123-195"
TabName = "Yield"
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=PathName & Filename
ActiveSheet.Name = TabName
Sheets(TabName).Copy After:=Workbooks(ControlFile).Sheets(1)
Windows(Filename).Activate
ActiveWorkbook.Close SaveChanges:=False
Windows(ControlFile).Activate
End Sub

最佳答案

从已关闭的工作簿导入数据

Option Explicit

Sub ImportData()

Dim sFolderPath As String: sFolderPath = "C:\Documents\test\"
Dim sFileExtension As String: sFileExtension = ".xlsm" ' ".xls*"
Const sName As String = "Yield"
Const srgAddress As String = "A2:N2"
Const sFileDelimiter As String = "-"

Const dName As String = "Sheet1"
Const dlCol As String = "B" ' Lookup
Const dvCol As String = "E" ' Value
Const dfRow As Long = 2

If Right(sFolderPath, 1) <> "\" Then sFolderPath = sFolderPath & "\"
If Left(sFileExtension, 1) <> "." Then sFileExtension = "." & sFileExtension

Dim dwb As Workbook: Set dwb = ThisWorkbook ' workbook containing this code

Dim dws As Worksheet: Set dws = dwb.Worksheets(dName)
Dim dlRow As Long: dlRow = dws.Cells(dws.Rows.Count, dlCol).End(xlUp).Row
If dlRow < dfRow Then Exit Sub ' no data
Dim dlrg As Range
Set dlrg = dws.Range(dws.Cells(dfRow, dlCol), dws.Cells(dlRow, dlCol))
Dim dcCount As Long: dcCount = dws.Range(srgAddress).Columns.Count
Dim dvrg As Range: Set dvrg = dws.Cells(dfRow, dvCol).Resize(, dcCount)

Dim swb As Workbook
Dim sws As Worksheet
Dim sFilePattern As String
Dim sFileName As String
Dim dlCell As Range
Dim swsCount As Long

Application.ScreenUpdating = False

For Each dlCell In dlrg.Cells
sFilePattern = sFolderPath & Left(CStr(dlCell.Value), _
InStr(1, CStr(dlCell.Value), sFileDelimiter, vbTextCompare) - 1) _
& sFileExtension
sFileName = Dir(sFilePattern)
If Len(sFileName) > 0 Then ' file (workbook) exists
Set swb = Workbooks.Open(sFolderPath & sFileName)
On Error Resume Next
Set sws = swb.Worksheets(sName)
On Error GoTo 0
If Not sws Is Nothing Then ' worksheet exists
dvrg.Value = sws.Range(srgAddress).Value
swsCount = swsCount + 1
End If
swb.Close SaveChanges:=False
End If
Set dvrg = dvrg.Offset(1)
Next dlCell

Application.ScreenUpdating = True

Select Case swsCount
Case 0
MsgBox "No data imported", vbCritical
Case 1
MsgBox "Data imported from one worksheet.", vbInformation
Case Else
MsgBox "Data imported from " & swsCount & " worksheets.", vbInformation
End Select

End Sub

关于excel - 根据单元格值从多个工作簿复制行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70957411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com