简体   繁体   English

如何将 Cells().Select 命令中单元格的值设置为包含公式的单元格的值?

[英]How can I set the value of a cell in Cells().Select command to the value of a cell that has a formula in it?

I'm trying to make a macro that copies the values inside certain cells of sheet1 and pastes then in sheet2.我正在尝试制作一个宏来复制 sheet1 的某些单元格内的值,然后粘贴到 sheet2 中。

This is a formula that i wrote inside cell "AI2":这是我在单元格“AI2”中写的一个公式:

=IFERROR(SUM(1+AH:AH),"0")

and it produces a number that I want to use in the macro as a variable row coordinate.它产生一个我想在宏中用作可变行坐标的数字。

This is the code i have in my worksheet in order to trigger the macro:这是我在工作表中用于触发宏的代码:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
     If Range("AI2") <> 0 Then
     Call macro1
     End If
End Sub

And this is the macro:这是宏:

Sub macro1()
Dim RV As Integer
RV = Sheets("sheet1").Range("AI2").Value
Cells(RR, 33).Select
Range(ActiveCell.Offset(0, -6), ActiveCell.Offset(0, -1)).Select
Selection.Copy
Sheets("sheet2").Select
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End Sub

If I delete the first 3 lines of code, the macro works, but I have to manually select the cell for the offsets to reference from.如果我删除前 3 行代码,宏就可以工作,但我必须手动选择要引用的偏移量的单元格。

I need to make it so the value of cell "AI2" is used as the first coordinate in this line of code:我需要这样做,以便将单元格“AI2”的值用作这行代码中的第一个坐标:

Cells(RR, 33).Select

I am very new to any kind of programming, but I want to learn this in order to achieve my goals for this spreadsheet and future ones with similar functions.我对任何类型的编程都很陌生,但我想学习这一点,以实现我对这个电子表格和未来具有类似功能的电子表格的目标。

I am limiting the scope of your Worksheet_Change to only fire when a change is registered in Column AH since this is the column that will trigger a formula change in Column AI我将Worksheet_Change的范围限制为仅在Column AH注册更改时触发,因为该列将触发Column AI列中的公式更改


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 34 Then
        If Range("AI2") <> 0 Then
            Macro2
        End If
    End If
End Sub

Sub Macro2()

Dim cs As Worksheet: Set cs = ThisWorkbook.Sheets("Sheet1")
Dim ps As Worksheet: Set ps = ThisWorkbook.Sheets("Sheet2")

Dim xRow As Long, LR As Long
LR = ps.Range("A" & ps.Rows.Count).End(xlUp).Offset(1).Row
xRow = cs.Range("AI2").Value

cs.Range(cs.Cells(xRow, "AB"), cs.Cells(xRow, "AG")).Copy
    ps.Range("A" & LR).PasteSpecial xlPasteValues

End Sub

Copy Range to First Empty Cell将范围复制到第一个空单元格

Calculate计算

If you are using a formula in the cell range AI2 you should use the Worksheet Calculate event which will occur everytime the formula is being calculated.如果您在单元格区域AI2中使用公式,则应使用每次计算公式时都会发生的工作表计算事件。

Standard Module标准模块

Option Explicit

Public Const strRange As String = "AI2"
Public vntValue As Variant

Sub macro1()

    Dim rng As Range  ' Target Cell Range
    Dim RV As Long    ' Row Value

    ' In Target Worksheet
    With ThisWorkbook.Sheets("Sheet2")
        ' Calculate the first empty (unused) cell in column A (A1 not included).
        Set rng = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    End With

    ' In Source Worksheet
    With ThisWorkbook.Worksheets("Sheet1")
        ' Write the value of Row Cell to Row Value.
        RV = .Range(strRange).Value
        With .Cells(RV, "AH") ' or 33
            ' Copy range from "AB" to "AG" in row defined by Row Value in
            ' Source Worksheet to the range from "A" to "F" in row of Target
            ' Cell Range in Target Worksheet.
            rng.Resize(, 6) = Range(.Offset(0, -6), .Offset(0, -1)).Value
        End With
    End With

End Sub

Sheet1表 1

Option Explicit

Private Sub Worksheet_Calculate()
    If vntValue <> Range(strRange).Value Then
        vntValue = Range(strRange).Value
        If Range(strRange).Value <> "0" Then macro1
    End If
End Sub

ThisWorkbook这本练习册

Option Explicit

Private Sub Workbook_Open()
    vntValue = Worksheets("Sheet1").Range(strRange).Value
End Sub

Change改变

If you are manually changing the values in the cell range AI2 , you have to use the Worksheet Change event.如果您手动更改单元格区域AI2的值,则必须使用Worksheet Change事件。

Standard Module标准模块

Option Explicit

Sub macro1()

    Dim rng As Range  ' Target Cell Range
    Dim RV As Long    ' Row Value

    ' In Target Worksheet
    With ThisWorkbook.Sheets("Sheet2")
        ' Calculate the first empty (unused) cell in column A (A1 not included).
        Set rng = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
    End With

    ' In Source Worksheet
    With ThisWorkbook.Worksheets("Sheet1")
        ' Write the value of Row Cell to Row Value.
        RV = .Range("AI2").Value
        ' In cell at the intersection of Row Value and column "AH".
        With .Cells(RV, "AH") ' or 33
            ' Copy range from "AB" to "AG" in row defined by Row Value in
            ' Source Worksheet to the range from "A" to "F" in row of Target
            ' Cell Range in Target Worksheet.
            rng.Resize(, 6) = Range(.Offset(0, -6), .Offset(0, -1)).Value
        End With
    End With

End Sub

Sheet1表 1

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
     Const cCell As String = "AI2"
     If Target = Range(cCell) Then
         If Range(cCell).Value <> "0" Then macro1
     End If
End Sub

Like in the Calculate version, you might also want to use a public variable ( vntValue ) to prevent triggering macro1 in case the value in cell range AI2 hasn't actually changed.就像在计算的版本,你可能也想使用一个公共变量( vntValue ),以防止触发macro1的情况下,在单元格范围内的值AI2实际上并没有改变。

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

相关问题 如果活动单元格的值不是公式,如何选择活动单元格 - How to select the active cell if it has a value not the formula 如何将计算出的值发送到单元格并保存工作表,以便重新打开单元格时不附加公式? - How can I send a calculated value to a cell and save the sheet so that when reopened the cell has no formula attached? 如何根据另一个单元格的值更改单元格的公式 - How can I change the formula of the cell according to the value of another cell 如何使用Apache POI在具有公式的单元格上设置数值? - How to set Numeric value on cell that has a formula using Apache POI? Excel公式-如何将一个单元格中的公式值分为2个不同的单元格? - Excel formula - How to separate a formula value in a cell into 2 different cells? 单元格中的值作为选择公式中的变量 - Value in cell as variable in select formula 如何将日期值设置到单元格 - How can I set Date value into cell 如何在 VBA 的单元格中输入公式的值? - How can I input the value of a formula in a cell in VBA? 我是否可以将一个单元格值作为公式相等地传递给另一单元格? - Can I Pass one cell value as formula into another cell by equal? 如果其他单元格与特定值匹配,如何在公式中引用特定单元格 - how to reference a specific cell in a formula if other cells match a specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM