Pass a cell in sheet to macro or command button

C

Connie

Is there a way to pass a cell in a sheet to a macro or command button?
I have a spreadsheet that I created for a group of users which
ultimately creates a csv file to be uploaded to another system. The
users input various information and then the spreadsheet performs
calculations. I do not want to create the final csv file unless certain
conditions are met within the calculations. I have code to create the
final csv file (from a command button); however, I can't figure out how
to limit the creation of the file based on the parameters from the
spreadsheet. Any help would be appreciated.
 
N

Nigel

If you wish to prevent a commandbutton from being active, you can use

CommandButton1.Enabled = False

So in your sheet, you might add use a change event or the calculate event to
test if the cell has a specific value which will enable the command button.

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") = "123" Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub

Alternatively include the test in the macro that is initiated by the user
pressing the command button, this of course may not do anything and could
confuse the user....

'your sub
If Range("A1") <> "123" then Exit Sub
' your code
 
C

Connie

Nigel: Both options worked beautifully. I prefer the second option as
this way I can display a message box to let the user know why they
can't proceed. Here's the code I used:

Private Sub Test_Errors_Click()
If Range("TestErrors") = False Then
MsgBox "You have unresolved ERRORS. Please View the Error
Report and resolve all errors before proceeding."
Exit Sub
Else
My Code
End If
End Sub

Thanks so much for your help! I really appreciate it.
 

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

Top