View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Hari Prasadh Hari Prasadh is offline
external usenet poster
 
Posts: 63
Default Referring to the base workbook in which the module is situated

Hi,

I want to store the name of the workbook in which the module/macro is
actually there, in to a string variable.

So that if I change the name of the workbook the macro will still run.

I wrote the following, to try out whether it works

Option Explicit
Public Const s As String = ThisWorkbook.Name

Sub try()
MsgBox s
End Sub

Im getting a -- Compile error : constant expression required -- in the
line -- Public Const s As String = ThisWorkbook.Name --

Bascially I have to use this workbook name in different subs within the same
module , hence defined it as -- Public --

I read a recent post -- Dim Vs Public -- in which it was said that if am
using a variable name only within the present module then we dont need to
use Public but Dim would be sufficient. Why isn that working in the case of
Constants. Like I defined the above as -- dim Const s As String =
ThisWorkbook.Name -- and Im getting a -- compile error : expected
identifier -- in the word "const". Where is my understanding going wrong?

a) Why am I getting this error?

b) Is the logic of using "Thisworkbook" correct? I mean would that ensure
that -- s -- the string variable would have the name of the workbook in
which the module is located. (Since, the above macro didnt work so I have no
way of testing it, hence the query)

Thanks a lot,
Hari
India