Dynamic Range Name Macro

J

Jonathan Cooper

Learning from this comunity, I am using dynamic range names when importing
data through our ODBC connection, and then creating a pivot table that uses
the range name. I works great. In fact, I create new applications like this
frequently; so much so that I've created a macro to automate the creation of
the dynamic range name. The problem I run into is when the SHEET name has a
space in it.

If there is no space in the name when I create it, everything works fine.
How can I adjust this macro to accomodate a space in the sheet name.

Example,
If the sheet name is 'MyData', then it works fine. But if it is 'My Data'
then the macro doesn't work.


Keep in mind that I've just cobbled this together so it could be totally
wrong.

Dim ws As Worksheet
Dim DataName As String
Dim Formula As String

On Error Resume Next

Set ws = ActiveSheet
Formula = "=Offset(" & ws.Name & "!R1C1, 0, 0, CountA(" & ws.Name &
"!C1), CountA(" & ws.Name & "!R1))"

DataName = Application.InputBox("What do you want to call this range of
Data? NOTE: Assumes that your database starts in cell A1 of the
active worksheet.", "Name your data", "Database" & ActiveWorkbook.Names.Count
+ 1)
ActiveWorkbook.Names.Add Name:=DataName, RefersToR1C1:=Formula
End Sub
 
B

Bernie Deitrick

Jonathan,

Use single quote marks around the ws name:

Formula = "=Offset('" & ws.Name & "'!R1C1, 0, 0, CountA('" & ws.Name &
"'!C1), CountA('" & ws.Name & "'!R1))"

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dynamic range name 6
Lost in code 4
Date as Sheet Name 2
Named Range maker code 9
VBA error 6
VBA function to define name in a worksheet 2
Need help editing a macro 3
Macro to email spreadsheet 2

Top