how to delete cell value if cell is containing special text - vba

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

Related

Autofill to a specific date

I am working with date data and want to autofill to a specific date. The specific date will be stored as value X. X is the last value in column A
I've looked at some other posts here on StackOverflow, but the issue I keep running into is setting my final "value". My final value will not be a number, but a date to stop at.
With ThisWorkbook.Sheets("Form Date 2")
'Select this workbook
Dim X as Date
'I am not sure what I need to Dim this as
Range("B2").Value = Range("A2").Value
'B2 is the value I want to autofill down with
X = Range("A" & Rows.Count).End(xlUp).Value
'X will be the last value in column A
Range("B2").Autofill Destination:=Range("B2:B" & X), Type:=xlFillSeries
'Not sure what to set my Type as
End With
So the final result would be, Column B would obtain all dates from A1 to last value in A (02/04/2018-05/06/2018). Format should be dd/mm/yyyy.
In Column A I have a whole bunch of dates, but some are missing, so I am grabbing all the missing ones as well.
With the picture below I have the Dates column. I want VBA to spit out the All Dates column. So The code will copy and paste A2 over to B2 and then autofill until the last value in column A(not row). So in this example All Dates should continue down until value/date 01/06/2018.
Perhaps this will work:
With ThisWorkbook.Sheets("Sheet2")
'Select this workbook
Dim X As Integer
'I am not sure what I need to Dim this as
.Range("B2").Value = .Range("A2").Value
'B2 is the value I want to autofill down with
X = .Range("A" & .Rows.Count).End(xlUp).Row
'X will be the last value in column A
.Range("B2").Select
Selection.AutoFill Destination:=.Range("B2:B" & X), Type:=xlFillDefault
'Not sure what to set my Type as
End With
Updated Code to fill till the date on the last cell of Column A:
With ThisWorkbook.Sheets("Sheet2")
'Select this workbook
Dim X As Integer
'I am not sure what I need to Dim this as
.Range("B2").Value = .Range("A2").Value
'B2 is the value I want to autofill down with
X = .Range("A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value - .Range("B2").Value
'X will be the last value in column A
.Range("B2").Select
Selection.AutoFill Destination:=.Range("B2:B" & X + 2), Type:=xlFillDefault
'Not sure what to set my Type as
End With
This code works fine :
Sub test()
Dim X As Integer
ThisWorkbook.Sheets("Sheet2").Range("B2").Value = Range("A2").Value
'B2 is the value I want to autofill down with
X = ThisWorkbook.Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
'X will be the last value in column A
ThisWorkbook.Sheets("Sheet2").Range("B2").AutoFill Destination:=ThisWorkbook.Sheets("Sheet2").Range("B2:B" & X), Type:=xlFillDefault
End Sub
You're welcome :)

VBA Macro - I want to change each cell value to a new one in a specific range... how to do it? Excel

I have a range that is B3:B500
I want to change the value in each cell in that range
The range however, is dynamic so I need to look from B3 to last row
First:
How do I get it to change the range to work to last row rather than preset range?
Second:
I want to change each individual cell value to something like this:
myCell.Value = "=" & Chr(34) & Chr(61) & myCell.Value & Chr(34)
How do I get it to go through cell by cell to make change to each cell in the "dynamic" range we just created?
Appreciate all the help I can get... pretty new at VBA so please bear that in mind and keep it simple :)
I.e. Cell b3 contains: "ASP" (Text only)
Change to: ="="ASP" (formula instead giving result =ASP)
Dim lastRow As Integer: lastRow = 3
Dim myCell As Range
'determine last row of current region
Do While(Not IsEmpty(Cells(lastRow + 1, 2)))
lastRow = lastRow + 1
Loop
'loop through the range
For Each myCell In Range(Cells(2, 2), Cells(lastRow, 2))
myCell.Value = "=" & Chr(34) & Chr(61) & myCell.Value & Chr(34)
Next myCell
In this code, first you determine last row using While loop, which loops until the last row in column is found. Then use For Each loop, where you loop through the range, from third row to last row in B column.

To convert text to date in Excel 2010

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

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 Autofill dynamic column in VBA?

I have column K with a bunch of dates and I want to get the number of days between K's date and today's date inserted in column O.
My code for the first row is:
Range("O2").Value = Date - Range("K2").Value
How can I repeat this code for the rest of the column? Also, keep in mind that the length of populated cells in column K is dynamic and always changing.
Thanks to all that can help in advance!
before:
Sub Main()
Dim i As Long
For i = 1 To Range("K" & Rows.Count).End(xlUp).Row
Range("O" & i) = DateDiff("d", Now, Range("K" & i))
Next i
End Sub
after:
Range("O2").AutoFill Range("O2:O" & Cells(Rows.Count, "K").End(xlUp).Row)
That is if you have the formula already in cell O2.