![](/img/trans.png)
[英]Inserting formula into last row and autofilling until the last column Excel VBA
[英]Inserting a formula all the way to the last row in the last empty column
我對用VBA編寫宏還很陌生,但是我正在盡我所能來盡快學習。 無論如何,我要執行的任務非常簡單,但是我很難找到一種實現方法。
我想要做的是將公式粘貼到電子表格的最后一個空列中的所有行中。
因此,在這種情況下,進入屏幕截圖中高亮顯示的單元格:示例:
但是,我不想依靠鍵入(“ K1:K”和lastrow),我想做的是創建對最后一列和最后一行的引用。
到目前為止,我只能使用以下命令將公式粘貼到整個列中:
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1).Column
fvlookup = "=vlookup(@1,@2,@3,False)"
fvlookup = Replace (fvlookup, "@1", rng.Address)
fvlookup = Replace (fvlookup, "@2", [LookupFile.csv]LookupFile!$B:$1")
fvlookup = Replace (fvlookup, "@3", "5")
.Columns(lastcol).Formula = fvlookup
但是在稍后的工作中,我想刪除所有的“#N / A”並將其放入名為“ JEs”的其他選項卡中,因為表中的某些項目實際上沒有值在我要查找的表中,需要為其創建JE。 因此,如果我沿着這條路線走,我將在新選項卡中有很多不必要的行。
無論如何,我已經嘗試過了:
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0,1).Column
fvlookup = "=VLOOKUP(@1,@2,@3,False)"
fvlookup = Replace(fvlookup, "@1", rng.Address)
fvlookup = Replace(fvlookup, "@2", "[LookupFile.csv]LookupFile!$B:$I")
fvlookup = Replace(fvlookup, "@3", "5")
With .Columns(lastcol)
lrow = .range("A" & .Rows.Count).End(xlUp).Row
.Rows(lrow).Formula = fvlookup
End With
但這只會將公式放入“ K1”中(請參考所附圖片)
我也嘗試過在粘貼公式並自動填充后選擇此值(我知道建議避免使用vba選擇范圍,但我仍然想嘗試作為最后的手段),但它表示不允許這樣做。
我也嘗試過使用:
.range(lastcol & .range("A" & .Rows.Count).End(xlUp).Rows).Formula = fvlookup
但這給了我運行時錯誤'1004':應用程序定義的錯誤或對象定義的錯誤。 我嘗試創建一個名為'lrange'的變量,並將其設置為類似於我設置lastcol的方式,但是沒有運氣(它返回與上述相同的錯誤消息)。 我也嘗試將上述字符串中的lastcol編輯為lastcol.Column或.Columns(lastcol),但還是沒有。
我嘗試研究類似的問題,所有建議都建議定義兩個變量(lastrow和lastcolumn),然后將公式插入該范圍,但這似乎與我使用的方法不兼容。
我覺得VBA嘗試執行的過程僅限於只能以我設置宏的方式將數據插入整個列或其中的一部分,就像無法找到最后一列並插入所有我設置它的方式到最后一行。 因此,對於這種情況,或者如果我應該改寫其他方式,我深表歉意。
任何人都可以提供關於該主題的任何建議或指示,將不勝感激。
感謝您的時間!
而不是最后循環,我只會使用.FillDown
Cells(2, lastcol).FormulaR1C1 = fvlookup
Range(Cells(2, lastcol), Cells(lrow, lastcol)).FillDown
如何用這樣的代碼替換代碼:
Sub foo()
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1).Column
fvlookup = "=VLOOKUP(@1,@2,@3,False)"
fvlookup = Replace(fvlookup, "@1", Rng.Address)
fvlookup = Replace(fvlookup, "@2", "[LookupFile.csv]LookupFile!$B:$I")
fvlookup = Replace(fvlookup, "@3", "5")
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lrow
.Cells(i, lastcol).Formula = fvlookup
Next i
End Sub
這將從第2行循環到lastrow,並將公式添加到lastcol。
只要該表沒有完全空白的行或列(應該沒有), Range("A1").CurrentRegion
標識該表的范圍。 然后.Rows.Count
和.Columns.Count
為您提供了能夠用公式填充相鄰列所需的信息。
您也可以使用FormulaR1C1
一次性用公式填充列的單元FormulaR1C1
-如果您對單元格的引用非常謹慎。 這是一個例子:
Dim tableRange As Range
Dim x As Integer, y As Integer
Set tableRange = Range("A1").CurrentRegion
x = tableRange.Rows.Count
y = tableRange.Columns.Count
'fill the adjacent column with a SUM function summing across each row
Range(Cells(2, y + 1), Cells(x, y + 1)).FormulaR1C1 = "=SUM(RC[-" & y & "]:RC[-1])"
(您也可以使用y和1為該新列指定標題。)
如果您需要使用特定的(絕對)地址(例如$B:$I
)復制VLOOKUP,那么我將首先命名該范圍,然后在公式中插入該名稱。
這樣的一個例子是:
.FormulaR1C1 = "=VLOOKUP(RC[-1],mylookup,2,FALSE)"
其中mylookup
是范圍名稱, RC[-1]
引用每個公式左側的單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.