To convert text to date in Excel 2010 - vba

I want to use VBA code to convert my data (date) from text to date.
For Each c In Range("B4:B" & Range("B" & Rows.Count).End(xlUp).Row)
c.Value = DateValue(c.Value)
Next c
This code is not working with blank cells in between. Showing runtime error (error in datatype).
And this code:
For Each c In Range("B4:B" & Range("B" & Rows.Count).End(xlUp).Row)
c.Value = CDate(c.Value)
Next c
is showing 12.00.00 AM for empty values.
Basic idea behind to convert all text to date in given range, when new data is pasted.
Please give suggestions. Thanks

You can try this as well : Isdate function checks if a value is date or not.
For Each c In Range("B4:B" & Range("B" & Rows.Count).End(xlUp).Row)
if Isdate(c.value) then
c.Value = CDate(c.Value)
Else End if
Next c

Here is a two line code (This doesn't require looping)
Sub Sample()
[A1:A20].NumberFormat = "DD/MM/YYYY"
[A1:A20] = [index(IF(A1:A20="","",DATEVALUE(A1:A20)),)]
End Sub
I am assuming that the range is from A1:A20 Please change as applicable.
If you want to understand what this code does then see the explanation that I have given Here
This is a combination of INDEX and =IF(A1="","",DATEVALUE(A1))

From what I can gather, you have a range of cells with data you wish to convert to dates, but some of the cells in that range are blank? What do you want to do with the blank cells?
If you want to ignore them, just add an IF statement to cater for them
For Each c In Range("B4:B" & Range("B" & Rows.Count).End(xlUp).Row)
If c.Value <> "" Then
c.Value = DateValue(c.Value)
End If
Next c

Related

For each or Named Range to populate ListBox

All I am trying to populate a listbox with a For Each loop which iterates through the rows. The for each loop is going through the items in a Named range (ProgramIDs).
The current code I am using is
If Len(ProjectInformation.Range("H2").Value) = 7 Then
Dim Lr As Long
Lr = Range("H1048576").End(xlUp).Row
For Each C In Range("H2:H" & Lr)
With Program_ListBox
.AddItem C.Value
End With
Next C
End If
I fear this is a very basic question however after researching the website / google I simply cannot get this simple task to function.
Any help would be appreciated.
There is no need to loop, you can pass the range as the source of the listbox
Program_ListBox.List = Range("H2:H" & Lr)
Range("H2:H" & Lr) references the cells on the ActiveSheet. You should always fully qualify your references.
With ProjectInformation
If Len(.Range("H2").Value) = 7 Then
For Each C In .Range("H2", .Range("H" & .Rows.Count).End(xlUp))
With Program_ListBox
.AddItem C.Value
End With
Next C
End If
End With
There is no need loop the cells to the add the values to the listbox. You can assign the Range().Value array directly to the Listbox.List array.
With ProjectInformation
If Len(.Range("H2").Value) = 7 Then
Program_ListBox.List = .Range("H2", .Range("H" & .Rows.Count).End(xlUp)).Value
End If
End With

Excel VBA - Making a Named Range based on a found cell

I am trying to figure out how to make a named range, based on a cell found using:
For Each Cell In ISBN_Range
If Cell.Value = ISBN Then
ISBN_Valid = True
ISBN_Found = Range("A" & Cell.RowIndex & ":E" & Cell.RowIndex)
ISBN_Found.Interior.ColorIndex = 6
Exit For
End If
Next Cell
This is not working, and I am not sure why, I haven't been able to find the answer elsewhere, sorry if this is really simple! I basically just want to make the row of data that the found cell exists in a named range. ISBN_Found is declared as a range much earlier in my code, so that is not the issue.
I figured out the answer with some help from the guys in the comments, final code looks like this:
For Each Cell In ISBN_Range
If Cell.Value = ISBN Then
ISBN_Valid = True
Set ISBN_Found = Range("A" & Cell.Row & ":E" & Cell.Row)
ISBN_Found.Interior.ColorIndex = 6
Exit For
End If
Next Cell
Just needed to delete "Index" from "Cell.RowIndex"

Excel VBA range select

I have a macro in which I need to select the range R2:last row in sheet. However the last row in my sheet might be blank in column R. At the moment I am using this
Dim t As Range
Set t = Range("R2", Range("R1000").End(xlUp))
For Each Cell In t
If IsEmpty(Cell) Then
Cell.Activate
ActiveCell.Value = "To Be Picked Up"
End If
Next
However if the last row has a blank in column R then it gets ignored. I am hoping to pull the range using column A, as the last row of data always has column A. So something like,
Dim t As Range
Set t = Range("R2", Range("A1000").End(xlUp).ActiveCell.Offset(0, 17))
For Each Cell In t
If IsEmpty(Cell) Then
Cell.Activate
ActiveCell.Value = "To Be Picked Up"
End If
Next
It seems so simple but I'm sure im missing something stupid. Any help or alternative methods would be helpful thank you.
This should do the trick in one line:
Sub SO()
Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).Value = "To Be Picked Up"
End Sub
But in answer to your question specifically
Set t = Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row)
The Range() method will accept a string as an argument to obtain a range.So we can build this string any way we want:
"A1000" '// A1000
"A" & 1000 '// A1000
"A" & "10" & "00" '// A1000
"A" & CStr(1001 - 1) '// A1000
"A" & Rows.Count will return A65536 or A1048576 depending on the type of worksheet.
Range("A1048576").End(xlUp) as you know, will retrieve the last cell in that area, or the first cell in the next area on the direction specified.
Range("A1048576").End(xlUp).Row will return the row number of that cell (let's say it's A1000 for argument's sake) so the return value is 1000.
"R2:R" & Range("A" & Rows.Count).End(xlUp).Row therefore makes the string R2:R1000.
So finally, Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row) is the same as Range("R2:R1000")

