简体   繁体   中英

Assign a constant to a cell inside a function in excel macro

I had a button called "CheckAvailability" in one of the excel sheets. I wrote the below macro to populate the sheet "12. Sheet1". If "CheckAvailability" is clicked, then A11 in "12. Sheet1" will be populated with "NO", if not it will be populated with "YES". I had the below working code for the mentioned logic.

Private Sub Button_Click()
 If CheckAvailability.Value = True Then
  Sheets("12. Sheet1").Range("A11").Value = "NO"
 Else
  Sheets("12. Sheet1").Range("A11").Value = "YES"
 End If
End Sub

Due to the business reasons, we have removed the button "CheckAvailability", but just to keep it simple, I need to populate cell A11 in "12. Sheet1" with NA. So I made the below code change but it doesnt seem to work.

Private Sub Button_Click()
 If CheckAvailability.Value = True Then
  Sheets("12. Sheet1").Range("A11").Value = "NO"
 ElseIf CheckAvailability.Value = False Then
  Sheets("12. Sheet1").Range("A11").Value = "YES"
 Else
   Sheets("12. Sheet1").Range("A11").Value = "NA"
 End If
End Sub

Not sure whether I am doing anything not expected. Any suggestions would be appreciated.

If the button is gone then the only line you need is

Sheets("12. Sheet1").Range("A11").Value = "NA"

But, the only way to run the code would be to press F5 with the cursor inside the procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM