I'm having trouble modifying the time to a specific time in a cell with both date and time with VBA.
The cell is formatted as "d/mm/yyyy h:mm AM/PM" and I'm taking a cell with time only in it formatted "h:mm AM/PM" and trying to use this bit of code to update the time portion (t1 is used thoughout the rest of the sub..)
Public t1 As String
t1 = Range("H1").Value
Range("D3").Select
ActiveCell = DateValue(ActiveCell) & " " & TimeValue(t1)
This seems to work on and off.. 6AM for instance will update the time to 12:25, and 10AM flat-out wont work at all. I figure I'm just doing something wrong but I've searched up and down for this and it seems like it's not a common problem.. Does anyone have any better ideas?! This is driving me nuts
The value in cell H1 does change, so i do need to pull the value from that in one way or another.
You mentioned the cell formatting; note that the cell formatting doesn't matter when you are merely retrieving the value as in: dt = ActiveCell
Note also that it is possible to store a date as text =istext(H1) returns true. In this case changing the formatting won't make any difference. Or you can store it as a value in the usual fashion. =isNumber(H1) returns true. Either way won't make any difference when using DateValue("H1")
I suggest declaring dt and tm as dates so that if the assignment dt = Range("H1").Value succeeds you know you actually have a date. Then you won't get a mismatch error when adding the dateValue + timeValue.
Try this
Dim dt As Date
Dim tm as Date
dt = Range("H1").Value
tm = ActiveCell.value
Range("D3").value = DateValue(dt) + TimeValue(tm) 'returns a date/time as a value
Maybe something like this:
Sub Macro1()
t1 = Range("H1").Value
Range("D3").Select
ActiveCell.Value = ActiveCell + t1
End Sub
Related
I have two dates in cell "A1" (Start Date) and "B1" (End Date). I change the date to get the real figures between the dates using SUMIFS formula.
There is Date Column in the Sheet with Time.
So what i want is that whenever the "A1" (Start Date) changes its time should always be initial like 00:00:00 and when i change the date for "B1" (End Date) it should always be like 23:59:59 is there any way to achieve this using VBA.
Your help will be appreciated.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
With Range("B1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End If
Numeric time 23:59:59 is not equal to 0.9999999, it is 0.999988425925926
But use native date/time handling for this and avoid smart numbers:
Range("A1").Value = DateValue(Range("A1").Value)
Range("B1").Value = DateValue(Range("B1").Value) + TimeSerial(23, 59, 59)
I think this could work, if i understood your question correct.
It will make sure A1 is integer and make B1 integer then add 0.99999.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
Range("A1").value = cint(range("A1").value)
Range("B1").value = cint(range("B1").value) + 0.99999999
End if
Just make sure the cells is set up to display as date and time.
If I understand you correctly, you don't need to bother with the time portion of the dates or go throught the mechanics of trying to change it.
You just need to properly construct your SUMIFS formula.
Range to sum: sumRange
Dates in your table: myDates
When you enter a date in A1 or B1, it will be entered as if it were at the beginning of the day.
If your dates in myDates are just dates, you can use the formula:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<=" & endDate)
However, if your dates in myDates also include a time, then you can modify the formula slightly:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<" & endDate+1)
And you can use the 2nd formula in either instance
Edit: I just noticed you posting your formula as a comment
I assume you have entered JUST the date (no time) in I1 and J1
Your formula as posted will work UNLESS C:C includes times with the dates. If it does include times with the dates, then try:
=SUMIFS(D:D,E:E,"<>Pending for Payment",B:B,H2,C:C,">="&$I$1,C:C,"<"&$J$1+1)
By adding 1 to the date, we move it to the start of the day after; so we change the <= to just < and encompass the full range of dates you desire.
Apologies in advance as this is my first time posting something on this site and am not the best at explain issues.
I have a spread sheet, this has production data such as meters daily, meters monthly etc. These values are updated by adding TAGS from a PLC using Rockwell VantagePoint Excel add-in (if your unfamiliar with this it shouldn't matter this part is not what I am struggling with)
I need I way to copy data from one cell to another cell on the same sheet at month end. Basically the Meters monthly field needs to copied into another cell at the end of the month to record meters run for that month. The monthly meters run resets back to 0 at the end of the month.
Basically I need to copy the value in J7 into the corresponding month in W column at the end of that month. If it could ignore the year that would be advantageous as I don't need it to keep the old values and would mean I just need one column.
I have some experience at MS-Excel, also VBA but mainly in MS-Access never in MS-Excel. If answers could be explained as simply and hands on as possible it would be appreciated.
After Googling the issue I came across this formula and changed the ranges to fit my sheet but Excel doesn't like it saying it contains an error
=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0
Sorry again if I haven't explained myself properly.
If your workbook isn't guaranteed to be open at the end of each month I would update the value every time it gets opened, like(Should be placed in ThisWorkbook):
'Runs when you open the workbook
Private Sub Workbook_Open()
'Loops through U3 to the last used cell in that column
For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
'Applies the J7 value to the current month and exits the sub
If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
Next c
End Sub
Also, not that it matters but, I would apply the following formula in U3:U14 to always get the correct dates:
=EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
Okay, I'm still not super sure what the question is and I know more Access VBA than Excel VBA, but here's something that might help to find a solution.
You can make a check date function that returns a Boolean value:
Public Function EoMonthCheck() As Boolean
Dim eo_month As Date, today As Date
eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
today = Format(Now(), "yyyy-MM-dd")
If today = eo_month Then
EoMonthCheck = True
Else
EoMonthCheck = False
End If
End Function
And the,, to add a value to the "W" column, we might use something like this:
Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.
Dim i As Integer
''' Activate whatever sheet you want to work with
Worksheets("Sheet1").Activate
If EoMonthCheck() = True Then
''' Look up the bottom of the 'W' column and find the first non-empty cell
''' Add 1 to that cell to get you to the next cell (the first empty one).
i = Cells(Rows.Count, "W").End(xlUp).Row + 1
''' Set the value of that empty cell in the 'W' column to the value of 'J7'
''' which will happen after we evaluate whether it is the end of the month.
Cells(i, "W").Value = Range(target_cell).Value
End If
Then, you could maybe trigger that each time the workbook opens.
Could you please someone can help me on how to change the date formatting cause i use the following code and instead of today's date it gives me august date. I mean it takes the date of my laptop (08-sep-2015) and it returns on the excel cell (09-Aug-2015).
Private Sub UserForm_Initialize()
'testing variable
'Dim LValue As String
'LValue = Format(#4/17/2004#, "Long Date")
'close testing variable
Me.tbDate = Date
'fill combobox
For Each cell In [cartridges]
Me.cmbCartridges.AddItem cell
Next cell
End Sub
The part that i exclude form the code is a test i made for a solution i found over the internet.
Also the text box that i want to present the date has the following code
ssheet.Cells(nr, 1) = CDate(Me.tbDate)
I found this and sorted my issue. Now everything its working like a charm.
Me.tbDate = Format(Date, "dd/mm/yyyy")
I resolved my issue with this code. I intentionally formated the cell the wrong way (excel default) and then formated it as I needed.
For j = 0 To LISTBOXNAME.ListCount - 1
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "m/d/yyyy")
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "dd.mm.yyyy")
Next j
I'm sure I'm missing something simple.
I have a date that I then want to compare to a list of Bank Holidays, if the date is in the list then minus 1 day from the date and re-check the date from the start of the list.
I have this so far:
For Each bhday In Sheets("testsheet").Range("$A$2:$A$" & Cells(Rows.Count, "A").End(xlUp).Row)
If testday= bankhol Then
tesday= DateAdd("d", -1, testday)
'RESTART THE LOOP AND CHECKS
Else
'IS NOT A BANK HOL CONTINUE CHECK
End If
Next bhday
Obviously this doesnt start the loop at the first value, how do i go about this?
Thanks
If there is no reason why you cannot use the WORKDAY() excel function, then that would probably be an easier way to do it.
http://office.microsoft.com/en-gb/excel-help/workday-HP005209339.aspx
You can insert your date in a cell, then have a list of holidays. Then just use the workday function. Set 1 as the "days" parameter and if its a Holiday (or a weekend, which is useful too in many instances), then it will show a different result (need to tweak slightly to have it register the date before as "1" will be the first date AFTER your input date)
You can put the holidays database in a sheet, then get your date and use workbookfunction.match() surround with Error handler. Something like:
On Error Resume Next
value = WorksheetFunction.Match(testday, Sheets("BD").[A:B], 0)
On Error GoTo 0
EDIT:
Maybe it's easier if you put a formula in a cell. Example:
Set HelpCell = .Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1)
With HelpCell
.Formula = "=MATCH(" & bhday & ",Holiday_BD!A:A,0)"
testDay = .Value
.ClearContents
End With
or Just:
With Range("A1")
.Formula = "=MATCH(" & bhday & ",Holiday_BD!A:A,0)"
testDay = .Value
.ClearContents
End With
I have a cell containing a date ex. "05/11/09"
It is currently displayed as "11-MAY-09". How do I copy-paste or use VBA to get the string "11-MAY-09" into the cell next to it ( NOT "05/11/09")?
I can't figure it out other than piecing out the date pieces by itself.
Range("B1").Value = Range("A1").Text
Using the cell's .text instead of .value modifier will copy the text formatting instead of the raw date.
Chuck
I believe you can use the TEXT function to format a date value to your liking.
The format string of "dd-mmm-yy" would format "05/11/09" as "11-MAY-09".
Use the Format function.
Format("5/11/2009", "DD-MMM-YY")
This will return:
11-May-09
If case matters:
UCase(Format("5/11/2009", "DD-MMM-YY"))
returns:
11-MAY-09
"The same answer but with a different function (that has worked for me):
Public Function DisplayText(ByVal pRange As Range) As String
DisplayText = pRange.Text
End Function
Just use =DisplayText(A1). If you change the cell format this function will return the displayed text"
cc: alvaroc
How can I get the displayed value of a cell in MS Excel ( for text that was converted to dates)?
Try this:
Sub FormattedText()
Dim r As Range
On Error Resume Next
Set r = Application.InputBox(prompt:="Select cell", Type:=8)
If r.Count <> 1 Or r Is Nothing Then
Exit Sub
End If
On Error GoTo 0
ActiveCell = "'" & r.Text
End Sub
It will put text of a selected cell (prompted) in the active cell.
You should be able to right click on the cell and set the format as General. This will allow you to put something in without it being automatically formatted to something else.
To save yourself from copying and pasting you will also want to start by putting in the date you want and not formatting and then copying.
In VBA you can do this:
Range("B2") = Range("A2")
Range("B2").NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005
If you need to loop it then:
Range("B" & i) = Range("A"& i)
Range("B" & i).NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005
Another way to do it.
Low-tech but very easy way - paste it into Word, then copy back into Excel! Can take a while with a big file, however... buts works great for one-off uses!