You could add a break start time and a break end time and figure out which time period it belongs in rather than just saying that there was a 1 hour break somewhere in the shift.
Posts by shknbk2
-
-
I think what you are doing wrong is confusing Time decimals with regular math decimals. What do I mean? A half an hour is 30 minutes in Time, while a half is 0.5 in regular math. The 8.3 you got above is correct because you formatted the text to show the hour (8) followed by the minutes (30) separated by a period (the cell formatting was probably to show 1 decimal place; thus, the 8.3 rather than 8.30). To correct for this, you would start needing to separate the hour calculations from the minutes so that you could divide the minutes by 60 to get the correct math decimal value.
However, that is more complicated than it needs to be (I actually started down that road and got the formulas to work, but they were over-complicated).
Try these revised formulas instead. I assume you want the math decimal values so that 7.5 hours means that you worked from 8AM to 3:30PM, for example.
This formula calculates the overtime hours for column H. This adds all time before 7AM to all of the time after 4PM.
This formula for column G adds all hours between Start and End and then subtracts the overtime hours and break hours (i.e., "-(E6+H6)*TIME(1,0,0)").
All of the math is done by adding/subtracting Times so that the result is a decimal value equivalent to the correct percentage of a 24-hour day; thus, the multiplaction by 24 to give a math value of the number of hours. For example, the result of the Normal Hours for 0400 is 0.03125, which multiplied by 24 gives the 0.75 hour result (or 45 minutes if you want to think of it in Time),
-
-
Re: clear cell contents based on date in another column
You could use something like the following code to delete the points on the calendar day that you run it. However, looking at your code, I couldn't figure out where you would want it to occur.
When do you want it to clear the day's points: when you run a macro or some other time?
CodeSub ClearPoints() Dim c As Range, d As Long d = Now() - 0.5 On Error Resume Next Set c = Cells.Find(CDate(d), Cells(1, 1)) If Not c Is Nothing Then c.Offset(0, 2).Value = "" End If End Sub
Also, the dates for December are wrong. Day 1's date is 1/1/1900, not 12/1/2016. That matters for this code above that uses today's date.
-
Re: Convert Array of Cell Addresses To Range Without Loop
If you can get the list of addresses as a comma-separated string, you can use the following function to return the optimized string. It's a little ironic that I used 3 For loops when you asked for not looping through each one!
Code
Display MoreSub test() Debug.Print groupArray("$A$1,$A$2,$A$3,$A$7,$A$9,$A$10,$A$11") End Sub Function groupArray(list As String) As String Dim a As Variant, b() As String, c As Variant Dim i As Integer, s As String a = Split(list, ",") ReDim b(UBound(a)) For i = LBound(a) To UBound(a) c = Split(a(i), "$") b(i) = c(2) Next i For i = LBound(a) + 2 To UBound(a) If b(i - 1) = b(i - 2) + 1 And b(i - 1) = b(i) - 1 Then a(i - 1) = ":" End If Next s = a(LBound(a)) For i = LBound(a) + 1 To UBound(a) If a(i) = ":" And Right(s, 1) <> ":" Then s = s & ":" ElseIf a(i) <> ":" And Right(s, 1) = ":" Then s = s & a(i) ElseIf a(i) <> ":" And Right(s, 1) <> ":" Then s = s & "," & a(i) End If Next i groupArray = s End Function
-
Re: Worksheet Event_Array
Get rid of the parentheses of "arr()" in this line:
Change it to:
Also, you will want to stop the procedure from running in a loop every time you set the Target.Value to vbNullString. Substitute this line:
for this:
-
Re: Copy and paste is very slow VBA
Try this and see if it is any faster. It may not be, but give it a shot. I haven't tested it because I don't have a workbook set up the same way, but something similar worked on one of my workbooks.
Code
Display MoreSub ExcelToTally_V2() Dim r As Range Dim rTemp(6) As Range Set r = Sheet15.Range("D3") If r.Value > 0 Then Dim sh1 As Worksheet Set sh1 = ActiveWorkbook.Worksheets("V2") Windows("Vouchers - Purchase Transactions With StockItems.xlsm").Activate On Error Resume Next With sh1 Set rTemp(0) = .Range("C5:C54").SpecialCells(xlCellTypeVisible) Set rTemp(1) = .Range("E5:E54").SpecialCells(xlCellTypeVisible) Set rTemp(2) = .Range("F5:F54").SpecialCells(xlCellTypeVisible) Set rTemp(3) = .Range("G5:G54").SpecialCells(xlCellTypeVisible) Set rTemp(4) = .Range("H5:H54").SpecialCells(xlCellTypeVisible) Set rTemp(5) = .Range("I5:I54").SpecialCells(xlCellTypeVisible) pasteValues rTemp(0), "D" pasteValues rTemp(1), "F" pasteValues rTemp(2), "H" pasteValues rTemp(3), "J" pasteValues rTemp(4), "E" pasteValues rTemp(5), "C" End With Else Exit Sub End If End Sub Sub pasteValues(r As Range, s As String) Dim i As Integer, c As Range, destination As Range i = 0 Set destination = Range(r & Rows.Count).End(xlUp).Offset(1, 0) For Each c In r destination.Offset(i, 0).Value = c.Value i = i + 1 Next End Sub
-
Re: Format Row Color based on cell then change color
No need to use a macro. You could do something like this with an extra data column (C in the sample workbook, D in your picture) keeping track of the changes together with Conditional Formatting based on that column.
forum.ozgrid.com/index.php?attachment/70777/
You'll need to find a way to extend the formatting and column formulas as the table gets longer . . . perhaps by formatting the data as a table.
-
Re: Allow user to choose which text file to import
Refer to this link to find one way of choosing files in Excel. Modify it for your needs. Do this at the beginning of the macro where you can also handle what happens if no file or the cancel button is selected.
In your code, change the Connection text from
to something like
or whatever variable you set as receiving the selected file name. Make sure the variable has the whole path. -
-
Re: Basic String Manipulation question - Clearing contents of Range
Can you paste your whole code now?
-
Re: Count Unique Values
You're welcome. Glad it's working.
-
Re: Count Unique Values
Try this to make it a range:
Code
Display MoreSub RenameTabsV2() Dim Wb As Workbook Dim ws As Worksheet Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim rng4 As Range Dim rng5 As Range Dim LastRow As Long Set Wb = ActiveWorkbook 'Rename each sheet in workbook For Each ws In ActiveWorkbook.Worksheets Set rng1 = ws.Range("A:A").Find("Group Number") If Not rng1 Is Nothing Then ws.Name = rng1.Offset(1, 0).Value End If 'Identify if the data to count is in column E or C then identify the starting and stopping cells for the count On Error Resume Next If ws.CodeName = "Sheet1" Then Set rng2 = ws.Range("E:E").Find("Employee SSN", lookat:=xlPart) LastRow = ws.UsedRange.Rows.Count + ws.UsedRange.Rows(1).Row - 1 Set rng3 = ws.Range("E" & LastRow) Set rng4 = ws.Range(rng2.Offset(1, 0), rng3) Else Set rng2 = ws.Range("C5") LastRow = ws.UsedRange.Rows.Count + ws.UsedRange.Rows(1).Row - 1 Set rng3 = ws.Range("C" & LastRow) Set rng4 = ws.Range(rng2, rng3) End If ws.Range("A1") = countUniques(rng4) 'Temp value to use for testing/verifying the range on each sheet. To be replaced once the unique counts coding is functional. 'Range("A1") = "EE Count:" Range("B1") = Application.WorksheetFunction.SumProduct((rng4 <> "") / Application.WorksheetFunction.CountIf(rng4, rng4)) Next ws End Sub Function countUniques(filterRange As Range) As Integer Dim filterCell As Range Set filterCell = Cells(1, Columns.Count) filterRange.AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=filterCell, Unique:=True countUniques = filterCell.CurrentRegion.Rows.Count Columns(filterCell.Column).Delete Set filterCell = Nothing End Function
-
Re: Basic String Manipulation question - Clearing contents of Range
Glad to hear.
-
Re: Basic String Manipulation question - Clearing contents of Range
The manual calculation should have nothing to do with it. I wrote the code based on the string values you provided, but maybe the strings you are using in the actual code have a different format. Can you list what the string values are in the project?
-
Re: Basic String Manipulation question - Clearing contents of Range
What line doesn't work? I should mention that the code doesn't delete the rows just yet. Instead, I put in a Debug.Print to put the non-overlapping cell addresses listed there to double check that the code is working properly. You can see the output in the Immediate window (Ctrl+G from within VBE).
If you want to clear the cells, change
to
-
Re: VBA code in Macro to copy data from one sheet to another without pasting formulas
Sarah,
It does make sense. There are lots of websites dealing with questions on how to return a truly empty cell with no direct solution, only workarounds.
So, in a workaround for your case, some additional code could be added to clear the blank cells after pasting the data.
Code
Display MoreSub Updatebutton() Dim copyTarget As Range, i As Integer, j As Integer Sheets("Team Member 1").Range("B18:G50", "I18:o50").Copy With Sheets("Team member 1 - Detail") Set copyTarget = .Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0) copyTarget.PasteSpecial Paste:=xlPasteFormats copyTarget.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False Application.CutCopyMode = False j = .Cells(.Rows.Count, 1).End(xlUp).Row + 1 For i = j - 1 To copyTarget.Row Step -1 If .Cells(i, 1).Value <> "" Then Exit For End If Next i .Rows(i + 1 & ":" & j).Delete End With End Sub
-
Re: Excel UserForm Object Required error 424
In my experience, when the code stops running and highlights the .Show line, the error is actually in the form code. You can try to step through the code to see where the problem is. Place a breakpoint (F9) on the .Show line and step through (F8) until you get the 424 error within the form code.
-
Re: Count Unique Values
Try this. The function creates an AdvancedFilter copy of just the unique values of rng4 in an out of the way place (the right-most column of the worksheet), counts the values, and then clears the data before returning the number back to Range("A1").
Code
Display MoreSub RenameTabsV2() Dim Wb As Workbook Dim ws As Worksheet Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim rng4 As Variant Dim rng5 As Range Dim LastRow As Long Set Wb = ActiveWorkbook 'Rename each sheet in workbook For Each ws In ActiveWorkbook.Worksheets Set rng1 = ws.Range("A:A").Find("Group Number") If Not rng1 Is Nothing Then ws.Name = rng1.Offset(1, 0).Value End If 'Identify if the data to count is in column E or C then identify the starting and stopping cells for the count On Error Resume Next If ws.CodeName = "Sheet1" Then Set rng2 = ws.Range("E:E").Find("Employee SSN", lookat:=xlPart) LastRow = ws.UsedRange.Rows.Count + ws.UsedRange.Rows(1).Row - 1 Set rng3 = ws.Range("E" & LastRow) rng4 = rng2.Offset(1, 0).Address & ":" & rng3.Address Else Set rng2 = ws.Range("C5") LastRow = ws.UsedRange.Rows.Count + ws.UsedRange.Rows(1).Row - 1 Set rng3 = ws.Range("C" & LastRow) rng4 = rng2.Address & ":" & rng3.Address End If ws.Range("A1") = countUniques(rng4) 'Temp value to use for testing/verifying the range on each sheet. To be replaced once the unique counts coding is functional. 'Range("A1") = "EE Count:" Range("B1") = Application.WorksheetFunction.SumProduct((rng4 <> "") / Application.WorksheetFunction.CountIf(rng4, rng4)) Next ws End Sub Function countUniques(filterRange As Range) As Integer Dim filterCell As Range Set filterCell = Cells(1, Columns.Count) filterRange.AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=filterCell, Unique:=True countUniques = filterCell.CurrentRegion.Rows.Count Columns(filterCell.Column).Delete Set filterCell = Nothing End Function
The only change I made in your sub was
-
Re: Basic String Manipulation question - Clearing contents of Range
I'm not coming up with anything. For Each is just so darn handy in this situation. It's too bad there is a Not Intersect function directly.
It's not what you wanted, but this works:
CodeSub subtractRange2() range1address = "A12:A15,A17:D20" range2address = "A10:A13" For Each c In Range(range2address) If Intersect(c, Intersect(Range(range2address), Range(range1address))) Is Nothing Then Debug.Print c.Address End If Next End Sub
The only other way I can think of is using arrays, but that would use loops as well.