vba 如何显示单元格值偏移活动单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18389294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:54:51  来源:igfitidea点击:

How to display cell value offset active cell

vbaexcel-vbaexcel

提问by Patrick Brunet

I'm trying to display the value of a cell according to my ActiveCell or Target Cell using a Function. The cell I'm trying to display is in the same spreadsheet.

我正在尝试使用函数根据我的 ActiveCell 或目标单元格显示单元格的值。我试图显示的单元格在同一个电子表格中。

My objective is to create a Header in the spreadsheet that would display information according to the position of the Active cell.

我的目标是在电子表格中创建一个标题,该标题将根据活动单元格的位置显示信息。

I've tried this code and typed the function =VendorName5() in the cell where I want the value to be displayed but it seems to be missing something. Can you help ?

我已经尝试过这段代码,并在我想要显示值的单元格中输入了函数 =VendorName5() 但它似乎丢失了一些东西。你能帮我吗 ?



Function VendorName5() As String

Name = ActiveCell.Offset(0, -4)

       VendorName5 = Name

End Function


OK, found it:

好的,找到了:

"Private Sub Worksheet_SelectionChange(ByVal Target As Range)

"私有子工作表_SelectionChange(ByVal Target As Range)

If Target.Column = 8 Then Range("C2") = Cells(Target.Row, 2) End If

If Target.Column = 8 Then Range("C2") = Cells(Target.Row, 2) End If

End Sub"

结束子”

tks for the help

tks 的帮助

回答by Fabio

This is VBA not VB.NET

这是 VBA 不是 VB.NET

Try .Valueof Cell

.Value细胞尝试

Function VendorName5() As String

    Name = ActiveCell.Offset(0, -4).Value

    VendorName5 = Name

End Function

回答by David Zemens

Why not use the Offsetfunction:

为什么不使用该Offset功能:

In cell A1:

在单元格 A1 中:

=OFFSET(A1,0,4,1,1)

=OFFSET(A1,0,4,1,1)

Or, refer to the cell directly:

或者,直接引用单元格:

=E1

=E1

etc.

等等。

This seems overkill to use a UDF to do something that worksheet functions ordinarily allow for, already.

使用 UDF 来做工作表函数通常已经允许的事情似乎有点过分了。