简体   繁体   English

VBA Excel 公式用于当前行而不是 lastrow

[英]VBA Excel formula for current row instead of lastrow

Good afternoon,下午好,

I found a similar query here:我在这里找到了一个类似的查询:

Formula for the current row in Excel Excel 中当前行的公式

but with no reasonable answer.但没有合理的答案。

My query is related to the situation here:我的查询与这里的情况有关:

VBA Excel autofill the table based on userform, when data comes from the other sheet VBA Excel 根据用户表单自动填充表格,当数据来自其他表格时

I am trying to manage the code.我正在尝试管理代码。

 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)

I was trying to manage with this solution, but the problem is that the code always brings me the last row.我试图用这个解决方案来管理,但问题是代码总是把我带到最后一行。

In my situation when I have the formula in the AD column populated down to the AD370 or something.在我的情况下,当我将 AD 列中的公式填充到 AD370 或其他东西时。 In the AE formula, I would like to have an auto-population based on the value from the AD column in the same row.在 AE 公式中,我想根据同一行中 AD 列的值进行自动填充。 It's like AE10 = Value (=AD10).就像 AE10 = 值 (=AD10)。

In this code, which I have I am getting the last value, as you can see in the image above.在这段代码中,我得到了最后一个值,如上图所示。 How can I get the value from exactly the same row?如何从完全相同的行中获取值?

UPDATE:更新:

According to this solution根据这个解决方案

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

I tried to do sth like this我试着这样做

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

But now I am getting error:但现在我收到错误:

The object doesn't support this property or method object 不支持此属性或方法

在此处输入图像描述

I think the easiest way is to step through the AD column and add them one at a time.我认为最简单的方法是逐步浏览 AD 列并一次添加一个。 The below For loop should do the trick.下面的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM