specific time in excel vba - vba

I am trying to clean my data set in excel. Currently, my code deletes data before 7 am and after 10 pm. how do I make it 7:30 am?
current code:
If Hour(.Cells(Cell, 2)) < 7 Or Hour(.Cells(Cell, 2)) > 22
I want to make the 7 to 7:30. How would I do this?

Depending on what is the format of the data in your Cells(Cell, 2), if it is as in the Nowfunction you could do this:
'Example with `Now`
If CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) < 730 Or _
Hour(.Cells(Cell, 2)) > 22 Then

Hour() will always return and integer. Try converting the time to a string: Format(Range("H1"), "hhnn") for the comparison.

One way:
if TimeValue(.Cells(Cell, 2)) > #7:30:00 AM# Or TimeVa...

Related

Calculating positive and negative numbers for past and future data

I am trying create a data set and finding it difficult to do it manually. I just wanted to know if there is way we can do it automatically in Excel.
As shown in the above image I need to do it 52 weeks. For e.g. 201703 ; 201704;201705...... 201752.
Any help in this regard will be greatly appreciated!!
Thanks,
Ganesh
I think you have already identify the pattern of WeekDiff, which always goes from 5 to -5.
Since you have data for 201701, based on them can generate data from 201702 onwards.
You can use below formulae for CRW, Snapweek and WeekDiff cells once inserting data for 201701.
Drag till you get preferred result.
Cell#A14 =IF(A2<>"",A2+1,"")
Cell#B14 =IF(NUMBERVALUE(RIGHT(B2,2))<52,IF(B2<>"",B2+1,""),B2+49)
Cell#C14 =IF(C2<>"",C2,"")
The formula for your weekdiff could be:
=(MIN(DATE(LEFT(A2,4)+1,1,0),DATE(LEFT(A2,4),1,1)-WEEKDAY(DATE(LEFT(A2,4),1,1),2)+RIGHT(A2,2)*7)-MIN(DATE(LEFT(B2,4)+1,1,0),DATE(LEFT(B2,4),1,1)-WEEKDAY(DATE(LEFT(B2,4),1,1),2)+RIGHT(B2,2)*7))/7
But I am not sure you also want the year-weeks be filled automatically, if so try this sub:
Sub tst()
Dim startweek As Long, i As Long, j As Long, k As Long, arr(1 To 624, 1 To 3)
startweek = 201701
For j = 1 To 52
For i = 1 To 12
k = k + 1
If i = 12 Then Exit For
arr(k, 1) = startweek: arr(k, 3) = 6 - i: arr(k, 2) = startweek - arr(k, 3)
If Val(Right(arr(k, 2), 2)) > 52 Then arr(k, 2) = arr(k, 2) - 48
Next i
startweek = startweek + 1
Next j
Sheet1.Cells(2, 1).Resize(UBound(arr), UBound(arr, 2)) = arr
End Sub
I did mine simultaneously with #Surani and got
In A2
=201700+INT((ROW()-2)/11)+1
In C2
=5-MOD(ROW()-2,11)
In B2
=IF((MOD(A2,100)-C2)<1,201652-(C2-MOD(A2,100)),IF((MOD(A2,100)-C2)>52,201800+(MOD(A2,100)-C2)-52,201700+MOD(A2,100)-C2))
but the other solution looks shorter!

VBA - How to remove string of characters in a range cell

This is the value of my date Mar 30 2016 4:46:34:256PM i want to remove the Time to make it like this Mar 30 2016.
I've tried various ways to format this if value with the format like mm-dd-yyyy , mm-dd-yyyy , yyyy-mm-dd etc. it's working but if tried this Mar 30 2016 4:46:34:256PM as value it's not working. can someone please tell me why?
I tried a simple code to test all the formats but it's not working with this value Mar 30 2016 4:46:34:256PM so i decided to remove the 4:46:34:256PM and that's how i got stock....
Sub formateDate()
Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Cells(i, 2).NumberFormat = ("dd-mm-yyyy")
Next i
End Sub
from your example you seem to have two consecutive spaces after the year number
should it always be so you could go like follows
Cells(i, 2) = Left(Cells(i, 2), InStr(Cells(i, 2), " ") - 1)
and then you can also assign it date formats since IsDate WorksheetFunction returns True if called on the resulting values

