![]() |
First Time VBA user- really easy question :-)
I'm maintaining some VBA code for Excel 2003, developed by a person no longer employed where I work. This code contains several small forms, and work sheets. One the main worksheet, there are several command buttons, (and the event handlers for these buttons as well) I want to add a version number to the program, so that the string variable is defined as a constant in the VB code, but is displayed on the main worksheet. I've defined the constant as follows Private Const ProgramVersionNumber As String = "1.1" Now, I want to display this in a cell on the main worksheet. 1. How do I print/display the value of this variable (from the VB code) in Cell A1, ? 2. Can I reference this variable (or any variables in the VB code) from the worksheet? If so, what is the syntax for doing this? I've tried entering =ProgramVersionNumber in Cell A1, but that doesn't work. I could use some beginners references for Excel VBA. I'm pretty comfortable with Visual Basic (VB6 to be precise), but I rarely work with Excel, let alone driving Excel from VBA code. thanks |
First Time VBA user- really easy question :-)
You can't directly read the version number from a worksheet cell. You need a
function in a module (create from "Insert" menu in VBA) that will read the constant and return its value to the worksheet cell. E.g., Public Function GetVersion() GetVersion = ProgramVersionNumber End Function Then, you can call this from a worksheet cell with =GetVersion() -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting LLC www.cpearson.com (email on the web site) "cappy2112" wrote in message ups.com... I'm maintaining some VBA code for Excel 2003, developed by a person no longer employed where I work. This code contains several small forms, and work sheets. One the main worksheet, there are several command buttons, (and the event handlers for these buttons as well) I want to add a version number to the program, so that the string variable is defined as a constant in the VB code, but is displayed on the main worksheet. I've defined the constant as follows Private Const ProgramVersionNumber As String = "1.1" Now, I want to display this in a cell on the main worksheet. 1. How do I print/display the value of this variable (from the VB code) in Cell A1, ? 2. Can I reference this variable (or any variables in the VB code) from the worksheet? If so, what is the syntax for doing this? I've tried entering =ProgramVersionNumber in Cell A1, but that doesn't work. I could use some beginners references for Excel VBA. I'm pretty comfortable with Visual Basic (VB6 to be precise), but I rarely work with Excel, let alone driving Excel from VBA code. thanks |
First Time VBA user- really easy question :-)
On Jun 18, 12:02 pm, "Chip Pearson" wrote:
You can't directly read the version number from a worksheet cell. You need a function in a module (create from "Insert" menu in VBA) that will read the constant and return its value to the worksheet cell. E.g., Public Function GetVersion() GetVersion = ProgramVersionNumber End Function Thanks- Using a function makes better sense than accessing a variable (or const) directly. |
First Time VBA user- really easy question :-)
On Jun 18, 12:02 pm, "Chip Pearson" wrote:
You can't directly read the version number from a worksheet cell. You need a function in a module (create from "Insert" menu in VBA) that will read the constant and return its value to the worksheet cell. E.g., Public Function GetVersion() GetVersion = ProgramVersionNumber End Function Then, you can call this from a worksheet cell with =GetVersion() Ok- I've tried this, but the spreadsheet still shows #NAME? Public Function GetVersion() GetVersion = ProgramVersionNumber End Function |
First Time VBA user- really easy question :-)
The code must be in a regular code module (create from Insert menu in VBA)
in the same workbook as the sheet from which you are calling the function. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting www.cpearson.com (email on the web site) "cappy2112" wrote in message ps.com... On Jun 18, 12:02 pm, "Chip Pearson" wrote: You can't directly read the version number from a worksheet cell. You need a function in a module (create from "Insert" menu in VBA) that will read the constant and return its value to the worksheet cell. E.g., Public Function GetVersion() GetVersion = ProgramVersionNumber End Function Then, you can call this from a worksheet cell with =GetVersion() Ok- I've tried this, but the spreadsheet still shows #NAME? Public Function GetVersion() GetVersion = ProgramVersionNumber End Function |
First Time VBA user- really easy question :-)
On Jun 18, 12:32 pm, "Chip Pearson" wrote:
The code must be in a regular code module (create from Insert menu in VBA) in the same workbook as the sheet from which you are calling the function. Not sure I understand what is different from what exists now, and what you are suggesting. There is already existing VBA code associated with this workbook- specifically, the worksheet in question. That is- when I double click on a command button on that worksheet, the VBA event handler is displayed. Is this relationship between workbook/worksheet & VBA code what your referring to ? |
First Time VBA user- really easy question :-)
Do NOT put the code in the code module associated with a worksheet. Re-read
my replies. You need to CREATE A NEW MODULE. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting www.cpearson.com (email on the web site) "cappy2112" wrote in message ups.com... On Jun 18, 12:32 pm, "Chip Pearson" wrote: The code must be in a regular code module (create from Insert menu in VBA) in the same workbook as the sheet from which you are calling the function. Not sure I understand what is different from what exists now, and what you are suggesting. There is already existing VBA code associated with this workbook- specifically, the worksheet in question. That is- when I double click on a command button on that worksheet, the VBA event handler is displayed. Is this relationship between workbook/worksheet & VBA code what your referring to ? |
First Time VBA user- really easy question :-)
I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. |
First Time VBA user- really easy question :-)
The code for the UDF belongs in a General module if you want to call it from a
formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. cappy2112 wrote: I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. -- Dave Peterson |
First Time VBA user- really easy question :-)
On Jun 18, 1:40 pm, Dave Peterson wrote:
The code for the UDF belongs in a General module if you want to call it from a formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. I'd rather have all of the code in the same module, and delete the code from the worksheet. I'm pretty sure it can be deleted and moved. It's only a few functions. |
First Time VBA user- really easy question :-)
Well, moving all the code to the .BAS module doesn't work, like I
expected. The code in the .BAS module isn't being called at all. First of all, I don't know where that file is saved. The VB editor doesn't allow me to Save As, so it's not in the directory where the spreadhseet is. So I've got to reorganize the project somehow. On Jun 18, 1:40 pm, Dave Peterson wrote: The code for the UDF belongs in a General module if you want to call it from a formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. cappy2112 wrote: I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. -- Dave Peterson |
First Time VBA user- really easy question :-)
Well, moving all the code to the .BAS module doesn't work, like I
expected. The code in the .BAS module isn't being called at all. First of all, I don't know where that file is saved. The VB editor doesn't allow me to Save As, so it's not in the directory where the spreadhseet is. So I've got to reorganize the project somehow. On Jun 18, 1:40 pm, Dave Peterson wrote: The code for the UDF belongs in a General module if you want to call it from a formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. cappy2112 wrote: I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. -- Dave Peterson |
First Time VBA user- really easy question :-)
If the code is all associated with UDFs called from a worksheet, then all the
code belongs in a General module. Select your project (in the VBE) Insert|Module Then back to the worksheet's code module. ctrl-a (to select all the code) ctrl-c (to copy all the code) Back to the new general module. ctrl-v (to paste the code) You don't need to export any code to a .BAS file (unless you wanted to back it up into a text file). cappy2112 wrote: Well, moving all the code to the .BAS module doesn't work, like I expected. The code in the .BAS module isn't being called at all. First of all, I don't know where that file is saved. The VB editor doesn't allow me to Save As, so it's not in the directory where the spreadhseet is. So I've got to reorganize the project somehow. On Jun 18, 1:40 pm, Dave Peterson wrote: The code for the UDF belongs in a General module if you want to call it from a formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. cappy2112 wrote: I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. -- Dave Peterson -- Dave Peterson |
First Time VBA user- really easy question :-)
Instead of using ctrl-c to copy the original code, use ctrl-x to cut the code.
Then you don't have to go back to clean it up. Sorry, Dave Peterson wrote: If the code is all associated with UDFs called from a worksheet, then all the code belongs in a General module. Select your project (in the VBE) Insert|Module Then back to the worksheet's code module. ctrl-a (to select all the code) ctrl-c (to copy all the code) Back to the new general module. ctrl-v (to paste the code) You don't need to export any code to a .BAS file (unless you wanted to back it up into a text file). cappy2112 wrote: Well, moving all the code to the .BAS module doesn't work, like I expected. The code in the .BAS module isn't being called at all. First of all, I don't know where that file is saved. The VB editor doesn't allow me to Save As, so it's not in the directory where the spreadhseet is. So I've got to reorganize the project somehow. On Jun 18, 1:40 pm, Dave Peterson wrote: The code for the UDF belongs in a General module if you want to call it from a formula in a cell. If you have that same code behind a worksheet or behind ThisWorkbook, it probably can be deleted with no problems--but you'll want to test. As for the rest of the code, I didn't see anything that would indicate that it belongs in one spot or another. So it would be difficult to guess. cappy2112 wrote: I understood your reply, however, that applies to the version number function I want to call from the Spreadsheet. Are you also suggesting that I put the existing code (the code that was in place before I started maintaining this project) in a new module as well? On Jun 18, 12:51 pm, "Chip Pearson" wrote: Do NOT put the code in the code module associated with a worksheet. Re-read my replies. You need to CREATE A NEW MODULE. -- Dave Peterson -- Dave Peterson -- Dave Peterson |
First Time VBA user- really easy question :-)
If the code is all associated with UDFs called from a worksheet, then all the
code belongs in a General module. So I wrote a 1 line function based on Chips suggestion, and put that in the new general module. That works. I was able to call it from the spreadsheet. From the project point of view, that module file looked like a .BAS file in a VB6 project. So I thought it would be better to have all of the code in one file. When I moved the worksheet code to the module, the worksheet doesn't run (obviously). So back it goes (except for the 1line function which is actually called from the Worksheet. Now- the function called from the spreadsheet- it shows the string returned from the function in the general module. However, when I update the string in the module, the string displayed in the spreadsheet does not update, unless I delete the function call from the cell, save the worksheet, then past the function call in to the cell again. How can the function call be updated whenever the string in the module is actually updated? |
First Time VBA user- really easy question :-)
That's why you don't want two procedures/UDFs in your workbook's project.
You want the UDF code in a General module--including this line: Private Const ProgramVersionNumber As String = "1.1" Your code is still in just a single file. It's in the project of that .xls file. You can have multiple worksheets (each with its own code module). You can have multiple General modules (nice for organization of procedures). But unless you're really, really smart and organized, you don't want two procedures that share the same name. (I wouldn't do this even if I were really, really smart and organized!) ===== I'm not sure what you mean by the worksheet didn't run anymore when you moved the code. Maybe you moved code that's associated with events or controls from the control toolbox toolbar that are placed on that worksheet. It's still difficult to tell since you haven't shared that code. cappy2112 wrote: If the code is all associated with UDFs called from a worksheet, then all the code belongs in a General module. So I wrote a 1 line function based on Chips suggestion, and put that in the new general module. That works. I was able to call it from the spreadsheet. From the project point of view, that module file looked like a .BAS file in a VB6 project. So I thought it would be better to have all of the code in one file. When I moved the worksheet code to the module, the worksheet doesn't run (obviously). So back it goes (except for the 1line function which is actually called from the Worksheet. Now- the function called from the spreadsheet- it shows the string returned from the function in the general module. However, when I update the string in the module, the string displayed in the spreadsheet does not update, unless I delete the function call from the cell, save the worksheet, then past the function call in to the cell again. How can the function call be updated whenever the string in the module is actually updated? -- Dave Peterson |
First Time VBA user- really easy question :-)
Your code is still in just a single file. It's in the project of that .xls
file. You can have multiple worksheets (each with its own code module). You can have multiple General modules (nice for organization of procedures). But unless you're really, really smart and organized, you don't want two procedures that share the same name. I wasn't suggesting this. I'm not sure what you mean by the worksheet didn't run anymore when you moved the code. Well, ALl of the event handlers for the worksheet were moved to the general module, so what's left are no event handlers. I never got the reply from Chip about putting all of the code in the general module, or just the one for the version number, so I tried it both ways. Now I see that's wrong, and I'm on an even keel now. Maybe you moved code that's associated with events or controls from the control toolbox toolbar that are placed on that worksheet. Exactly. It's still difficult to tell since you haven't shared that code. It's code from my work, I can't post that. Yes, I know it makes it extremely hard to give advice when you can't see the code, but you've got me back on track. |
First Time VBA user- really easy question :-)
Good to hear that you've got it working.
As a general rule, I only put those worksheet events and code for the controls inside a worksheet's module. I sometimes break that rule if there is a sub/function that will only be called by one of those procedures. But if there's even the slightest chance that I'd use that sub/function elsewhere, I move it to a General module. cappy2112 wrote: Your code is still in just a single file. It's in the project of that .xls file. You can have multiple worksheets (each with its own code module). You can have multiple General modules (nice for organization of procedures). But unless you're really, really smart and organized, you don't want two procedures that share the same name. I wasn't suggesting this. I'm not sure what you mean by the worksheet didn't run anymore when you moved the code. Well, ALl of the event handlers for the worksheet were moved to the general module, so what's left are no event handlers. I never got the reply from Chip about putting all of the code in the general module, or just the one for the version number, so I tried it both ways. Now I see that's wrong, and I'm on an even keel now. Maybe you moved code that's associated with events or controls from the control toolbox toolbar that are placed on that worksheet. Exactly. It's still difficult to tell since you haven't shared that code. It's code from my work, I can't post that. Yes, I know it makes it extremely hard to give advice when you can't see the code, but you've got me back on track. -- Dave Peterson |
First Time VBA user- really easy question :-)
ps.
I don't think that Chip could guess what to do with that original code without seeing the code--or at least the procedure names. But Chip does have some very nice instructions on the kind of worksheet events that have to be under the worksheet: http://www.cpearson.com/excel/events.htm David McRitchie has some notes, too: http://www.mvps.org/dmcritchie/excel/event.htm cappy2112 wrote: Your code is still in just a single file. It's in the project of that .xls file. You can have multiple worksheets (each with its own code module). You can have multiple General modules (nice for organization of procedures). But unless you're really, really smart and organized, you don't want two procedures that share the same name. I wasn't suggesting this. I'm not sure what you mean by the worksheet didn't run anymore when you moved the code. Well, ALl of the event handlers for the worksheet were moved to the general module, so what's left are no event handlers. I never got the reply from Chip about putting all of the code in the general module, or just the one for the version number, so I tried it both ways. Now I see that's wrong, and I'm on an even keel now. Maybe you moved code that's associated with events or controls from the control toolbox toolbar that are placed on that worksheet. Exactly. It's still difficult to tell since you haven't shared that code. It's code from my work, I can't post that. Yes, I know it makes it extremely hard to give advice when you can't see the code, but you've got me back on track. -- Dave Peterson |
First Time VBA user- really easy question :-)
Good to hear that you've got it working.
Mostly.... The function that is called from the Worksheet- shows the string in the Worksheet that is returned from the function in the general module. However, when I update the string in the module, the string displayed in the spreadsheet does not update, unless I delete the function call from the worksheet cell, save the worksheet, then past the function call in to the cell again. How can the function call be updated whenever the string in the module is actually updated? |
First Time VBA user- really easy question :-)
If you still have that function (and the declaration line) in the worksheet
module, you should delete it. It's gonna do nothing but cause confusion later on. If your code in the worksheet (or any other module) needs that constant, then change this: Private Const ProgramVersionNumber As String = "1.1" to Public Const ProgramVersionNumber As String = "1.1" By making the constant Public, then any procedure/function in any module will be able to see it. (See Scope and visibility in VBA's help for more info.) And you'll have to force that cell to reevaluate to update. Just changing the code doesn't do this. From xl2003's Help: Change when and how formulas are calculated By default, Microsoft Excel automatically recalculates formulas when the cells that the formula depends on have changed. Calculate a worksheet or workbook now Press F9 Calculates formulas that have changed since the last calculation, and formulas dependent on them, in all open workbooks. If a workbook is set for automatic calculation, you do not need to press F9 for calculation. Press SHIFT+F9 Calculates formulas that have changed since the last calculation, and formulas dependent on them, in the active worksheet. Press CTRL+ALT+F9 Calculates all formulas in all open workbooks, regardless of whether they have changed since last time or not. Press CTRL+SHIFT+ALT+F9 Rechecks dependent formulas, and then calculates all formulas in all open workbooks, regardless of whether they have changed since last time or not. (The last one is not available in all versions of excel. I _think_ it was added in xl2002, but I don't recall for sure.) ===== One way to force all formulas to recalculate if you're having trouble. Select all the cells edit|Replace what: = (equal sign) with: = replace all Excel will see that you're "re-entering" each formula and will re-evaluate them. cappy2112 wrote: Good to hear that you've got it working. Mostly.... The function that is called from the Worksheet- shows the string in the Worksheet that is returned from the function in the general module. However, when I update the string in the module, the string displayed in the spreadsheet does not update, unless I delete the function call from the worksheet cell, save the worksheet, then past the function call in to the cell again. How can the function call be updated whenever the string in the module is actually updated? -- Dave Peterson |
First Time VBA user- really easy question :-)
And this line:
Public Const ProgramVersionNumber As String = "1.1" should only appear once--in that General module. Dave Peterson wrote: If you still have that function (and the declaration line) in the worksheet module, you should delete it. It's gonna do nothing but cause confusion later on. If your code in the worksheet (or any other module) needs that constant, then change this: Private Const ProgramVersionNumber As String = "1.1" to Public Const ProgramVersionNumber As String = "1.1" By making the constant Public, then any procedure/function in any module will be able to see it. (See Scope and visibility in VBA's help for more info.) And you'll have to force that cell to reevaluate to update. Just changing the code doesn't do this. From xl2003's Help: Change when and how formulas are calculated By default, Microsoft Excel automatically recalculates formulas when the cells that the formula depends on have changed. Calculate a worksheet or workbook now Press F9 Calculates formulas that have changed since the last calculation, and formulas dependent on them, in all open workbooks. If a workbook is set for automatic calculation, you do not need to press F9 for calculation. Press SHIFT+F9 Calculates formulas that have changed since the last calculation, and formulas dependent on them, in the active worksheet. Press CTRL+ALT+F9 Calculates all formulas in all open workbooks, regardless of whether they have changed since last time or not. Press CTRL+SHIFT+ALT+F9 Rechecks dependent formulas, and then calculates all formulas in all open workbooks, regardless of whether they have changed since last time or not. (The last one is not available in all versions of excel. I _think_ it was added in xl2002, but I don't recall for sure.) ===== One way to force all formulas to recalculate if you're having trouble. Select all the cells edit|Replace what: = (equal sign) with: = replace all Excel will see that you're "re-entering" each formula and will re-evaluate them. cappy2112 wrote: Good to hear that you've got it working. Mostly.... The function that is called from the Worksheet- shows the string in the Worksheet that is returned from the function in the general module. However, when I update the string in the module, the string displayed in the spreadsheet does not update, unless I delete the function call from the worksheet cell, save the worksheet, then past the function call in to the cell again. How can the function call be updated whenever the string in the module is actually updated? -- Dave Peterson -- Dave Peterson |
First Time VBA user- really easy question :-)
If you still have that function (and the declaration line) in the worksheet
module, you should delete it. It's gonna do nothing but cause confusion later on. I only have it in the general module If your code in the worksheet (or any other module) needs that constant, then change this: Private Const ProgramVersionNumber As String = "1.1" to Public Const ProgramVersionNumber As String = "1.1" Only the function which is called from the worksheet references the constant string, so leaving it private is ok. The function is public Calculate a worksheet or workbook now Press CTRL+SHIFT+ALT+F9 This is the only key sequence that will update the Version number in the Worksheet It's too bad that I can''t force recalculation from the code But, everything is working the way I need. Thank You |
First Time VBA user- really easy question :-)
You could always create another macro that recalculates everything
or even just show the immediate window in the VBE (ctrl-g). Then type this and hit enter: application.calculatefull (well, if .calculatefull is in the version of excel that you're using--I've lost track what you're running.) cappy2112 wrote: If you still have that function (and the declaration line) in the worksheet module, you should delete it. It's gonna do nothing but cause confusion later on. I only have it in the general module If your code in the worksheet (or any other module) needs that constant, then change this: Private Const ProgramVersionNumber As String = "1.1" to Public Const ProgramVersionNumber As String = "1.1" Only the function which is called from the worksheet references the constant string, so leaving it private is ok. The function is public Calculate a worksheet or workbook now Press CTRL+SHIFT+ALT+F9 This is the only key sequence that will update the Version number in the Worksheet It's too bad that I can''t force recalculation from the code But, everything is working the way I need. Thank You -- Dave Peterson |
First Time VBA user- really easy question :-)
or even just show the immediate window in the VBE (ctrl-g).
The people using the spreadsheet won't know how to use the Immediate Window, and it would be confusing for them. Then type this and hit enter: application.calculatefull I will just update the version string and manually do the Shift-Alt- Ctrl-F9 whenever I make code changes, before distributing the new version. (well, if .calculatefull is in the version of excel that you're using--I've lost track what you're running.) 2003 |
First Time VBA user- really easy question :-)
You're going to let the users update your UDF???
That sounds more confusing than learning about the immediate window. cappy2112 wrote: or even just show the immediate window in the VBE (ctrl-g). The people using the spreadsheet won't know how to use the Immediate Window, and it would be confusing for them. Then type this and hit enter: application.calculatefull I will just update the version string and manually do the Shift-Alt- Ctrl-F9 whenever I make code changes, before distributing the new version. (well, if .calculatefull is in the version of excel that you're using--I've lost track what you're running.) 2003 -- Dave Peterson |
All times are GMT +1. The time now is 08:43 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com