Calculate Total Hrs spent given intime and outtime using Excel vba - excel

I have to calculate total hours spent
enter image description here
I have attached Excel(img) which shows the data format.
I have tried for some code as below"
For k = 2 To rowval:
InTime = Rows(k).Cells(1, 6).Value
OutTime = Rows(k).Cells(1, 7).Value
If InTime <> "" And OutTime <> "" Then
a = Val(InTime)
b = Val(OutTime)
time = b - a
For some values instead of getting 3.516666667 as output am getting 3 as output.
Can anybody please help me to get the appropriate output?

I don't think you need to use VBA for this calculation. You can just use a basic vba forumla in the Total Hrs Field as below.
=TEXT(G2-F2,"hh:mm")
If you want to do this in VBA then you can also use this formula in your VBA code with something like this:
time = TEXT(b-a,"hh:mm")
The trick here is including "hh:mm" which tells excel how to format the result.
Best

Related

VBA - How can I get all Mondays in a month then multiply to working hours?

I'm trying to figure out how to get the total of Mondays in a month then multiply by working hours.
This is my example code it works but it counts wrong output:
If UCase(val) Like "EVERY MONDAY" Then
Dim numString As Integer
Dim strDays() As String
Dim wordCount As Long
numString = 2
strDays = VBA.Split(val, " ")
wordCount = UBound(strDays)
strWhEveryDay = ThisWorkbook.Sheets("Input").Cells(X, 4).Value
strWhEveryDay = strWhEveryDay * var_month
Debug.Print "Every = " & strWhEveryDay
Explanation:
It depends on the user if what they like to input in a TEXTBOX. However, the CALCULATION it depends on the date where the user input in TEXTBOX.
I have Textbox which is the target month where the user input the format of date like this:
**Jan-2023 or Feb-2023 **
I have a table like this:
Place this text in a table start in Column B Row 2:
**every Monday**
Place this text in a table Column D Row 4:
**1.2**
All I need is to get all the total of Mondays based on the given month and year. The calculation of the days in a table of "every Monday" once I change the text from "every Monday" to "every Tuesday" so the calculation will adjust or automatically knows where the calculation days start to end:
Example of expected calculation: every Monday (January 2023 = 5 Days) * 1.2 so, the expected result will be 5.
Note: use Debug.Print to see the result or output
So, using networkdays.intl() as suggested in my comment:
NETWORKDAYS.INTL($A$3,$A$33,"0111111",)
The result shown is 5, which is correct by inspection, for cells A3:A33 the long date format was used.
So multiplying by 1.2 is:
NETWORKDAYS.INTL($A$3,$A$33,"0111111",)*1.2
and 5 * 1.2 = 6
Also, the string "0111111" can be put in cell F5 and referred to so it is easier to edit.
The easiest way I know to recognise any day of the week is the following:
=WEEKDAY(B2,2)
The "2" means that weekdays are counted, starting with "Monday" as 1, "Tuesday" as 2, ...
So, if you want to know if your date is a Monday, you can use this formula:
=IF(WEEKDAY(B2,2)=1,...)
This can easily be translated into VBA, using a standard IF-clause.

Add missing row with Macro/Excel or Matlab

I am really struggling with the simple task to write a Code to fill missing data in a measurement file. I've never coded before so it is quite difficult.
Problem description:
I have uploaded a picture with an example of the problem.
The source format is a .csv file with two columns, a timestamp (hh:mm:ss) and a value for each timestamp.
I created the row 'time value' in excel, which is displaying the timestamp as a number. (minute 1 = 1/1440 to minute 59 = 1439/1440).
In column D, I put the difference between two timesteps, which should be around 0.007 for a 1-minute step.
However, the problem is that some minutes are missing in the data throughout the column, sometimes only one minute and sometimes multiple.
Required Code
So what I need would be a Matlab Code, for instance, that would go through the timestamps or time values and identify missing minutes and write the values of its previous minute.
For example. Minute 6 missing? --> Write row for minute 6 and give it the value of minute 5.
I hope my problem is clear, otherwise, I am happy to explain more details.
--
Cheers
Alison :)
PS: The multiple files each have hundreds of thousands of data. That's why I would need an automated code :)
PICTURE:
I think you can do stuff like this :
Sub test()
lastrow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
If Format(TimeValue(CDate(Cells(i, 1).Value)) - TimeValue(CDate(Cells(i - 1, 1).Value)), "hh:mm:ss") > "00:01:00" Then
Rows(i).Insert
Cells(i, 1).Value = Format(TimeValue(CDate(Cells(i - 1, 1).Value) + "00:01:00"), "hh:mm:ss")
Cells(i, 2).Value = Cells(i - 1, 2).Value
End If
Next
End Sub
before
Output AFTER

