Code doesn't work properly on active sheet

karangambhir

New Member
Joined
Jul 25, 2017
Messages
1
I have a code which I need to run on all the sheets in a Workbook except "All Employee" Sheet.
This code calculates the time spent by the employees in a shift and their less time (Time not completed in a shift) for each day of the month which is logged horizontally in the sheet.
The problem I am facing is that it shows the correct values on all the sheets except the one which is open when I run the macro.
Here Cell (2,69) contains the value of the number of hours which is supposed to be completed in a shift.
---------------------------------------------------------------------------------------------------------------------------------------------

Public Sub timing()Dim w As Integer, x As Integer, y As Integer, z As Integer, slr As Integer, WS_Count As Integer, S As Integer, lcol As Long


WS_Count = ActiveWorkbook.Worksheets.Count
For S = 2 To WS_Count
If Sheets(S).Name <> "All Employee" Then Sheets(S).Select Replace:=False
slr = Sheets(S).Range("A" & Rows.Count).End(xlUp).Row + 1
lcol = Sheets(S).Cells(1, Columns.Count).End(xlToLeft).Column + 1
For x = 4 To slr Step 2
Sheets(S).Cells(x, "F").FormulaR1C1 = "=R[-1]C[1]-R[-1]C"
Range("F" & x & ":G" & x).Select
Selection.AutoFill Destination:=Range("F" & x & ":BO" & x), Type:=xlFillDefault
Next x
Next S
For S = 2 To WS_Count
If Sheets(S).Name <> "All Employee" Then Sheets(S).Select Replace:=False
slr = Sheets(S).Range("A" & Rows.Count).End(xlUp).Row + 1
lcol = Sheets(S).Cells(1, Columns.Count).End(xlToLeft).Column + 1
For z = 4 To slr Step 2
For y = 7 To lcol Step 2
w = y - 1
If Sheets(S).Cells(2, 69).Value2 > Sheets(S).Cells(z, w).Value2 Then
Sheets(S).Cells(z, y) = Sheets(S).Cells(2, 69).Value2 - Sheets(S).Cells(z, w).Value2
Sheets(S).Cells(z, y).Font.ColorIndex = 3
Else
Sheets(S).Cells(z, y) = Sheets(S).Cells(z, w).Value2 - Sheets(S).Cells(2, 69).Value2
Sheets(S).Cells(z, y).Font.ColorIndex = 10
End If
Next y
Next z
Next S
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Are you intentionally skipping the first sheet in the workbook (based on position)? If not you should change:

Code:
For S = 2 To WS_Count
to
Code:
For S = 1 To WS_Count
 
Upvote 0
If Sheet 1 is active when you run the macro this line of code
Code:
If Sheets(S).Name <> "All Employee" Then Sheets(S).Select [COLOR=#0000ff]Replace:=False[/COLOR]
will mean that BOTH sheet 1 & sheet 2 are selected, but sheet 1 will be the active sheet.
hence this line
Code:
Range("F" & x & ":G" & x).Select
will refer to sheet 1 & not sheet 2.
I hope that this is clear.

I suspect that you need to remove the code in blue in the first part of this post
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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