简体   繁体   中英

How can i make it in excel vba with button?

"It's an easy,quick form to tell what i wish to do. It's not complete i know, i just wanted to show you how i imagine the inner workings of it in excel by this.(I asked one of my colleges to write it down quickly :) )

We only need 4 stated to be rotating by a button, as the following shows:

             Class State
           {
            enum RowStates {White:0,Red:1;Yellow:2,Green:3}
            //White is the basic state.Nothing has  //happened,only 
            recording
            //Red is failed
            //Yellow is problematic/under production
            //Green is all done.

             int states = 0;
             //state counter helper number
             RowStates CurrentState=(enum)states;

             Static Void ButtonPressed(event args stuff...)
             {
            If(states!=3)
            states++;
            //this makes the steps further
            //which results color change
            else
            states=0;
            //this resets the states to white
            }
            }

How can i make it in excel with macros?

I am not quite sure what you are after but you might have a look at the following code for a class State as your colleague wrote down

    Option Explicit

    Enum RowStates
        white = 0
        red = 1
        yellow = 2
        green = 3
    End Enum

    Dim states As Byte

    Property Get CurrentState() As RowStates
        CurrentState = states
    End Property

    Public Function ButtonPressed() As RowStates

        If states <> 3 Then
            states = states + 1
        Else
            states = 0
        End If

    End Function

It is a kind of "one by one" translation of your Java/C-code

EDIT Let's change the class State a little bit in order to switch color of a certain range in a sheet.

Option Explicit

Enum RowStates
    White = 0
    Red = 1
    Yellow = 2
    Green = 3
End Enum

Dim states As Byte

Public Function ButtonPressed() As RowStates

    If states <> 3 Then
        states = states + 1
    Else
        states = 0
    End If

End Function

Property Get Color() As Long

    Select Case states
    Case RowStates.White
        Color = vbWhite
    Case RowStates.Red
        Color = vbRed
    Case RowStates.Yellow
        Color = vbYellow
    Case RowStates.Green
        Color = vbGreen
    End Select

End Property

Then place a Active-X button on your sheet and add the following code to the sheet module

Option Explicit
Dim btnState As New state
Private Sub CommandButton1_Click()

    With btnState
        .ButtonPressed
        Range("A4:Q4").Interior.Color = .Color
    End With

End Sub

EDIT2 But the TO's request is IMHO easier to implement. Just place a button on Sheet1 and assign the following code to it. There is no real need for a class.

Option Explicit

Sub Button_Click()

Dim rg As Range
Static btnPressed As Byte

    Set rg = Sheet1.Range("A4:Q4")

    If btnPressed = 3 Then
        btnPressed = 0
    Else
        btnPressed = btnPressed + 1
    End If

    Select Case btnPressed
    Case 0
        rg.Interior.Color = vbWhite
    Case 1
        rg.Interior.Color = vbRed
    Case 2
        rg.Interior.Color = vbYellow
    Case 3
        rg.Interior.Color = vbGreen
    End Select

End Sub

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