简体   繁体   English

引用另一个工作表中的单元格

[英]Referencing cell in another worksheet

I can write the correct formula in Excel, but because I have so many IF statements, I have to put it into VBA. 我可以在Excel中编写正确的公式,但是由于我有很多IF语句,因此必须将其放入VBA。 The formula in Excel is supposed to return a value from another worksheet ("Sheet2") based on a value (F5) selected on "Sheet1". Excel中的公式应该基于在“ Sheet1”上选择的值(F5)从另一个工作表(“ Sheet2”)返回一个值。 Here is part of the Excel formula that was created (there are many more IF Statements): 这是已创建的Excel公式的一部分(还有更多的IF语句):

IF($F$5="AOM",
   OFFSET('Sheet2'!B3,'Sheet2'!$B$1,1,1,1),
   IF($F$5 = "Mid Adj",
      OFFSET('Sheet2'!B3,'Sheet2'!$B$1,6,1,1),
      ""
     )
  )

Here is the If Statement part of the VBA I've created: 这是我创建的VBA的If语句部分:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim dummyVar As String

If Target = Range("F5") Then

    If Range("F5").Text = "AOM" Then
        dummyVar = ProcAOM()

    ElseIf Range("F5").Text = "Mid Adj" Then
        dummyVar = ProcML()

For the Proc, I'm not sure how to set up the Offset formula that references another worksheet. 对于Proc,我不确定如何设置引用另一个工作表的Offset公式。

From what I can understand you are trying to achieve, this will be just for 1 cell? 据我了解您正在努力实现的目标,这将仅适用于1个单元格? If so, you don't have to care about Worksheet_Change. 如果是这样,则不必关心Worksheet_Change。

You can create a User Defined Function (UDF) for this purpose. 为此,您可以创建用户定义函数(UDF)。 Say BigIF with assumptions: 用以下假设说BigIF

  • The Reference Cell does not change (B3 of Sheet2) 参考单元格不变(Sheet2的B3)
  • Row Offset is defined in a fixed cell (B1 of Sheet2) 行偏移量在固定单元格(Sheet2的B1)中定义
  • Column Offset is determined in the code 列偏移量由代码确定
  • Returns the value of the range offset from Reference Cell 返回参考单元格的范围偏移量的值

Paste below code into a module and use it in worksheet just like a formula, but referencing the F5 of Sheet1 (doing this forces Excel to recalculate when F5 changes). 将下面的代码粘贴到模块中,就像在公式中一样在工作表中使用它,但是引用Sheet1的F5(这样做会强制Excel在F5更改时重新计算)。 ie. 即。 =BigIf('Sheet1'!$F$5)

Function BigIF(oRng As Range) As Variant
    Dim oWS As Worksheet, oRngRef As Range
    Dim lRowOffset As Long, lColOffset As Long, sID As String

    Set oWS = ThisWorkbook.Worksheets("Sheet2")
    Set oRngRef = oWS.Range("B3") ' Offset Reference
    sID = oRng.Text ' Sheet1!F5 value
    ' ROW OFFSET: Sheet2!$B$1 value
    lRowOffset = CLng(oWS.Range("B1").Value)
    ' COLUMN OFFSET: based on sID
    Select Case sID
        Case "AOM":         lColOffset = 1
        Case "Mid Adj":     lColOffset = 6
        '... Other Cases
        Case Else:          lColOffset = 0
    End Select
    BigIF = oRngRef.Offset(lRowOffset, lColOffset)
    Set oRngRef = Nothing
    Set oWS = Nothing
End Function

Of cause you can also use the Worksheet_Change event method, but more code. 当然,您也可以使用Worksheet_Change事件方法,但需要更多代码。

暂无
暂无

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

相关问题 在VBA函数中引用另一个工作表中的单元格 - Referencing a cell from another worksheet in a function for VBA 在Excel中引用另一个工作表中的单元格 - Referencing a cell from another worksheet in excel 通过引用工作表的名称来引用另一个工作表中的空白单元格? - Refer to a vaiable cell in another worksheet by referencing the worksheet's name? 通过引用当前工作表的名称来引用另一个工作表中的单元格? - Refer to a cell in another worksheet by referencing the current worksheet's name? 通过将另一个单元格中的字符串引用为列来在另一个工作表中引用动态单元格 - Dynamic cell reference in another worksheet by referencing string in another cell as column 引用另一个工作表中的单元格,其中公式在“ =”之后带有“ +” - Referencing a cell from another worksheet, with formula that has '+' after '=' 引用单元格到不同工作表中的范围 - referencing a cell to a range in different worksheet (Excel)在引用其他工作表中的单元格时,是否可以将工作表名称作为单元格值传递? - (Excel) When referencing cells from another worksheet, can I pass the worksheet name as a cell value? 当单元格包含引用另一个工作表中另一个单元格的公式时,如何更新时间戳? - How do i get the Timestamp to update, when the cell contains a formula referencing another cell in another worksheet? 引用工作表中的宏以在另一个工作表中使用 - Referencing a macro from worksheet to use in another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM