Consulting

Results 1 to 2 of 2

Thread: Copy Values if name of sheets in two different workbooks are equal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,873
    Location
    1. I added CODE tags around your macro you can use the [#] icon to insert them and then paste your macro between

    2. Not tested but I guessed -- see the comments

    3. Also added an alternate version -- matter of style and personal choice

    Option Explicit
    
    Sub Import_Basisdatei()
        Dim strPath As String, strDataName As String
        Dim ws As Worksheet
        Dim wbSource As Workbook          ' Not used
        Dim wbTarget As Workbook
        Dim i As Long, ws_count As Long  '   Use Long
        Application.AskToUpdateLinks = False
        Application.AutomationSecurity = msoAutomationSecurityLow
        Application.ScreenUpdating = False
        strPath = ThisWorkbook.Worksheets("Menu").Range("C52") 'Pfad Zieldatei
        strDataName = ThisWorkbook.Worksheets("Menu").Range("C53") 'Name Zieldatei
        Set wbTarget = Application.Workbooks.Open(strPath & strDataName, UpdateLinks:=0, ReadOnly:=False)
        ws_count = ActiveWorkbook.Worksheets.Count  '   This is wbTarget ??
        With ThisWorkbook
            For Each ws In .Worksheets       '   Add the dot so that Worksheets's parent is ThisWorkbook
                For i = 1 To ws_count
                    If ws.Name = wbTarget.Worksheets(i).Range("N1").Value Then
                        ws.Range(ws.Cells(25, 3), ws.Cells(25, 17)).Copy
                        wbTarget.Worksheets(i).Range("C23").PasteSpecial Paste:=xlPasteValues
                    End If
                Next i
            Next ws
        End With
        Application.AskToUpdateLinks = True
        Application.AutomationSecurity = msoAutomationSecurityByUI
        Application.ScreenUpdating = True
    End Sub
    Alternate

    Sub Import_Basisdatei_Alt()
        Dim strPath As String, strDataName As String
        Dim ws As Worksheet
        Dim wbTarget As Workbook
        Dim i As Long
        With Application
            .AskToUpdateLinks = False
            .AutomationSecurity = msoAutomationSecurityLow
            .ScreenUpdating = False
        End With
        strPath = ThisWorkbook.Worksheets("Menu").Range("C52") 'Pfad Zieldatei
        strDataName = ThisWorkbook.Worksheets("Menu").Range("C53") 'Name Zieldatei
        Set wbTarget = Application.Workbooks.Open(strPath & strDataName, UpdateLinks:=0, ReadOnly:=False)
        For Each ws In ThisWorkbook.Worksheets
            For i = 1 To wbTarget.Worksheets.Count
                If ws.Name = wbTarget.Worksheets(i).Range("N1").Value Then
                    ws.Range(ws.Cells(25, 3), ws.Cells(25, 17)).Copy
                    wbTarget.Worksheets(i).Range("C23").PasteSpecial Paste:=xlPasteValues
                End If
            Next i
        Next ws
        With Application
            .AskToUpdateLinks = True
            .AutomationSecurity = msoAutomationSecurityByUI
            .ScreenUpdating = True
        End With
    End Sub
    Last edited by Aussiebear; 12-18-2024 at 07:00 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •