Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2023, 06:22 AM
MartinGM MartinGM is offline Declaring a Constant Windows 11 Declaring a Constant Office 2021
Advanced Beginner
Declaring a Constant
 
Join Date: May 2023
Location: England
Posts: 66
MartinGM is on a distinguished road
Default Declaring a Constant

I understand how to declare a Public Constant and set its value eg . . .

Public Const <name> as String = <value>



However I want to define the value as a concatenated string eg . . .

Public Const <name> as String = "C:\Users" & Environ$("username") & "\OneDrive"

. . . but this fails with "Compile error".

I can get round this by just declaring the constant's name, then setting its value in the Auto-Open macro.

I was wondering if there is a better or neater way to do this ?
Reply With Quote
  #2  
Old 05-26-2023, 03:34 AM
Debaser's Avatar
Debaser Debaser is offline Declaring a Constant Windows 10 Declaring a Constant Office 2016
Competent Performer
 
Join Date: Oct 2015
Location: UK
Posts: 221
Debaser will become famous soon enough
Default

A constant cannot be the result of a function since that result could change. Similarly you can't declare a constant and then set its value in another routine.

You can either use a variable - which I assume is what you meant with your second option - or use a function that returns the value you want. You can then simply use that function name as you would the constant.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a Constant Declare Constant for Chr(14) VBorNotVB Word VBA 3 12-06-2020 09:28 PM
Declaring Public variables and accessing in other modules rekent Word VBA 16 11-24-2020 02:09 PM
Declaring a Constant Declaring a variable that is known across modules Officer_Bierschnitt Excel Programming 5 11-18-2015 12:28 PM
Declaring a Constant Keep cell in formula constant jprt68 Excel 1 06-26-2012 08:31 AM
Declaring a Constant Keeping the top row constant in Excel MSmand Excel 2 07-13-2011 01:14 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:30 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft