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

drom

Well-known Member
Joined
Mar 20, 2005
Messages
521
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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!
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Solution
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.
;)
 
Upvote 0
@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?
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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