Solved

Make command button work as a toggle button vba

Posted on 2009-04-06
3
476 Views
Last Modified: 2013-11-25
I currently have a command button set with the code below.  I would like to make this command button more like a toggle button so that when you click it the first time it will produce the totals as it does now, but if you click it again it will remove the totals (or undo the command)..
Private Sub CommandButton1_Click()
 

Dim lrow As Long

lrow = Cells(65536, "D").End(xlUp).Row

Cells(lrow + 2, "I").Formula = "=sum(I16:I" & lrow & ")"

Cells(lrow + 2, "J").Formula = "=sum(J16:J" & lrow & ")"

Cells(lrow + 2, "K").Formula = "=sum(K16:K" & lrow & ")"

Cells(lrow + 2, "L").Formula = "=sum(L16:L" & lrow & ")"

Cells(lrow + 2, "M").Formula = "=sum(M16:M" & lrow & ")"

Cells(lrow + 2, "N").Formula = "=sum(N16:N" & lrow & ")"

Cells(lrow + 2, "O").Formula = "=sum(O16:O" & lrow & ")"

Cells(lrow + 2, "F") = "Total Hours"

Range("I" & lrow + 2 & ":O" & lrow + 2).Font.Bold = True

Cells(lrow + 2, "F").Font.Bold = True

End Sub

Open in new window

0
Comment
Question by:RRUTLAND5
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Steve Dubyo
ID: 24078922
There is a toggle button that you can insert instead of a command button.  When you use the toggle button you can check the true/false value property of the button in the click event, so your code would look something like this..
Private Sub ToggleButton1_Click()

    If ToggleButton1.Value Then

        'Toggle on code

        Dim lrow As Long

        lrow = Cells(65536, "D").End(xlUp).Row

        Cells(lrow + 2, "I").Formula = "=sum(I16:I" & lrow & ")"

        Cells(lrow + 2, "J").Formula = "=sum(J16:J" & lrow & ")"

        Cells(lrow + 2, "K").Formula = "=sum(K16:K" & lrow & ")"

        Cells(lrow + 2, "L").Formula = "=sum(L16:L" & lrow & ")"

        Cells(lrow + 2, "M").Formula = "=sum(M16:M" & lrow & ")"

        Cells(lrow + 2, "N").Formula = "=sum(N16:N" & lrow & ")"

        Cells(lrow + 2, "O").Formula = "=sum(O16:O" & lrow & ")"

        Cells(lrow + 2, "F") = "Total Hours"

        Range("I" & lrow + 2 & ":O" & lrow + 2).Font.Bold = True

        Cells(lrow + 2, "F").Font.Bold = True

    Else

        'Toggle Off code

        Dim lrow As Long

        lrow = Cells(65536, "D").End(xlUp).Row

        Cells(lrow + 2, "I").Formula = ""

        Cells(lrow + 2, "J").Formula = ""

        Cells(lrow + 2, "K").Formula = ""

        Cells(lrow + 2, "L").Formula = ""

        Cells(lrow + 2, "M").Formula = ""

        Cells(lrow + 2, "N").Formula = ""

        Cells(lrow + 2, "O").Formula = ""

        Cells(lrow + 2, "F") = ""

        Range("I" & lrow + 2 & ":O" & lrow + 2).Font.Bold = False

        Cells(lrow + 2, "F").Font.Bold = False

    End If

End Sub

Open in new window

0
 

Author Comment

by:RRUTLAND5
ID: 24079922
Thanks....I'm getting a "compile error: Duplicate declaration in current scope" at: lrow As Long in the Toggle Off Code
0
 
LVL 5

Accepted Solution

by:
Steve Dubyo earned 250 total points
ID: 24080652
Ah, yes I missed that as I copy/pasted your code.  Need to declare lrow just once, I've ammended the code below.
Private Sub ToggleButton1_Click()

    Dim lrow As Long

    If ToggleButton1.Value Then

        'Toggle on code

        lrow = Cells(65536, "D").End(xlUp).Row

        Cells(lrow + 2, "I").Formula = "=sum(I16:I" & lrow & ")"

        Cells(lrow + 2, "J").Formula = "=sum(J16:J" & lrow & ")"

        Cells(lrow + 2, "K").Formula = "=sum(K16:K" & lrow & ")"

        Cells(lrow + 2, "L").Formula = "=sum(L16:L" & lrow & ")"

        Cells(lrow + 2, "M").Formula = "=sum(M16:M" & lrow & ")"

        Cells(lrow + 2, "N").Formula = "=sum(N16:N" & lrow & ")"

        Cells(lrow + 2, "O").Formula = "=sum(O16:O" & lrow & ")"

        Cells(lrow + 2, "F") = "Total Hours"

        Range("I" & lrow + 2 & ":O" & lrow + 2).Font.Bold = True

        Cells(lrow + 2, "F").Font.Bold = True

    Else

        'Toggle Off code

        lrow = Cells(65536, "D").End(xlUp).Row

        Cells(lrow + 2, "I").Formula = ""

        Cells(lrow + 2, "J").Formula = ""

        Cells(lrow + 2, "K").Formula = ""

        Cells(lrow + 2, "L").Formula = ""

        Cells(lrow + 2, "M").Formula = ""

        Cells(lrow + 2, "N").Formula = ""

        Cells(lrow + 2, "O").Formula = ""

        Cells(lrow + 2, "F") = ""

        Range("I" & lrow + 2 & ":O" & lrow + 2).Font.Bold = False

        Cells(lrow + 2, "F").Font.Bold = False

    End If

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now