how to delete cell value if cell is containing special text

I want to delete the value of a cell with vba, if the cell contains the phrase "01.01.1970 01:00:00"
The cell should be empty afterwards.
This date is only in column C and can exist in many rows.
The sheet has a lot of rows with different dates.
I only found ways, how to delete an entire row or column, if a cell contains a specific phrase, but I only want to delete the cell value.
Can someone help me?
Look at the Range.Replace method. You will want to localize the cells to be examined to column C.
with activesheet '<-set this worksheet reference properly!
.columns(3).replace what:="01.01.1970 01:00:00", _
replacement:=vbnullstring, lookat:=xlwhole
end with
Try this: (Untested)
Dim RowCount as integer, i as integer
RowCount = Range("C" & .Rows.Count).End(xlUp).Row
For i = 1 to RowCount
If Range("C" & i).Value = "01.01.1970 01:00:00" Then
Range("C" & i).ClearContents
End If
Next i
I found a solution / workaround with the following code
'START: CONVERT UNIX TIMECODE
Range("D1").EntireColumn.Insert
lastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("D2").Formula = "=(C2/86400)+25569+(+1/24)"
Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)
Columns("D").NumberFormat = "DD.MM.YYYY HH:MM:SS"
Columns("D").Copy
Columns("D").PasteSpecial xlPasteValues
Columns("C").Delete
Range("C1").Value = "'Druckbeginn"
'END: CONVERT UNIX TIMECODE
'START: CHANGE FORMAT OF COLUMN D TO TEXT
Columns("C").NumberFormat = "#"
'END: CHANGE FORMAT OF COLUMN D TO TEXT
'START: DELETE SPECIFIC TEXT, THE NUMBER 25569.* IS FOR THE DATE AND EACH TIME FROM 01.01.1970
Columns("C").Replace "25569.*", "", xlPart
'END: DELETE SPECIFIC TEXT
'START: CHANGE FORMAT OF COLUMN D TO DATE AND TIME
Columns("C").NumberFormat = "DD.MM.YYYY HH:MM:SS"
'END: CHANGE FORMAT OF COLUMN D TO TEXT

excel VBA : how to skip blank cells between 2 cells that contain values?

I am working out a button that can auto sum value at column C that column A = column B
like the picture :
PIC:
I can only copy the value in column C (that the word in column A = column B) to column E so far.
the code
Private Sub CommandButton2_Click()
Dim i As Integer, q As Integer
q = 2
For i = 3 To 100
Range("E" & q).Value = Range("b" & 3).Value
If Range("B" & i).Value = "A-RDL1" And Range("c" & i).Value = "OPEN" Then
Range("E" & i).Value = Range("d" & i).Value
End If
Next i
End Sub
the question 1) is how can I skip the blanks E9 to E17, so the numbers can be continuous? (AFTER CLICK THE BOTTON)
question 2) is it possible to auto sum the Numbers in column E instead of show each?
Thanks a lot and sorry for my poor English...
1) Yes, you can skip those, just carry out a check in the cell value and compare to empty string: Range("").Value2 = "". I personally prefer to do it like this though, to avoid false positives: Len(Trim(Range("").Value2)) = 0.
2) Yes, you can do that. just declare an Integer variable or two and use that to carry out a running count of your values.