简体   繁体   English

VBA 循环遍历列中的值,将值放置在影响公式的单元格中,并将结果公式复制并粘贴到相邻列

[英]VBA to loop through values in column, place values in cell that affects formula, and copy and paste resultant formula to adjacent column

Not super knowledgeable with VBA, but I'm assuming/hoping this is a fairly simple question.对 VBA 不是很了解,但我假设/希望这是一个相当简单的问题。 I have a worksheet where column K has any values that can be input in cell E1.我有一个工作表,其中 K 列具有可以在单元格 E1 中输入的任何值。 Cell B25 contains a simple formula (=$B$24-$B$8), but is affected by other formulas, which depend on the value in cell E1.单元格 B25 包含一个简单的公式 (=$B$24-$B$8),但受其他公式的影响,这些公式取决于单元格 E1 中的值。

What I need is for E1 to be looped through with the values in column K and paste the calculated value in B25 in column L for each value in column K. I have 2 VBA codes that I'm working with, but neither of them are working exactly how we need them to.我需要的是让 E1 与 K 列中的值循环,并将 B25 中的计算值粘贴到 L 列中 K 列中的每个值。我有 2 个正在使用的 VBA 代码,但它们都不是完全按照我们需要的方式工作。

Sub Check()
Dim c As Range, sh1 As Worksheet
Set sh1 = Sheets("Sheet1")
    For Each c In sh1.Range("K2", Cells(Rows.Count, "K").End(xlUp))
        sh1.Range("E1") = c.Value
        sh1.Range("B25", Cells(Rows.Count, "B").End(xlUp)).Copy
        sh1.Range("L2", Cells(Rows.Count, "L").End(xlUp)).PasteSpecial Paste:=xlPasteValues
         Next
End Sub

And

        Sub Check2()
    Dim sh1 As Worksheet, c As Range
    Set sh1 = Sheets("Sheet1")
        For Each c In sh1.Range("K2", sh1.Cells(Rows.Count, "K").End(xlUp))
            sh1.Range("E1") = c.Value
            sh1.Range("B25", sh1.Cells(Rows.Count, 2).End(xlUp).Offset(, 2)).Copy _
            sh1.Cells(Rows.Count, 12).End(xlUp)(2)
        Next

End Sub

The first code loops through E1 and posts the calculated value of B25 in cell L2 only, so there's no way for us to know which corresponding item in column K it's referencing.第一个代码循环遍历 E1 并仅在单元格 L2 中发布 B25 的计算值,因此我们无法知道它引用了 K 列中的哪个对应项。

The second code is closer.第二个代码更接近。 It posts the results of the formula in column L sequentially, but it recalculates for each instance of the looped column K value in E1, meaning that ultimately the result of the formula in B25 will post the results only corresponding to the final value of column K in column L (ie the formula =$B$24-$B$8 is copied and pasted down column L for every value in column K but the last value of column K will be in cell E1, so that's what the formula is referring to).它将公式的结果按顺序发布在 L 列中,但它会重新计算 E1 中循环列 K 值的每个实例,这意味着最终 B25 中的公式结果将发布仅对应于列 K 的最终值的结果在 L 列中(即公式 =$B$24-$B$8 被复制并粘贴到 L 列,用于 K 列中的每个值,但 K 列的最后一个值将在单元格 E1 中,所以这就是公式所指的内容) .

I might be going about this all wrong, but as mentioned, I don't deal with VBA a lot.我可能会在这一切都错了,但如前所述,我不经常处理 VBA。 I hope someone can help me with this, but please let me know if I was unclear in my request or anyone needs more information!我希望有人能帮我解决这个问题,但如果我的请求不清楚或者有人需要更多信息,请告诉我!

Try this.尝试这个。

Sub Check2()

Dim sh1 As Worksheet, c As Range

Set sh1 = Sheets("Sheet1")

For Each c In sh1.Range("K2", sh1.Cells(Rows.Count, "K").End(xlUp))
    sh1.Range("E1") = c.Value
    sh1.Range("B25", sh1.Cells(Rows.Count, 2).End(xlUp).Offset(, 2)).Copy
    sh1.Cells(Rows.Count, 12).End(xlUp)(2).PasteSpecial xlValues
Next

End Sub

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

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