PDA

View Full Version : Revising A VBA Code



fa2020
05-25-2020, 12:36 AM
Hi, everyone
I have a code for summarizing personnel work time while it's sensitive to 8 hours shift, means if the user types work time for a personnel in a specific date which their cumulative work time exceeds 8 hours, this code omits it and just write 8 hours for that personnel in that date. This code was wrote by my friend but I want to develop it to my real world problem in excel. In the last command, you see there is SumIfs object which refers to limited rows. I want to develop it to all rows in those determined columns. So, if I write my data till row 15233 (my real data in huge), there is no need to make any change in the code. I don't know how to make such a change in my code. I'm new to VBA. Please help me to learn.

Logit
05-25-2020, 07:54 AM
.

Sub SummarizeData()Dim LR As Long, BR As Long


With ActiveSheet

BR = .Range("G" & .Rows.Count).End(xlUp).Row
If BR > 1 Then .Range("G1").CurrentRegion.ClearContents

LR = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:C" & LR).Copy .Range("G1")
BR = .Range("G" & .Rows.Count).End(xlUp).Row

.Range("G1:I" & BR).RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
BR = .Range("G" & .Rows.Count).End(xlUp).Row

.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("G2:G" & BR), Order:=xlAscending
.Sort.SortFields.Add Key:=Range("I2:I" & BR), Order:=xlAscending
With .Sort
.SetRange Range("G1:I" & BR)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

.Range("H2:H" & BR).FormulaR1C1 = "=MIN(8, SUMIFS(R2C2:R16C2, R2C1:R16C1,RC[-1], R2C3:R16C3,RC[1]))"
End With


End Sub