How do you loop through the entire column and check if it is full year format or full date format and change every cell to full date format. For example, if a cell is 1832, how do you make it 1/1/1832 so the format is consistent throughout the column Q
Here is how you can do that.
Dim i As Integer
For i = 1 To Range("Q1").End(xlDown).Row
If Not IsDate(Cells(i, 17).Value) Then
Cells(i, 17).Value = "1/1/" & Cells(i, 17).Value
End If
Next i
IsDate function returns "true" if the passed value is a date. If the cell value is not a date, Concatenate "1/1" to the cell value. (17 represents column Q)
Related
In my workbook "isum", I have the week number figured out by a WEEKNUM formula (right now is week 27) listed on column X under the label Week#. The worksheet is called "Orders" with data to see what orders are late. I am struggling to create an if/then statement that makes it so that all of the week numbers on column X (starting at X2) that are < 27 (the current week number out of 52) are labeled as "Late". I am not sure how to change this value to the text, but the hard part is making sure that each week changes until it gets to 52. Otherwise nothing will change that is "Late". If this does not make sense let me know, but this is what I have so far:
isum.Sheets("Orders").Activate
Range("X2").Formula = "=WEEKNUM(RC[-9])"
Range("X2", "X" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
'Change statement to say "Late" and account for changing week numbers after every week
If cell.Value < 27 Then cell.Value = "Late"
Try looping through the range
Dim col As Range: Set col = Worksheets("Orders").Range("X2:X" & <current week num>)
Dim i As Integer
For i = 1 To col.Rows.Count
col.Cells(RowIndex:=i, ColumnIndex:="X").Value = "Late"
Next
(http://codevba.com/excel/for_each_cell_in_range.htm)
I would suggest a custom number format that displays Late for weeknums less than 27 but retains the underlying numerical weeknum value for use in future calculations. This can be applied through a conditional formatting rule that checks the weeknum formula's result against the current weeknum for dynamic results week to week.
With isum.workSheets("Orders")
With .Range(.Cells(2, "X"), .Cells(.Rows.Count, "O").End(xlUp).Offset(0, 9))
.Formula = "=weeknum(o2)"
.NumberFormat = "0_)"
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=x2<weeknum(today())")
.NumberFormat = "L\at\e_)"
'optionally apply a red fill color
'.interior.color = vbred
End With
End With
End With
I hope you can help. I have a small piece of code below. The issue I am having is that I am trying to subtract today's date from the date in Column C (see Pic 1) and then return a numerical result in Column D and then continue this formula down Column D until there is no values left in column C to subtract against.
So today's date is 09/03/2017 I want to subtract this date from the date in C2 03/07/2017 giving me 2 in D2 and then continue this through column D until C has a blank cell.
The piece of code that is bugging is Range("D2").Formula = DateDiff(C2, Date, "d")
The error I get is run time error 13 type mismatch.
The larger piece of code it belongs to is
Public Sub Activate_Sheet()
Worksheets("In Progress").Activate
Columns.AutoFit
Range("N:N").EntireColumn.Delete
Range("D1").Value = "# days open"
Range("D2").Formula = DateDiff(C2, Date, "d")
End Sub
As always any and all help is greatly appreciated.
Pic 1
Instead of
Range("D2").Formula = DateDiff(C2, Date, "d")
use
Range("D2").Formula = "=DAYS(TODAY(),C7)"
.Formula has to be a formula as you write it into a cell (for english Excel versions). If you have a non-english (localized) Excel version then you can use .FormulaLocal to write formulas in your localized language.
DateDiff function Parameters:
Interval in your case "d" (represnting days), is the first parameter, not the third.
You can't use C2 inside the DateDiff function, but you need to get the value from that cell by using Range("C2").Value.
Also, DateDiff will return a Numeric result in days, so you need to enter it in Range("D2").Value and not Formula.
Modify your code to:
Range("D2").Value = DateDiff("d", Range("C2").Value , Date)
Edit 1: To run this code for all occupied cells in Column C:
Dim LastRow As Long, i As Long
With Worksheets("In Progress")
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 2 To LastRow
.Range("D" & i).Value = DateDiff("d", .Range("C" & i).Value, Date)
Next i
End With
I have a csv file on excel that is ~CU for the column.
And the row gets keep updated (as of now it's 2606).
I'm trying to
delete all row that are before today's date as recorded on column D
no typing/text box/human input for today's date.
Sub deleterows()
lastrow = Cells(Rows.Count, 4).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 4).Value < *numeric value* Then Rows(i).EntireRow.Delete
Next i
End Sub
For dates (and currency), its always recommended to use Value2 instead of Value.
MSDN:
The only difference between this property and the Value property is
that the Value2 property doesn’t use the Currency and Date data types.
You can return values formatted with these data types as
floating-point numbers by using the Double data type.
So all you need to do is change this part If Cells(i, 4).Value < *numeric value* Then
With this If Cells(i, 4).Value2 < Date Then
and it evaluate as true if Column D is older than today.
I have a table I need to fill with some information for each client job according to the client job ID, and I pull the info using vlookup within VBA.
Dim jobID as double, path as String, creationDate as date
For i = 2 to 100
jobID = Sheet11.Cells(i, 1).Value
path = Application.WorksheetFunction.VLookup(jobID, Sheet9.Range("A:G"), 7, False)
creationDate = Application.WorksheetFunction.VLookup(jobID, Sheet9.Range("A:M"), 13, False)
Sheet11.Cells(i, 6).Value = creationDate
Sheet11.Cells(i, 8).Value = path
Next i
The paths in sheet 9 look like "180,28,60,91,40,178" or "179,143,141,142,178" and has "General" format. The creation dates in sheet 9 look like "2015-11-12 11:44" and has yyyy-mm-dd h:mm custom date format. However, when the stored values were entered into sheet11 as shown, some of the paths were converted to numbers e.g. "179,143,141,142,178" became "179143141142178", although some stayed as string as I intended e.g. "180,28,60,91,40,178". Most dates also lost time information (h:mm info) and was entered in sheet11 in yyyy-mm-dd format, while some maintained original format.
Could someone help me maintain all the information from the date and prevent the path stored as string to become a number?
If you do not want to format the cells then here is the simplest way to do it
Sheet11.Cells(i, 8).Value = "'" & path
Excel is trying its best to repair what it thinks are errors in your data.
A standard thousands separator puts a comma every third digit. This is a standard method of expressing numbers. 179,143,141,142,178 can be viewed as a real number.
When Excel sees a value like 180,28,60,91,40,178 it doesn't know what to do with it as the two digits separated by commas are not a standard method of expressing numerical values so it treats them as text.
If you have not manually changed the cell alignment, widen the column and look at the values. Text values by default are left aligned in a cell; numerical values and dates (which are considered numerical) are right aligned by default in a cell. This will quickly show you what Excel 'thinks' is a number and what it 'thinks' is text.
Format the target path as Text, not General. Format the target date to include a time value.
Dim jobID as double, path as String, creationDate as date
For i = 2 to 100
jobID = Sheet11.Cells(i, 1).Value
path = Application.WorksheetFunction.VLookup(jobID, Sheet9.Range("A:G"), 7, False)
creationDate = Application.WorksheetFunction.VLookup(jobID, Sheet9.Range("A:M"), 13, False)
Sheet11.Cells(i, 6).NUMBERFORMAT = "yyyy-mm-dd h:mm"
Sheet11.Cells(i, 6).Value = creationDate
Sheet11.Cells(i, 8).NUMBERFORMAT = "#"
Sheet11.Cells(i, 8).Value = path
Next i
Addendum:
It has occurred to me that your source values may be a mash-up[ of text and numbers. Locating the source row and using that to pull the Range.Text property may be a better fit.
Dim jobID as double, path as String, creationDate as date, rw as variant
For i = 2 to 100
jobID = Sheet11.Cells(i, 1).Value
rw = Application.Match(jobID, Sheet9.Columns("A"), 0)
if not iserror(rw) then
path = Sheet9.Cells(rw, "G").TEXT 'this will return whatever is displayed; not the underlying value
creationDate = Sheet9.Cells(rw, "M").VALUE2
Sheet11.Cells(i, 6).NUMBERFORMAT = "yyyy-mm-dd h:mm"
Sheet11.Cells(i, 6).Value = creationDate
Sheet11.Cells(i, 8).NUMBERFORMAT = "#"
Sheet11.Cells(i, 8).Value = path
end if
Next i
Working with excel-Access VBA environment. The vba code in the excel page assigns the value of a date cell to the date filter. Since I always work dd/mm/yyyy format with panama locale, the code works fine for dates after the 12th (meaning there's no ambiguity to designate the month), but for days les than 13, it converts the day to its numeric value and I get an error message saying 42502 (for may 12,2016 for example) is not a valid value for the filter. When the day passes the 12th, it works fine. How can i trap this error and solve it ?
Code :
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
' Range R1 contains the value we desire to filter the date of dynamic table
' Range B1 contains the date filter of the dynamic table
Range("B1").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("fecha").ClearAllFilters
Application.ScreenUpdating = False
'ActiveSheet.Range("B1") = Range("R1").Value
ff = Range("R1").Value
ActiveSheet.Range("B1") = Right("00" & Day(ff), 2) & "/" & Right("00" & Month(ff), 2) & "/" & Year(ff)
Application.ScreenUpdating = True
This is one of the many ways Ive tried to solve this, but only works for days after the 12th of the month
It would be helpful if you posted some code, but I believe the problem is you need to specify in code what format you are using instead of letting VBA guess it for you. Since Microsoft is a US company and we use the MM/DD/YYYY format, it could be defaulting to the US format, but then when it reaches the 13th, it defaults to the non-US format by the context of the date.
Try this:
Dim lastRow, i As Long
Dim datStg As String
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 2 To lastRow
dateStg = Cells(i, "R").Text
If datStg <> "" Then
Cells(i, "B").Value = DateSerial(Mid(dateStg, 8, 4), Mid(dateStg, 5, 2), Mid(dateStg, 2, 2))
Cells(i, "B").NumberFormat = "dd/mm/yyyy"
End If
Next i