首页 > 解决方案 > 当前行而不是 lastrow 的 VBA Excel 公式

问题描述

下午好,

我在这里找到了一个类似的查询:

Excel 中当前行的公式

但没有合理的答案。

我的查询与这里的情况有关:

当数据来自另一张表时,VBA Excel 根据用户表单自动填充表格

我正在尝试管理代码。

 lastrowG = wks.Range("G" & Rows.Count).End(xlUp).Row
 LastrowH = wks.Range("H" & Rows.Count).End(xlUp).Row + 1
 lastrowL = wks.Range("L" & Rows.Count).End(xlUp).Row
 LastrowAD = wkf.Range("AD" & Rows.Count).End(xlUp).Row + 1

'LastrowAE = Sheets("Formulas").Range("AE" & Rows.Count).End(xlUp).Row + 1
 wkf.Range("AD" & LastrowAD).value = ("=IF(Tracker!L" & lastrowL & "<20,'1:00','2:00'")
 wkf.Range("AE" & LastrowAE).value = ("=Formulas!AD" & LastrowAD)
 wks.Range("H" & LastrowH).value = ("=G" & lastrowG & "+Formulas!AE" & LastrowAE)

我试图用这个解决方案来管理,但问题是代码总是把我带到最后一行。

在我的情况下,当我将 AD 列中的公式填充到 AD370 或其他东西时。在 AE 公式中,我想根据同一行中 AD 列的值进行自动填充。就像 AE10 = 值 (=AD10)。

在这段代码中,我得到了最后一个值,如上图所示。如何从完全相同的行中获取值?

更新:

根据这个解决方案

https://excel.tips.net/T002267_Selecting_a_Cell_in_the_Current_Row.html

我试着这样做

     LastrowAD = wkf.Range("AD" & Rows.Count).ActiveCell.Row + 1

但现在我收到错误:

该对象不支持此属性或方法

在此处输入图像描述

标签: excelvbaexcel-formula

解决方案


我认为最简单的方法是逐步浏览 AD 列并一次添加一个。下面的 For 循环应该可以解决问题。

Dim LastrowAD as Long
Dim rowAD as Range
Dim wkf as Worksheet
Dim i as Variant

Set wkf = ThisWorkbook.Worksheets("Formulas")

LastrowAD = wkf.Cells(Rows.count, "AD").End(xlUp).Row
Set rowAD = wkf.range("AD2:AD" & LastrowAD)

'puts formula from AD row x into AE row x
for each i in rowAD
    wkf.Cells(i.row, "AE").Formula = "=Formulas!AD" & i.row
next i

'rest of code
end sub

推荐阅读