Fill formula until the last row automatically - excel

IF(ISBLANK(B5),"",IF(ISBLANK(O5)=TRUE,"Missing PSD",TODAY()-O5))
This is my formula that calculates the difference between the date in column O and current date. My first filled row with values is 5. The row in which the formula calculation is being done is AC.
I want to automatically calculate this until the last filled row and the row values should also increment like it does while dragging down.
I am not good with VBA so any help would be highly appreciated.

Try the next code, please:
Sub testFilFormulaDown()
Dim sh As Worksheet, lastRow As Long
Set sh = ActiveSheet 'use here the necessary sheet
lastRow = sh.Range("O" & rows.count).End(xlUp).row 'chosen O:O column, being involved in the formula...
sh.Range("AC5:AC" & lastRow).Formula = "=IF(ISBLANK(B5),"""",IF(ISBLANK(O5)=TRUE,""Missing PSD"",TODAY()-O5))"
End Sub
In order to properly calculate the last row, you must choose a fully filled column (A:A, C:C etc.). I used one involved in the formula, but since there is a check for blank cells, column O:O could not be the most appropriate one...

Related

How to fo find the last row of a selected range

I would like to find the last row in a range that I have selected so that my code is more dynamic and less likely to break if I exceed the range.
I am unsure of the syntax to use.
Instead of P4201, I would like to select the last row with a value within column P, whatever that may be.
Selection.AutoFill Destination:=Range("P2:P4201")
I am just unsure of the last syntax to select the last row with a value with P. Instead of doing P10000, I would like to make it cleaner.
The below macro counts up to the last filled cell in column P, and selects the full range from P2 until P-end.
Sub Select_Range()
Dim I As String
I = Sheet1.Range("P" & Rows.Count).End(xlUp).Row 'Determine last filled cell and count rows
Sheet1.Range("P2" & ":P" & I).Select 'select full filled range in column P.
End Sub
Let me know if this is what you're looking for.

Excel How to automatically choose the whole column data?

How to automatically choose the whole column data? For example theSUM,
When data is A1:A20, then the result is =SUM(A1:A20), when we change data into A1:A30, then the result automatically become =SUM(A1:A30), i.e choose the all effective data this column or A1:End.
Simply try using: =SUM(A:A)
It should ideally work.
Using VBA, you can use the method .End(xlDown) with .Select to select the last non-empty row in a column.
Range("A1").End(xlDown).Select
Or instead of .Select, you can use .Row to get the last non-empty row number.
Example code:
Sub SumData()
Dim LastRow As Long
LastRow = Range("A1").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A1:A" & LastRow & ")"
End Sub
This will work to find the last row used in the range:
MAX(IF(A:A<>"",ROW(A:A))
To incorporate that in the SUM formula:
=SUM(INDIRECT("A1:A"&MAX(IF(A:A<>"",ROW(A:A)))
Please note that this formula will need to be entered as an array (When in the formula bar hit Ctrl+Shift+Enter) and will slow down your spreadsheet if you have a lot of data in there

Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B

I am trying to do an AutoFill in Excel where I can make my macro:
Find the last row with data in column B (lets say this is B40), and remember.
Select A1.
Drag A1's formula to the row that was found in step 1 (which would be A40) so as to perform an autofill the formula.
I am pretty noobish at VBA so would really appreciate some help.
This should do what you need.
' Get last row with data in column B...
Dim intLastRow As Long
intLastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Fill column A down to this row...
Range("A1:A" & intLastRow).FillDown
You don't even require a Range.FillDown or Range.AutoFill method in this case.
With ActiveSheet
.Cells(1, 1).Resize(.Cells(Rows.Count, 2).End(xlUp).Row, 1) = .Cells(1, 1).Formula
End With
Just start with the top-left cell and use the Range.Resize property to expand it to the desired dimensions and transfer the formula.

Excel VBA: changing hard coded column to dynamic range to autofill to last row

Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!

Auto populate columns in one sheet from another sheet

I would like to populate columns in sheet2 from sheet1. If I have column A in Sheet1 I want A in Sheet2 to have the same information.
I tried using =sheet1!A1 but it only returns the value from A1 in sheet1. I tried using =sheet1!A but it only returns #NAME?.
If Column A from Sheet1 has a dynamic range (it can be empty or have 500 or 1000 rows (I'm populating sheet1 from my database)). How do I use some of those columns in another sheet showing all 500 or 1000 rows?
If I understood you right you want to have sheet1!A1 in sheet2!A1, sheet1!A2 in sheet2!A2,...right?
It might not be the best way but you may type the following
=IF(sheet1!A1<>"",sheet1!A1,"")
and drag it down to the maximum number of rows you expect.
I have used in Google Sheets
={sheetname!columnnamefrom:columnnameto}
Example:
={sheet1!A:A}
={sheet2!A4:A20}
Below code will look for last used row in sheet1 and copy the entire range from A1 upto last used row in column A to Sheet2 at exact same location.
Sub test()
Dim lastRow As Long
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A1:A" & lastRow).Value = Sheets("Sheet1").Range("A1:A" & lastRow).Value
End Sub
In Google Sheets you can use =ArrayFormula(Sheet1!B2:B)on the first cell and it will populate all column contents not sure if that will work in excel
Use the 'EntireColumn' property, that's what it is there for. C# snippet, but should give you a good indication of how to do this:
string rangeQuery = "A1:A1";
Range range = workSheet.get_Range(rangeQuery, Type.Missing);
range = range.EntireColumn;

Resources