How to use SUMPRODUCT using months and different criterion using Excel

I have a table that looks like the following:
Name Date Description
HallA 8/24/19 texttexttext
HallB 8/29/19 texttexttext
HallC 9/1/19 texttexttext
HallB 9/4/19 texttexttext
HallB 9/24/19 texttexttext
HallC 10/1/19 texttexttext
I would like to count how many times each Hall appears within each month.
This information should populate a table that looks like this.
Halls August September October .......
HallA 1 0 0
HallB 1 2 0
HallC 0 1 1
Additionally, it should be built to take more information.
I have been combining a solution to this from a number of resources, both of which are not correct.
=IF(SUMPRODUCT(--(MONTH($B$2:$B)=8)) - SUMPRODUCT(--($A:$A = "HallB")) - SUMPRODUCT(--($A:$A = "HallC")) > 0, SUMPRODUCT(--(MONTH($B2:$B)=8)) - SUMPRODUCT(--($A:$A = "HallB")) - SUMPRODUCT(--($A:$A = "HallC")), 0)
=SUMPRODUCT(((MONTH($B$2:$B)=8)))*($A:$A = "HallA")
I think I've been combining so many things to the point that I'm over complicating it...
Sometimes pivot table is a powerful tool in transforming data and here is one example:
Please make sure the dates are in Date format but not Text format otherwise pivot table will not be able to group the dates.
Cheers :)
Why do you need a sum product? I added a helper column with the months.
Month column = text(b2, "mmmm") filled all the way down.
Counter table = COUNTIFS($A$2:$A$7, F2, $C$2:$C$7, H$1)

subtract 5 minutes from formated time and reformat answer with seconds [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
VBA Excel: Add one second to a time
I have an Excel spreadsheet with the time value formated as Custom 00"00". I would like to subtract 5 minutes from this value and format the answer as hh:mm:ss using Excel VBA. I have looked all over but have not found an answer. I tried using various time functions but am not having much luck. I am a new Excel VBA programmer. Any suggestions would be appreciated.
If the Cell holds a real date-time-value, you can go with something like this:
Cells(r, c).Value = DateAdd("n", -5, Cells(r, c).Value)
Cells(r, c).NumberFormat = "hh:mm:ss"
I have no idea what custom format 00"00" is supposed to do, but since you say you have "...spreadsheet with the time value..." I assume the underlying data is a Date/Time serial number.
If this is the case then
Dim cl as Range
Set cl = Range("YourCellAddress")
cl = cl - 5# / 1440# ' subtract 5 minutes
cl.NumberFormat = "hh:mm:ss" ' format

Dates and Charting

I have three sets of data and each has two columns.
The first column is Date and second column is Price. The dates are formatted different with each data set eg. Gold's date(1951), Money Supply M2's date (1951-01), and Money Sup. M3 Date (9/01/1951)
What i need:
I want to chart these with dates on x-axis and price on y-axis
Questions:
do i need to make Gold's Date(YYYY) and Money Supply M2's Date(YYYY-MM) a date object?
if so how?
do i need to place all dates in one column and create a sub to help sort the Price with appropriate Date?
Does this make sense?
I have very little experience programming in VBA and was having a hard time finding info on using dates. I came across an Answer, given by the person who edited this (brettdj), that listed a few extremely helpful sights on VBA programming for excel. One of those had info on the DateSerial() function. I was then able to throw together some code to quickly fix my problem.
Sub CreateDates()
Dim dt As Date
Dim s As String
For Each c In Worksheets("Sheet1").Range("A7:A27080").Cells
s = Len(c.Value)
If s = 4 Then '(YYYY)
dt = DateSerial(s, 7, 1)
c.Value = dt
End If
If s = 7 Then '(YYYY-MM)
y = Left(s, 4)
m = Right(s, 2)
dt = DateSerial(y, m, 1)
c.Value = dt
End If
Next c
End Sub
I could probably spend some time and add some regular expressions and make a one-size fits most, but this will work as i want have to use it very often.

Resources