[英]How to Fill formula down till last row in column in VBA Excel?
I need the expert help.我需要专家的帮助。 I wanted to fill the Formula for whole column ("D") based on other column 'C' value start from 2nd row.我想根据从第二行开始的其他列“C”值填充整列(“D”)的公式。
Formula is " =IF(ISBLANK(C2),"Still open",TEXT(C2,"mmm")) "公式是“ =IF(ISBLANK(C2),”Still open”,TEXT(C2,“mmm”)) ”
I have tried as below but But its fill down the bottom of the column irrespective of the No data on dependent column.我已经尝试过如下,但无论从属列上的无数据如何,它都会填充列的底部。 If 'C' Column have 50 records.如果“C”列有 50 条记录。 I wanted to fill till last valid row 50 (Example : no data in column 'C' after 50 row but column D fill row no 1048576) of the table我想填充到表格的最后一个有效行 50(例如:50 行之后的“C”列中没有数据,但 D 列填充第 1048576 行)
Dim Output_Sh As Worksheet
Set Output_Sh = ThisWorkbook.Sheets("Output")
Output_Sh.Range("D1").Value = "Month Closed" ' Heading
Set Rng = Output_Sh.Range("D2:D" & Output_Sh.Range("C2").End(xlDown).Row)
Rng.FormulaR1C1 = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))"
HI, quick update :嗨,快速更新:
I have tried the formola on colmn D then it's working fine.我已经在 colmn D 上尝试过formola,然后它工作正常。 When I want to apply on Column "AK" then its populate only 1st two row.当我想申请列“AK”时,它只填充第一两行。
Sub InsertAK()
Dim n As Integer
Dim Output_Sh As Worksheet
Dim Rng As Range
Set Output_Sh = ThisWorkbook.Sheets("Output")
Output_Sh.Columns("AK:AK").Insert
Output_Sh.Range("AK1").Value = "Month Closed"
Output_Sh.Range("AK2").Value = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))" ' "Month Closed"
Set Rng = Output_Sh.Cells.Find("*", , , , , xlPrevious)
Output_Sh.Range("AK3:AK" & Rng.Row).FormulaR1C1 = "=IF(ISBLANK(C3),""Still open"",TEXT(C3,""mmm""))"
End Sub
If 'C' Column have 50 records.如果“C”列有 50 条记录。 I wanted to fill till last valid row 50我想填到最后一个有效行 50
HI Siddharth thanks for reply.嗨悉达多感谢回复。 Actually I need it to apply on column "AK".实际上,我需要将其应用于“AK”列。 Then I have replaced the word from D to AK then I have got the value only 1irst 2 rows only.然后我将单词从 D 替换为 AK 然后我只得到了第 1irst 2 行的值。 I don't know where I am doing wrong ?我不知道我哪里做错了? – skt 6 mins ago – skt 6 分钟前
From the formula that you are using =IF(ISBLANK(C2),"Still open",TEXT(C2,"mmm"))
, it is evident that there will be blank values and hence I suggested in the comments above PLEASE do not use xlDown
to find the last row.根据您使用的公式=IF(ISBLANK(C2),"Still open",TEXT(C2,"mmm"))
,很明显会有空白值,因此我在上面的评论中建议请不要使用xlDown
查找最后一行。
Is this what you are trying?这是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim Output_Sh As Worksheet
Dim lRow As Long
Set Output_Sh = ThisWorkbook.Sheets("Output")
With Output_Sh
lRow = .Range("C" & .Rows.Count).End(xlUp).Row
.Range("AK1").Value = "Month Closed" ' Heading
.Range("AK2:AK" & lRow).Formula = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))"
End With
End Sub
The below is the running solution for the above problme以下是上述问题的运行解决方案
Sub InsertAK()
Dim n As Integer
Dim Output_Sh As Worksheet
Dim Rng As Range
Set Output_Sh = ThisWorkbook.Sheets("Output")
Output_Sh.Columns("AK:AK").Insert
Output_Sh.Range("AK1").Value = "Month Closed"
Output_Sh.Range("AK2").Value = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))" ' "Month Closed"
n = Output_Sh.Range("B2", Output_Sh.Range("B2").End(xlDown).Rows).Count
Output_Sh.Range("AK3:AK" & n + 1).FormulaR1C1 = "=IF(ISBLANK(C3),""Still open"",TEXT(C3,""mmm""))"
End Sub
You'd get incorrect results with xlDown
as described in comment.如评论中所述,使用xlDown
会得到不正确的结果。 However, you may get incorrect results with xlUp
if last few rows of column C are blank while other data rows are filled in.但是,如果 C 列的最后几行为空而其他数据行已填充,则使用xlUp
可能会得到不正确的结果。
Following approach will locate last filled row in the data and accordingly set formula.以下方法将定位数据中最后填充的行并相应地设置公式。
Dim Output_Sh As Worksheet
Dim Rng As Range
Set Output_Sh = ThisWorkbook.Sheets("Output")
Output_Sh.Range("D1").Value = "Month Closed" ' Heading
Set Rng = Output_Sh.Cells.Find("*", , , , , xlPrevious)
Output_Sh.Range("D2:D" & Rng.Row).FormulaR1C1 = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))"
Thanks for your support it's working now as below code:感谢您的支持它现在工作如下代码:
Sub InsertAK()
Dim n As Integer
Dim Output_Sh As Worksheet
Dim Rng As Range
Set Output_Sh = ThisWorkbook.Sheets("Output")
Output_Sh.Columns("AK:AK").Insert
Output_Sh.Range("AK1").Value = "Month Closed"
Output_Sh.Range("AK2").Value = "=IF(ISBLANK(C2),""Still open"",TEXT(C2,""mmm""))" ' "Month Closed"
Set Rng = Output_Sh.Cells.Find("*", , , , , , , xlPrevious)
n = Output_Sh.Range("C2", Output_Sh.Range("C2").End(xlDown).Rows).Count
Output_Sh.Range("AK3:AK" & n + 1).FormulaR1C1 = "=IF(ISBLANK(C3),""Still open"",TEXT(C3,""mmm""))"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.