VBA to import a tab from another workbook

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,342
Office Version
  1. 365
Platform
  1. Windows
Is there a way VBA to have the user identify a workbook and then once they selected the workbook to copy in a tab from that workbook?

Only if the workbook has a tab named "CMCS Bill of Materials".
But I also want to make sure the current workbook doesn't already have a tab by that name. (if it does, something like a MsgBox that says they must first delete the current tab named "CMCS Bill of Materials" and or give them an option to delete it before proceeding).

Hope this isn't too much.

Thanks for the Help. Always trying to learn.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I got most of the way there. But when I run this, it gives me the MsgBox message even though the workbook that is opening does contain a tab with the name "CMCS Bill of Materials"

If I take out the part thats checking for the tab, the code woks.

Any help figuring out why it thinks the opened workbook (CMCS) doesn't have that tab when it does is very much appreciated

Code:
Sub GetCMCS()

    Dim ProposalReports As Workbook
    Dim CMCS As Variant


    Set ProposalReports = ThisWorkbook
    CMCS = Application.GetOpenFilename("Excel files (*.xls*), *.xls*")
    
    If CMCS <> False Then
        Workbooks.Open (CMCS)
        Set CMCS = ActiveWorkbook
        
         If Not Evaluate("isref(CMCS Bill of Materials!a2)") Then
            MsgBox "The Selected Workbook does not contain a CMCS Bill of Materials tab, check to validate the tab is named correctly or select a different Workbook"
            Exit Sub
        Else
        
      Sheets("CMCS Bill of Materials").Select
            Sheets("CMCS Bill of Materials").Copy Before:=ThisWorkbook.Sheets(2)
    
    
    
    End If
    End If


End Sub

thank you!
 
Upvote 0
Hi gheyman. Maybe something like this. HTH. Dave
Code:
Dim sht As Worksheet, Flag As Boolean
Workbooks.Open (CMCS)
For Each sht In Workbooks("CMCS").Worksheets
If LCase(sht.Name) = LCase("CMCS Bill of Materials") Then
sht.copy Before:=ThisWorkbook.Sheets(2)
Flag = True
Exit For
End If
Next sht
If Not Flag Then
MsgBox "The Selected Workbook does not contain a CMCS Bill of Materials tab!" & vbCrLf _
& "Check to validate the tab is named correctly or select a different Workbook"
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,216,080
Messages
6,128,692
Members
449,464
Latest member
againofsoul

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top