VBA to calculate and conditional format cell

johnny70

New Member
Joined
Dec 22, 2015
Messages
21
Currently using a direct formula in the excel sheet: B10=A10-A9 to get the cycle time between the two units
1710510535453.png

How do use Vba code for this so that each time a press the button the sub created also does this calculation?
Would also like it to conditional format the Cell ( in the example B10) so that the text is highlighted bold in red if value is larger then set time, Cell C2
Current code for click function looks like this
Public countunit As Integer

Public koll As Integer


Private Sub Click()

' function to secure continuing from last used cell if document is closed and repopened
Dim lRow As Long
lRow = Sheets("User Data").Cells(Rows.Count, "A").End(xlUp).Row
If lRow >= 8 Then
koll = 1
countunit = lRow + 1
End If

'click function and stepping to next cell
If koll = 1 Then
Worksheets("User Data").Protect Contents:=False
Worksheets("User Data").Cells(countunit, 1) = Format(Now, "mm/dd/yyyy HH:mm:ss")
countunit = countunit + 1
koll = 1

Cells(2, 4) = "Antal enheter"
Cells(3, 4) = countunit - 9

End If

Worksheets("User Data").Protect Contents:=True

End Sub
 

Attachments

  • 1710509804367.png
    1710509804367.png
    17.4 KB · Views: 20

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Is this what you meant? Please note that I didn't use CF, I just formatted the cell in column B if greater than C2:
VBA Code:
Public countunit As Integer
Public koll   As Integer
Private Sub Click()
    'function to secure continuing from last used cell if document is closed and reopened
    Dim lRow  As Long
    lRow = Sheets("User Data").Cells(Rows.Count, "A").End(xlUp).Row
    If lRow >= 8 Then
        koll = 1
        countunit = lRow + 1
    End If
    'click function and stepping to next cell
    If koll = 1 Then
        With Worksheets("User Data")
            .Protect Contents:=False
            .Cells(countunit, 1) = Format(Now, "mm/dd/yyyy HH:mm:ss")
            If countunit = 9 Then
                .Cells(countunit, 2) = "0:00"     'first line time (no formula)
            Else
                .Cells(countunit, 2).FormulaR1C1 = "=RC[-1]-R[-1]C[-1]" 'add formula
                .Cells(countunit, 2).NumberFormat = "h:mm;@" 'format time
                If .Cells(countunit, 2) > .Cells(2, "C") Then 'if greater than C2
                    .Cells(countunit, 2).Interior.ColorIndex = 3 'add color
                    .Cells(countunit, 2).Font.Bold = True 'add bold
                End If
            End If
        End With
        countunit = countunit + 1
        koll = 1
        Cells(2, 4) = "Antal enheter"
        Cells(3, 4) = countunit - 9
    End If
    Worksheets("User Data").Protect Contents:=True
End Sub
 
Upvote 1
Solution
Overall is seems to be working, comparing and marking if greater than set cycle time but the time format is not correct, Im not familiar to the code used NumberFormat "h:mm;@" , is it just to change to "hh:mm:ss;@" or "mm:ss;@"?
1710662204949.png

Should show 11 minutes and 7 sec
2024-03-17 08:40:26
2024-03-17 08:51:33
 

Attachments

  • 1710662023410.png
    1710662023410.png
    48.7 KB · Views: 20
Upvote 0
Yes, for the minutes and seconds in column B it would be "m:ss;@". You can use the Macro Recorder to have Excel create code snippets as suggestions.
I'm assuming the negative time I see in your last screenshot at line 11 is just due to testing, so it's not a problem.
 
Upvote 0
Yes, for the minutes and seconds in column B it would be "m:ss;@". You can use the Macro Recorder to have Excel create code snippets as suggestions.
I'm assuming the negative time I see in your last screenshot at line 11 is just due to testing, so it's not a problem.
Negative time was due to my calculation formula if second time was higher than cycle time in calculation the time went negative. But with your solution it is corrected and the formatting highlight if larger than set cycle time.
 
Upvote 0
Last issue is that if I want to reset the sheet, e.g. new test or so.
I have started with this code:
Private Sub Reset_Click()
countunit = 9
koll = 1
Worksheets("User Data").Protect Contents:=False
Worksheets("Background Data").Protect Contents:=False

Worksheets("User Data").Cells(8, 1) = "Tidpunkt för enhet till station"


Worksheets("User Data").Cells(3, 4) = 0
Worksheets("User Data").Range("A9:A800").ClearContents
Worksheets("User Data").Range("B9:B800").ClearContents
Worksheets("User Data").Range("B9:B800").FormatConditions.Delete
Worksheets("User Data").Range("C9:C800").ClearContents

'Worksheets("Data").Cells.ClearContents

Worksheets("User Data").Cells(8, 1) = "Tidpunkt för enhet till station"
Worksheets("User Data").Cells(2, 4) = "Antal Enheter"
'Worksheets("User Data").Cells(9, 1) = 0
Worksheets("Background Data").Cells(2, 6) = "Värde för radräknare"
Worksheets("Background Data").Cells(3, 6) = countunit
Worksheets("Background Data").Cells(4, 6) = "Uppdaterad"
Worksheets("Background Data").Cells(4, 6) = Format(Now, "mm/dd/yyyy HH:mm:ss")


Worksheets("User Data").Protect Contents:=True
Worksheets("Background Data").Protect Contents:=True

End Sub

Bur it still keep the formatting in cell that has been highlighted:
1710676242855.png


Not sure if this should be another tread or if Im allowed to place it in same
 

Attachments

  • 1710676226093.png
    1710676226093.png
    27.2 KB · Views: 17
Upvote 0
With some tweaks this should do the job:
VBA Code:
Private Sub Reset_Click()
    countunit = 9
    koll = 1
    With Worksheets("User Data")
        .Protect Contents:=False
        .Cells(8, 1) = "Tidpunkt för enhet till station"
        .Cells(2, 4) = "Antal Enheter"
        .Cells(3, 4) = 0
        .Range("A9:C800").ClearContents
        .Range("B9:B800").FormatConditions.Delete
        .Range("B9:B800").Interior.ColorIndex = xlNone
        .Protect Contents:=True
    End With
    With Worksheets("Background Data")
        .Protect Contents:=False
        .Cells(2, 6) = "Värde för radräknare"
        .Cells(3, 6) = countunit
        .Cells(4, 6) = "Uppdaterad"
        .Cells(5, 6) = Format(Now, "mm/dd/yyyy HH:mm:ss")
        .Protect Contents:=True
    End With
End Sub
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top