Reference Excel sheet number in an external workbook

0 votes

I have a workbook and in that workbook, I would like to make a reference to the external Workbook "Book4"

Book4

When I write:

Text = Workbooks("Book4").Worksheets("Text").Range("A2").Value

It works. Nevertheless, I'd prefer to make the reference using the CodeName rather than the sheet's real name, "Text" (Sheet1).

Yet when I enter the following code, which I would have thought would have worked, it doesn't appear to operate.

Text = Workbooks("Book4").Sheet1.Range("A2").Value

Can anybody please tell me what I'm doing wrong?

Feb 26, 2023 in Others by narikkadan
• 63,720 points
306 views

1 answer to this question.

0 votes

Try this:

Function SheetByCodeName(wb As Workbook, cn As String) As Worksheet
    Dim ws As Worksheet: For Each ws In wb.Worksheets
        If ws.CodeName = cn Then
            Set SheetByCodeName = ws
            Exit Function
        End If
    Next
    Set SheetByCodeName = Nothing
End Function

Use it like this:

Debug.Print SheetByCodeName( ThisWorkbook, "Sheet1" ).Name
answered Mar 17, 2023 by Kithuzzz
• 38,020 points

Related Questions In Others

0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,720 points
1,215 views
0 votes
1 answer

How do I protect all worksheet in an Excel workbook with a single click?

VBA Code : Dim ws as Worksheet Dim pwd ...READ MORE

answered Oct 22, 2022 in Others by narikkadan
• 63,720 points
768 views
0 votes
1 answer

Format an Excel Sheet in Python

The below code will help you refer ...READ MORE

answered Nov 6, 2022 in Others by narikkadan
• 63,720 points
1,312 views
0 votes
1 answer

Update an Excel sheet in real time using Python

Using xlwings, I have really discovered how ...READ MORE

answered Nov 13, 2022 in Others by narikkadan
• 63,720 points
3,069 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,085 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,498 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
805 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
976 views
0 votes
1 answer

How to reference an excel sheet by variable

Identify Worksheet By Partial Name Sub SelectByMonth() ...READ MORE

answered Feb 16, 2023 in Others by Kithuzzz
• 38,020 points
696 views
0 votes
1 answer

Excel VBA- How to loop through specific sheets in a workbook and format the same ranges in each sheet

Range(...) instructs VBA to always use the ...READ MORE

answered Mar 21, 2023 in Others by Kithuzzz
• 38,020 points
1,622 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP