找到匹配的标题,将值复制/粘贴到最后一行。

huangapple go评论64阅读模式
英文:

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(&quot;DAILY REPORT&quot;)
head_count = WorksheetFunction.CountA(Range(&quot;A1&quot;, Range(&quot;A1&quot;).End(xlToRight)))


Workbooks.Open (&quot;Y:\Daily_Yield&amp;Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm&quot;)
ActiveWorkbook.Sheets(&quot;PLT-1&quot;).Activate

row_count = WorksheetFunction.CountA(Range(&quot;A1&quot;, Range(&quot;A1&quot;).End(xlDown)))
col_count = WorksheetFunction.CountA(Range(&quot;A1&quot;, Range(&quot;A1&quot;).End(xlToRight)))

For i = 1 To head_count

    j = 1
    
    Do While j &lt;= 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(&quot;DAILY REPORT&quot;)
    
    Set wbData = Workbooks.Open( _
       &quot;Y:\Daily_Yield&amp;Production_Report.2\FREEPORT_PRO_YIELD_DAILY_REPORT\PLT-1-2-3_PYDR.xlsm&quot;)
    Set rngSource = wbData.Worksheets(&quot;PLT-1&quot;).Range(&quot;A1&quot;).CurrentRegion
    
    For Each c In wsCopy.Range(wsCopy.Range(&quot;A1&quot;), _
                                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 &#39;matched column?
            With f.Offset(1).Resize(rngSource.Rows.Count - 1)
                c.Offset(1).Resize(.Rows.Count) = .Value &#39;assign values directly
            End With
        End If
                                
    Next c
    
    wbData.Close False
    wsCopy.Cells(1, 1).Select
    
    Application.ScreenUpdating = True
End Sub

huangapple
  • 本文由 发表于 2023年6月15日 04:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定