I am trying to update the date in the format of 20190715 (yyyymmdd) to column F under the header. My code seems to return hashes and I cannot understand why.
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("F2:F" & lastRow).Value = Format(Date, "yyyymmdd")
End Sub
All the cells populated with the current date in yyyymmdd
Does this do the job?
Range("F2:F" & lastRow).NumberFormat = "yyyymmdd;#"
Related
I am writing a macro for Excel VBA to remove old promotions from an excel sheet. The date also has the day of the week before the date. The code below is supposed to remove the day of the week, which because that's when reports come out is always Tue, evaluate if it is before today, then re-add Tue at the beginning. However, instead, it fails to delete Tue from older dates, resulting in older dates saying Tue Tue, then the date. I have tried five different ways of doing this and it has all failed in multiple ways.
For i = 50 To 150
Range("E" & i).Select
Range("E" & i).Replace What:="Tue ", Replacement:=""
If ActiveCell < Date Then
Rows(i).EntireRow.Delete
End If
Range("E" & i).Value = "Tue " & Range("E" & i).Value
Next i
When you delete a row all rows below it get pushed up. You need to either iterate backwards, delete all at once using a unionized range, or just put Range("E" & i).Value = "Tue " & Range("E" & i).Value into an else statement.
To delete the dates you'll want to convert their value to dates as they are currently strings looking like dates.
This one uses a unionized range:
Sub t()
Dim i As Long
Dim delrange As Range
For i = 50 To 150
Range("E" & i).Replace What:="Tue ", Replacement:=""
If CDate(Range("E" & i).Value) < Date Then
If Not delrange Is Nothing Then
Set delrange = Union(delrange, Rows(i).EntireRow)
Else
Set delrange = Rows(i).EntireRow
End If
Else
Range("E" & i).Value = "Tue " & Range("E" & i).Value
End If
Next i
delrange.Delete
End Sub
I also removed your Select and ActiveCell no need for those.
I am trying to populate the date and have it populate the column until the last row of data. However it doesn't seem to populate to the last line of data and I don't understand why. What is wrong with my code?
Sub updatedate()
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("F2:F" & lastRow) = Date
Range("F2:F" & lastRow).Style = "Normal"
Range("F2:F" & lastRow).Value = Format(Date, "yyyymmdd")
End Sub
the date in format yyyymmdd to be updated and populated on each row until last in column F
The problem is on the line
lastRow = Range("F" & Rows.Count).End(xlUp).Row
If you change "F" to "A" (or any other suitable column) it will work better.
The last used cell in the F column is not yet used.
I'm stuck with a problem and can't find a solution. I want to concatenate cells with two different formats. One cells has the format "tt:mm" and the other cell has format "general" how would I concatenate the cells? I have seen online that this can be done in Exel with something like
=A1 & TEXT(B1,"tt:mm")
or
=CONCATENATE(TEXT(A1;"tt:mm"); " ";B1)
But how is this done in VBA via a loop?
I have tried to just add the cells together but I the date is not returned in the format that I want.
Sub Merge()
Dim i As Long
Dim time As String
LastRow = Range("A" & StartRow).End(xlDown).Row
For i = StartRow To LastRow
Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value
Range("B" & i).NumberFormat = "tt:mm General"
End Sub
So if I run this code for xyz and 02:30 then I get 0,15625 xyz but I want to get 02:30 xyz.
Many thanks for any help.
Make this line:
Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value
Into:
Range("B" & i).Value = format(Range("B" & i).Value, "hh:mm") & " " & Range("A" & i).Value
This link has the different format strings: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications
Sub Concatenate ()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("K2").Formula = "= TODAY() - I2"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K" & LastRow)
For i = 2 To LastRow
If Range("K" & i).Value < 5 Then Range("J2:J" & i).Value = "Week of" & "" & ("I2:I" & i)
Next i
End Sub
I have a spreadsheet that lists item numbers in Column A, and corresponding dates in column I. Not every item will have a date, so I'm basing the LastRow on Column A to work around the gaps. I want dates in the past to return 0 in column J. I want future dates to return "Week of __" there the __ is the date in column I.
I'm not the most familiar with VBA, and I've run into a bit of a snag. With the above, everything returns "Week of9". I know it's a simple answer, but I have been Googling for an hour. I just need to know the syntax to make the above return the value of "I" at the end of the concatenate as it loops down the rows. If this is a duplicate question, I apologize.
Thanks in advance.
Maybe your condition should be:
If Range("K" & i).Value < 5 Then Range("J" & i).Value = "Week of " & Range("I" & i)
What I have:
Range("A" & i+1).value=Range("B" & i).value
What I Need:
"'" & Range("A" & i+1).value=Range("B" & i).value
The output I Need:
'1234
How do I add that type of text using VBA I'm trying to implement with other functions but it is giving me an error
You've got the order mixed up:
Range("A" & i+1).value = "'" & Range("B" & i).value
If you need to format given cell as a text, then this is a working solution:
Sub TestMe()
Range("A1").NumberFormat = "#"
Range("A1") = 1234
End Sub