Adding Sheets

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi, is it possible to get Excel to import the tabs only once, no matter how many times I run the code. That is if new tabs are added.

Any help will be much appreciated.
Many Thanks
Klaus W

Sub Rektangelafrundedehjørner1_Klik()

Dim SS As Integer
SS = Sheets.Count

Path = Range("h1")

Filename = Dir(Path & "*.xlsm")

Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True

For Each Sheet In ActiveWorkbook.Sheets

Sheet.Copy After:=ThisWorkbook.Sheets(SS)

Next Sheet

Workbooks(Filename).Close

Filename = Dir()

Loop

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
VBA Code:
Sub KlausW()
   Dim Ws As Worksheet
   Dim Wbk As Workbook
   Dim Pth As String, Fname As String
   
   Pth = Range("h1").Value
   Fname = Dir(Pth & "*.xlsm")
   Do While Fname <> ""
      Set Wbk = Workbooks.Open(Pth & Fname)
      For Each Ws In Wbk.WorkSheets
         If Not ShtExists(Ws.Name, Wbk) Then
            Ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next Ws
      Wbk.Close False
      Fname = Dir
   Loop
End Sub
and
VBA Code:
Public Function ShtExists(ShtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Upvote 0
Hi Fluff
Excel don't import any sheets/tabs with this code.

KW
 
Upvote 0
Oops, it should be
Rich (BB code):
Sub KlausW()
   Dim ws As Worksheet
   Dim Wbk As Workbook
   Dim Pth As String, Fname As String
   
   Pth = Range("h1").Value
   Fname = Dir(Pth & "*.xlsm")
   Do While Fname <> ""
      Set Wbk = Workbooks.Open(Pth & Fname)
      For Each ws In Wbk.WorkSheets
         If Not ShtExists(ws.Name, ThisWorkbook) Then
            ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next ws
      Wbk.Close False
      Fname = Dir
   Loop
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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