Macros - How can I update the active worksheet only?

josephss

New Member
Joined
Oct 7, 2011
Messages
2
I am using excel 2010.

I have a macro-based employee leave system that works by couting the number of days shaded with a certain colour and thus calculating leave days taken, remaining and entitled... I have a single workbook with multiple sheets for different employees.

However, when I update by using ctrl alt f9, it updates all the other worksheets (ignoring their shading) with the values of the active worksheet! So if I update Peter, who has taken 14 days so far, it will update Liam's sheet too, with 14 days, ignoring Liam's actual shaded days...

How can I update each sheet individually, without compromising the other sheets?

Excel is not responding to Shift - F9.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
So how does the macro itself work? Presumably the macro isn't initiated by Ctrl+Alt+F9? Unless is it triggered by a Calculate event? Can you post the code?
 
Upvote 0
Hi Richard.

Voila:


Sub Macro1()

End Sub

Public Function countColourCompare(rowN As Integer, startCol As Integer, endCol As Integer, colorIndexRange As Range, weekdayRow As Integer, halfFridays As Boolean) As Double
countColourCompare = 0
For curCol = startCol To endCol
If Cells(rowN, curCol).Interior.colorIndex = colorIndexRange.Interior.colorIndex Then
countColourCompare = countColourCompare + 1
If Cells(weekdayRow, curCol) = "F" And halfFridays Then countColourCompare = countColourCompare - 0.5
End If
Next curCol
'return countcolour
End Function
 
Upvote 0
OK with a UDF, the Ctrl+Alt+F9 will force a recalc of all the UDFs in the workbook. The problem with the function as written is that it makes a call to Cells(). This is an unqualified reference (doesn't have the parent sheet reference preceding it) so it refers to whatever is the Activesheet at the time. So the Function may well end up referring to a different sheet from that on which it is enetered.

I think the easiest way to correct this is to re-write the code to reference the sheet on which the calling function is held:

Rich (BB code):
Public Function countColourCompare(rowN As Integer, startCol As Integer, endCol As Integer, colorIndexRange As Range, weekdayRow As Integer, halfFridays As Boolean) As Double
Dim shtParent As Worksheet
Set shtParent = Application.Caller.Parent   'hold ref to parent sheet
countColourCompare = 0
For curCol = startCol To endCol
If shtParent.Cells(rowN, curCol).Interior.colorIndex = colorIndexRange.Interior.colorIndex Then
countColourCompare = countColourCompare + 1
If shtParent.Cells(weekdayRow, curCol) = "F" And halfFridays Then countColourCompare = countColourCompare - 0.5
End If
Next curCol
'return countcolour
End Function

Amendments are in red.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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