Name of sheets in cell to import

KlausW

Active Member
Joined
Sep 9, 2020
Messages
453
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have a challenge with this VBA code.
I would like to be able to change the name of the sheets I would like to import.
I can get it to run if I change the line
With ActiveWorkbook.Sheets.Nanme = Range("C1").Value
To
With ActiveWorkbook.Sheets("IBM")
The name of the sheet I would like to be imported is in C1
Any help will be appreciated
Best Regards
Klaus W

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim Path As String
Dim Filename As String


Path = Range("a1").Value
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
On Error Resume Next
    With ActiveWorkbook.Sheets.Name = Range("C1").valu
        .Copy After:=ThisWorkbook.Sheets(1)
     End With

Workbooks(Filename).Close

Filename = Dir()
Loop

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You have two typo failures.

With ActiveWorkbook.Sheets.Nanme = Range("C1").Value
Has to be: Name
And in your code:
With ActiveWorkbook.Sheets.Name = Range("C1").valu
...are missing an e after .Valu
 
Upvote 0
If C1 contains a sheet name, then the correct syntax would be:

Code:
With ActiveWorkbook.Sheets(Range("C1").Value)

But rather than relying on which workbook is active, you could open the workbook like this. Then you can subsequently refer to wbOpen or ThisWorkbook, and it will always be clear which workbook you mean.

VBA Code:
Dim wbOpen As Workbook

'...

Set wbOpen = Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)

You should also appropriately qualify the (currently unqualified) reference to Range("C1") ;)
 
Upvote 0
You have two typo failures.

With ActiveWorkbook.Sheets.Nanme = Range("C1").Value
Has to be: Name
And in your code:
With ActiveWorkbook.Sheets.Name = Range("C1").valu
...are missing an e after .Valu
Thanks, I fixed it. But it doesn't do what I want.
 
Upvote 0
Thanks, I fixed it. But I can't get it to do that.
VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim Path As String
Dim Filename As String
Dim wbOpen As Workbook

Path = Range("a1").Value
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
On Error Resume Next
    With ActiveWorkbook.Sheets(Range("C1").Value)
        .Copy After:=ThisWorkbook.Sheets(1)
     End With
Set wbOpen = Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)
Workbooks(Filename).Close

Filename = Dir()
Loop

End Sub
 
Upvote 0
Try this:

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

    Dim Path As String
    Dim Filename As String, SheetName As String
    
    Path = Range("A1").Value
    SheetName = Range("C1").Value
    Filename = Dir(Path & "*.xlsm")
        
    Do While Filename <> ""
        With Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)
            .Sheets(SheetName).Copy After:=ThisWorkbook.Sheets(1)
            .Close SaveChanges:=False
        End With
        Filename = Dir()
    Loop

End Sub
 
Upvote 0
Solution
Hi, it runs well, there is a sheet with the same name (The name is in C1) it must be retrieved from 6 different files in the same folder. The VBA code only takes from a file. Coming up with errors in this line.

Regards Klaus
VBA Code:
.Sheets(SheetName).Copy After:=ThisWorkbook.Sheets(1)
 
Upvote 0
Try this:

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

    Dim Path As String
    Dim Filename As String, SheetName As String
  
    Path = Range("A1").Value
    SheetName = Range("C1").Value
    Filename = Dir(Path & "*.xlsm")
      
    Do While Filename <> ""
        With Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)
            .Sheets(SheetName).Copy After:=ThisWorkbook.Sheets(1)
            .Close SaveChanges:=False
        End With
        Filename = Dir()
    Loop

End Sub
Hi StephenCrump, it was my mistake, a mistake 40 as we say in Denmark. 40 cm from the body. The code you made runs as it should. Thank you very much. Best regards from Klaus in Denmark
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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