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

Related

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

How to use the row number of a named cell in Excel VBA

I have a worksheet in which I want to use a button to add rows. I have multiple buttons on multiple rows which should each add a row at their location.
The problem is, that whenever a new row is added, the location at which I want to add a row changes. I have therefore named cells where I want to add new rows for each button.
I am having trouble getting the row number of a cell so I can choose where the new row is added. Atm I have tried this:
Sub Button2_Click()
Dim RowNum As Variant
Set RowNum = WorksheetFunction.Index(cell, 1, 0)
Rows(RowNum & ":" & RowNum).Insert Shift:=xlDown
Rows(RowNum - 1 & ":" & RowNum - 1).Copy Range("A" & RowNum)
Range("D" & 7, "L" & 7).ClearContents
End Sub
Where cell is a cell where I want to add a new row.
Im having trouble using the index function to return the row as a number. When I hover over "cell" in debug it says cell=empty.
Hope this is clear, as you can maybe tell Im not very experienced with this stuff.
Thanks :)
When you use Set to assign the variant to the returned Application.Index, you are actually setting a range. You can simplify this to simply return the row number of the cell but you have to refer to the named range as if it were a cell address in a Range object.
Sub Button2_Click()
Dim RowNum As Long
With ActiveSheet
RowNum = .Range("cell").Row
.Rows(RowNum).Insert Shift:=xlDown
.Rows(RowNum - 1).Copy .Range("A" & RowNum)
.Range("D" & 7, "L" & 7).ClearContents 'not really sure where the 7 comes in here
End With
End Sub
Try this to find the cell you named:
rowToInsertIn=Application.WorksheetFunction.Match("MyCellName", Range("A:A"), 0)

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.