PDA

View Full Version : Solved: How can I return cell value via worksheet name



blacktusk
05-11-2009, 11:26 PM
I expect this is a simple question;

I am attempting to pass a set of parameters to a SQL query, using values stored in known worksheet cell names. In this example I have stored a table name in the cell TableName.

However, when I use the syntax below, my tableName variable returns a cell reference of the form;

"=worksheet!$row$col", eg. "=control!$B$2"

Private Sub CommandButton2_Click()
Dim tableName As String
tableName = ActiveWorkbook.Names("TableName").Value
SQLDataTableExtract (tableName)
End Sub

I want the contents of the cell, which should be "Products", not the reference.
How can I convert the cell reference into a value using VBA, so that I can pass it to the query function??

Bob Phillips
05-12-2009, 01:07 AM
Private Sub CommandButton2_Click()
Dim tableName As String
tableName = Application.Evaluate(ActiveWorkbook.Names("TableName").RefersTo)
SQLDataTableExtract (tableName)
End Sub

blacktusk
05-12-2009, 03:54 PM
Thankyou that works perfectly. Much appreciated