From a closed WKB get a cell Value when we do not know the Sheet Name

drom

Active Member
Joined
Mar 20, 2005
Messages
450
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Hi and thanks in advance!
Is it possible to get from a closed workbook file a cell value eg B1, when you do not know the name of the only existing sheet within the desired workbook
I do not want to open the WKB (I know how to get the value opening the file)

I am trying to use Walkenbach's GetValue(path, file, sheet, ref) using ExecuteExcel4Macro but in my case

When using Function GetValue(wPath, wFile, wSheet, wRef)

I know:
  • wPath Known
  • wFile Known
  • wSheet NOT Known
  • wRef Known
So in this case I do not know if is not Possibble when the sheet name in unknown
  • If NOT, then I will open the file, to get the value

Thanks (y)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

drom

Active Member
Joined
Mar 20, 2005
Messages
450
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Try Sheets(1)
How?


VBA Code:
On Error Resume Next
Dim wTxT as string :         wTxT = "'" & wPath & "[" & wFile & "]" & wSHT & "'!" & Range(wCell).Range("A1").Address(, , xlR1C1)
'wTxT ='C:\Users\AAAA\[File01.xlsx](SheetNAME)'!R1C2
'Execute XLM macro
Dim wGetValue  As String:    wGetValue = ExecuteExcel4Macro(wTxT)
Debug.Print wGetValue

I am not opening the file to get sheets(1).name

Thanks!
 

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
823
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
Did you try:

wSHT = Sheets(1).Name
 

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
823
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows

ADVERTISEMENT

Is there a reason that you don't just open the file one time to get the sheet name?

You can run excel code, if you must, to get sheet names from a closed workbook.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,303
Office Version
  1. 2007
Platform
  1. Windows
the only existing sheet
Use GetObject to extract data:

For example:

VBA Code:
Sub GetCellValue()
  Dim wTxT As String, wPath As String, wFile As String, wCell As String
  
  wPath = "C:\data\"
  wFile = "book.xlsx"
  wCell = "B1"
  
  wTxT = GetObject(wPath & wFile).Sheets(1).Range(wCell).Value
End Sub
 
Solution

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
823
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows

ADVERTISEMENT

@DanteAmor I suggested Sheets(1) already. :)
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,303
Office Version
  1. 2007
Platform
  1. Windows
wSHT = Sheets(1).Name
Your suggestion is going to get the sheet name from the active workbook, but we need the sheet name from the closed workbook.

For that I use the GetObject function instead of the ExecuteExcel4Macro function.
;)
 

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
823
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
@DanteAmor I think your code somehow opens the workkbook? The 'closed' workbook shows up in the 'vba project panel' after executing. Doesn't that mean the workbook is open?
 

Forum statistics

Threads
1,136,764
Messages
5,677,612
Members
419,707
Latest member
Anna vib

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
Top