![]() |
Newbie Question - Excel VBA
Have written some elementary VBA code in an excel spreadsheet with a
UserForm that shows on demand with some calculated output using some of spreadsheets cells as input. Please excuse my terminology, I am embarrased enough to try to even ask. The problem is, that I have a command button on the UserForm. I cannot seem to get the variables in the main "sheet1" or Worksheet(?) code to pass to the command button code for the UserForm (click event). How can I call them up to the UserForm CommandButton code please ? If this makes sense to anybody I'd appreciate a response. |
Newbie Question - Excel VBA
The click event of the command button does not accept arguments because it
is not normally called from another routine. If you want to establish some condition that the button's code will react to, you might declare a public variable (or more than one) at the top of the general module (not a sheet, workbook or userform module) outside any procedure. Public myvar as long then set the value for this variable from your routine. The click event can have code that looks at this variable Private Sub commandbutton1_Click() if myvar = 3 then msgbox "Myvar is 3" else msgbox "Myvar is not 3" end if End Sub -- Regards, Tom Ogilvy "Hambone" wrote in message om... Have written some elementary VBA code in an excel spreadsheet with a UserForm that shows on demand with some calculated output using some of spreadsheets cells as input. Please excuse my terminology, I am embarrased enough to try to even ask. The problem is, that I have a command button on the UserForm. I cannot seem to get the variables in the main "sheet1" or Worksheet(?) code to pass to the command button code for the UserForm (click event). How can I call them up to the UserForm CommandButton code please ? If this makes sense to anybody I'd appreciate a response. |
Newbie Question - Excel VBA
Thanks Tom that sorted it. I was trying to declare the public variable
inside a workbook module, and not in the general one... "Tom Ogilvy" wrote in message ... The click event of the command button does not accept arguments because it is not normally called from another routine. If you want to establish some condition that the button's code will react to, you might declare a public variable (or more than one) at the top of the general module (not a sheet, workbook or userform module) outside any procedure. Public myvar as long then set the value for this variable from your routine. The click event can have code that looks at this variable Private Sub commandbutton1_Click() if myvar = 3 then msgbox "Myvar is 3" else msgbox "Myvar is not 3" end if End Sub -- Regards, Tom Ogilvy "Hambone" wrote in message om... Have written some elementary VBA code in an excel spreadsheet with a UserForm that shows on demand with some calculated output using some of spreadsheets cells as input. Please excuse my terminology, I am embarrased enough to try to even ask. The problem is, that I have a command button on the UserForm. I cannot seem to get the variables in the main "sheet1" or Worksheet(?) code to pass to the command button code for the UserForm (click event). How can I call them up to the UserForm CommandButton code please ? If this makes sense to anybody I'd appreciate a response. |
All times are GMT +1. The time now is 05:33 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com