英文:
Find matching header, copy/paste value in last row
问题
我想从每日报告中复制数据,如果两个工作表的列标题匹配,就打开另一个工作簿(数据库),然后找到匹配标题中最后一个空单元格,将数据从每日报告中粘贴进去,保存,然后关闭。我已经拼凑了一个宏,但我只是初学者,对VBA了解有限,我确信结构和顺序是错误的。
Sub Submit()
Dim head_count As Integer
Dim row_count As Integer
Dim col_count As Integer
Dim i As Integer
Dim j As Integer
Dim wsCopy As Worksheet
Application.ScreenUpdating = False
Set wsCopy = ThisWorkbook.Sheets("DAILY REPORT")
head_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
Workbooks.Open ("Y:\Daily_Yield&Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm")
ActiveWorkbook.Sheets("PLT-1").Activate
row_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
col_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
For i = 1 To head_count
j = 1
Do While j <= col_count
If wsCopy.Cells(1, i) = ActiveSheet.Cells(1, j).Text Then
ActiveSheet.Range(Cells(1, j), Cells(row_count, j)).Copy
wsCopy.Cells(1, i).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = col_count
End If
j = j + 1
Loop
Next i
ActiveWorkbook.Close savechanges:=False
wsCopy.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
希望这个帮助!如果有其他问题,请随时提出。
英文:
I want to copy data from a daily report, open another workbook(databank) if the column headers in both sheets match. then find last empty cell in row in matching header to paste data from the daily report, save, then close. I have pieced together a macro but I'm only just beginning to learn vba. I'm sure the structure and order is wrong.
Sub Submit()
Dim head_count As Integer
Dim row_count As Integer
Dim col_count As Integer
Dim i As Integer
Dim j As Integer
Dim wsCopy As Worksheet
Application.ScreenUpdating = False
Set wsCopy = ThisWorkbook.Sheets("DAILY REPORT")
head_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
Workbooks.Open ("Y:\Daily_Yield&Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm")
ActiveWorkbook.Sheets("PLT-1").Activate
row_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
col_count = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
For i = 1 To head_count
j = 1
Do While j <= col_count
If wsCopy.Cells(1, i) = ActiveSheet.Cells(1, j).Text Then
ActiveSheet.Range(Cells(1, j), Cells(row_count, j)).Copy
wsCopy.Cells(1, i).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = col_count
End If
j = j + 1
Loop
Next i
ActiveWorkbook.Close savechanges:=False
wsCopy.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
I have youtubed for answers for the past week but im limited in knowledge on this topic.
答案1
得分: 2
以下是翻译好的部分:
"Try this out - using Find()
to match columns" 可以尝试这个 - 使用 Find()
来匹配列
Sub Submit()
Dim c As Range, wsCopy As Worksheet, f As Range, wbData As Workbook, rngSource As Range
Application.ScreenUpdating = False
Set wsCopy = ThisWorkbook.Worksheets("DAILY REPORT")
Set wbData = Workbooks.Open( _
"Y:\Daily_Yield&Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm")
Set rngSource = wbData.Worksheets("PLT-1").Range("A1").CurrentRegion
For Each c In wsCopy.Range(wsCopy.Range("A1"), _
wsCopy.Cells(1, Columns.Count).End(xlToLeft)).Cells
Set f = rngSource.Rows(1).Find(what:=c.Value, lookat:=xlWhole)
If Not f Is Nothing Then 'matched column?
With f.Offset(1).Resize(rngSource.Rows.Count - 1)
c.Offset(1).Resize(.Rows.Count) = .Value 'assign values directly
End With
End If
Next c
wbData.Close False
wsCopy.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
希望这对您有所帮助。
英文:
Try this out - using Find()
to match columns
Sub Submit()
Dim c As Range, wsCopy As Worksheet, f As Range, wbData As Workbook, rngSource As Range
Application.ScreenUpdating = False
Set wsCopy = ThisWorkbook.Worksheets("DAILY REPORT")
Set wbData = Workbooks.Open( _
"Y:\Daily_Yield&Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm")
Set rngSource = wbData.Worksheets("PLT-1").Range("A1").CurrentRegion
For Each c In wsCopy.Range(wsCopy.Range("A1"), _
wsCopy.Cells(1, Columns.Count).End(xlToLeft)).Cells
Set f = rngSource.Rows(1).Find(what:=c.Value, lookat:=xlWhole)
If Not f Is Nothing Then 'matched column?
With f.Offset(1).Resize(rngSource.Rows.Count - 1)
c.Offset(1).Resize(.Rows.Count) = .Value 'assign values directly
End With
End If
Next c
wbData.Close False
wsCopy.Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论