VBA Excel - Extract first 10 characters of entire column

I would like to extract the first 10 characters from all cells in an entire column (B-column).
The cells are dates on the format:
2014-09-03 08:00:00
And I want all of them to be like this:
2014-09-03
My code looks like:
Sub AdjustDate()
Dim NewDate As String
For Counter = 1 To 1000000
Set NewDate = Left(Cells(Counter, 2), 10)
Cells(Counter, 2).Value = NewDate
Next Counter
End Sub
I am no master at VBA and I cannot figure out what I do wrong..
Would be thankful for help!
Set is used when assigning an object. Since a string variable is not an object Set should either be replaced by Let or left out all together.
In your case you wouldn't need to define the 10 first characters as a string variable and then replace the cell with the value of that variable. You could instead just do:
For Counter = 1 To 1000000
Cells(Counter, 2).Value = Left(Cells(Counter, 2), 10)
Next Counter
I suspect this has to do with the formatting since the dates are typically stored as a number of days past Jan 1 1900 then formatted to be whatever you like
also agree with above, no need to use "set" in this situation w/o having an object to modify

Adding parts of a second to time

I am writing an excel VBA script, and I would like to add parts of a second to a time extracted from a cell.
I am using the time function to divide the content by 2, and then convert using the Time function, and add the result to the current time.
tt = Time(0, 0, Cells(1, 12) / 2)
I get a type mismatch error, as the result of the division is not an integer.
what I would like to do at the end is to perform the following addition.
newTime = timeCurrent + tt
Is there any alternative to the Time function to be used in this case ?
Consider:
Sub TimeOnMyHands()
Dim tt As Date
tt = TimeSerial(0, 0, CInt(Cells(1, 12).Value / 2))
MsgBox Format(tt, "hh:mm:ss.000")
End Sub
assuming the cell contains seconds as a simple number rather than a time value.

excel macro deleting extra rows

I have a macro that deletes the rows I don't want in excel. it deletes rows that have a time before 9:30 am and after 4:00 pm. For some reason, it also deletes the rows that have time of 10:00 am to 10:09 am, 11:00 am to 11:09 am, 12:00 pm to 12:09 pm, 1:00 pm to 1:09 pm, 2:00 pm to 2:09 pm, 3:00 pm to 3:09 pm
please help me so it does not delete those rows.
my code:
Sub DeleteRows()
Dim lastRow As Long
Dim Cell As Long
Dim dt As Date
'Work with the active sheet.
With ActiveSheet
'Find the last row of your dataset.
lastRow = .Range("A:A").Find("*", searchdirection:=xlPrevious).Row
'Format your time column to a readable time.
.Columns("B").NumberFormat = "[$-F400]h:mm:ss AM/PM"
'Loop through the rows, beginning at the bottom.
For Cell = lastRow To 2 Step -1
'Piece together the date.
dt = Mid(.Cells(Cell, 1), 5, 2) & "/" & _
Mid(.Cells(Cell, 1), 7, 2) & "/" & Left(.Cells(Cell, 1), 4)
'If the date is a Sat or Sun, delete the row.
If Weekday(dt) = 1 Or Weekday(dt) = 7 Then
.Rows(Cell).EntireRow.Delete
'If the time is before 9:30am or after 4pm, delete the row.
ElseIf CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) < 930 Or _
CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) > 1600 Then
.Rows(Cell).EntireRow.Delete
End If
Next Cell
End With
MsgBox "Done!"
You could try checking the value against an actual time:
'If the time is before 9:30am or after 4pm, delete the row.
ElseIf .Cells(Cell, 2) - Int(.Cells(Cell, 2)) < CDate("09:30:00") Or _
.Cells(Cell, 2) - Int (.Cells(Cell, 2)) > CDate("16:00:01") Then
.Rows(Cell).EntireRow.Delete
Note the upper bound 16:00:01 avoids a boundary evaluation error (probably due to rounding) at 16:00:00.
Instead of using
CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2)))
use
100*Hour(.Cells(Cell, 2)) + Minute(.Cells(Cell, 2))
You are hitting your problem because, for example, 11:08 is being converted into 11 hours and 8 minutes, which when concatenated together in your code yields 118 (which meets the deletion condition of being less than 930). The revised version (untested) should convert it to 1108 and avoid meeting the deletion condition.