Macro to compare worksheet names in 2 workbooks

wilkisa

Well-known Member
Joined
Apr 7, 2002
Messages
649
I have 2 workbooks each with 30 sheets, numbered 1 to 30. I need to link data from Workbook1 Sheet1 to the data in Workbook2 Sheet1, Workbook1 Sheet2 to Workbook2 Sheet2, etc. etc. etc. Can someone help me with this code?
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

hatman

Well-known Member
Joined
Apr 8, 2005
Messages
2,664
something like this?

Code:
Sub coord()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim sht2 As Worksheet
    Dim rnge As Range
    
    
    'assuming that the source workbook is currently open
    
    Set wb = Workbooks("book1")
    
    For Each sht In wb.Sheets
    
        On Error Resume Next
        
        Set sht2 = ActiveWorkbook.Sheets(sht.Name)
        
        On Error GoTo 0
        
        If Not sht2 Is Nothing Then
    
            For Each rnge In sht.UsedRange
            
                If sht2.Range(rnge.Address).Value = "" And rnge.Value <> "" Then
                
                    sht2.Range(rnge.Address).Formula = "='[" & wb.Name & "]" & sht.Name & "'!" & rnge.Address
                    
                End If
            
            Next rnge
        
            Set sht2 = Nothing
            
        End If
        
    Next sht
    
    Set wb = Nothing

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,112,937
Messages
5,543,095
Members
410,583
Latest member
gazz57
Top