如何基于Excel中的单元格值启用或禁用按钮?

网友投稿 225 2023-12-26

如何基于Excel中的单元格值启用或禁用按钮?

如何基于Excel中的单元格值启用或禁用按钮?

本文,我将讨论一些基于Excel工作表中的单元格值启用或禁用按钮的方法。

使用VBA代码基于两个列数据启用或禁用两个按钮

使用VBA代码基于两个单元格值启用或禁用按钮

使用VBA代码基于两个列数据启用或禁用两个按钮
惊人的! 在 Excel 中使用高效的选项卡,如 Chrome、Firefox 和 Safari! 每天节省50%的时间,并减少数千次鼠标单击!

例如,我在工作表中有两个命令按钮,如果A列中有数据,则button1为启用,而button 2为禁用; 如果列C包含数据,则按钮2被启用,按钮1被禁用。 要解决此任务,以下VBA代码可能会为您提供帮助。

1。 右键单击包含按钮的工作表,然后选择 查看代码 从上下文菜单中,查看屏幕截图:

2。 在 Microsoft Visual Basic应用程序 窗口,请复制以下代码并将其粘贴到空白模块中:

VBA代码:基于两列启用或禁用两个按钮:

Private Sub Worksheet_Change(ByVal Target As Range) CommandButton1.Enabled = False CommandButton2.Enabled = False If Application.WorksheetFunction.CountA(Range("C:C")) > 0 Then CommandButton2.Enabled = True End If If Application.WorksheetFunction.CountA(Range("A:A")) > 0 Then CommandButton1.Enabled = True End If End Sub
Copy
Note: In the above code, CommandButton1 and CommandButton2 are the buttons names, A:A and C:C are the two columns that you use to enable or disable two buttons, please change them to your need. 3. Then save and close the code window, now, you will get the following result as you need: (1.) If type data in Column A, the button 1 is enabled and button 2 is disabled, see screenshot: (2.) If type data in Column C, the button 2 is enabled and button 1 is disabled, see screenshot: (3.) Both Column A and Column C have data, two buttons are all enabled, see screenshot: (4.) Two buttons are all disabled if there is no data in Column A and Column C, see screenshot: Enable or disable a button based on two cell values with VBA code If you want to enable or disable a command button based on two cell values, such as, when value in A1 is greater than value in B1, the button is enable, else, the button is disable. The following VBA code also can do you a favor. 1. Right click the sheet that contain the button, and then choose View Code from the context menu, in the Microsoft Visual Basic for Applications window, please copy and paste the below code into the blank module, see screenshot: VBA code: Enable or disable a button based on two cell values: Private Sub Worksheet_Change(ByVal Target As Range) Dim TargetAdd As String Dim xTemp As Double On Error GoTo ExitSub TargetAdd = Target.Address CommandButton1.Enabled = False If (TargetAdd = Range("A1").Address) Or (TargetAdd = Range("B1").Address) Then If Range("A1") > Range("B1") Then CommandButton1.Enabled = True End If End If ExitSub: End Sub Note: In the above code, A1 and B1 are the cell values that you use, CommanButton1 is the button name, change them to your own. 2. Then save and close the code window, now, if the cell value in A1 is greater than B1, the button is enabled, else, if the cell value in A1 is less than B1, the button is disabled, see screenshot: Best Office Productivity Tools Transform Hours into Minutes with Kutools for Excel! Ready to supercharge your Excel tasks? Harness the power of Kutools for Excel - your ultimate time-saving tool. Streamline intricate tasks and glide through your data like a pro. Experience Excel at lightning speed! Why You Need Kutools for Excel 🛠️ Over 300 Powerful Features: Kutools is packed with more than 300 advanced features, simplifying your work in over 1500 scenarios. 📈 Superior Data Processing: Merge cells, remove duplicates, and perform advanced data conversions – all without breaking a sweat! ⏱️ Efficient Batch Operations: Why put in extra effort when you can work smart? Import, export, combine, and tweak data in bulk with ease. 📊 Customizable Charts and Reports: Access a broad variety of additional charts and generate insightful reports that tell a story. 🗄️ Powerful Navigation Pane: Gain an advantage with the robust Column Manager, Worksheet Manager, and Custom Favorites. 📝 Seven Types of Drop-down Lists: Make data entry a breeze with drop-down lists of various features and types. 🎓 User-Friendly: A breeze for beginners and a powerful tool for experts. Download Now and Soar Through Time with Excel! Read More... Free Download... Purchase... Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project. Open and create multiple documents in new tabs of the same window, rather than in new windows. Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day! Read More... Free Download... Purchase...

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何在Excel中启用宏
下一篇:如何在Excel的数据透视表中启用显示详细信息?
相关文章