excel - 当前行而不是 lastrow 的 VBA 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
但现在我收到错误:
该对象不支持此属性或方法
解决方案
我认为最简单的方法是逐步浏览 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
推荐阅读
- javascript - 如何表示和更新数据库中文档的顺序?
- spring - Spring-boot 存储库未注册为有效 bean
- c# - 如何获取 oauth 刷新令牌?
- python - Errno 13 Permission Denied 在我刚刚创建的目录中
- java - 如何将场景的边框设置为不同颜色的 JavaFX?
- html5-canvas - HTML5 Canvas - 为带有阴影的透明 png 添加边框
- django - 如何使用 Graphene 和 Relay 访问 CustomNode 上的 Meta
- python - 使用 python3 将相同的环境构建到 pex 文件中失败,但适用于 python2
- mysql - Windows 服务管理器中没有 MySQL 服务
- azure - Azure VNET 中的 Internet 丢失