我可以隐藏包含Vlookup函数但该函数仍然有效的单元格吗



我正在处理一个Excel项目,其中用户键入产品id号,相应的图像显示在不同的单元格上。

为了实现这一点,还有另一个单元运行Vlookup,并在存储在不同工作表中的产品数据库中检查该id号
然后我使用VBA将图像输出到正确的单元格中。

是否可以用Vlookup函数隐藏单元格,但当用户用ID号更改单元格时,它仍然运行?

我会使用类似以下的函数

Function getval() As Variant
Dim ws As Worksheet
Dim rg As Range

Set ws = Worksheets("Input") ' sheet used for input
Set rg = ws.Range("F10")  ' cell with the productid

Dim wsData As Worksheet
Set wsData = Worksheets("Data")  ' sheet with the data

Dim rgData As Range
Set rgData = wsData.Range("A1:B4")  ' range with the data

getval = WorksheetFunction.VLookup(rg, rgData, 2, False)  ' replace the 2 with the column you need
End Function

你可以测试它

Sub testit()
Debug.Print getval
End Sub

使用VBA非常简单(请参阅https://techcommunity.microsoft.com/t5/excel/lock-cell-without-protecting-worksheet/m-p/2360200)。打开VBA编辑器,然后选择要保护的工作表。将该代码添加到同一张纸内;正确的";如果有人决定更改公式。

' protect cells in worksheet, change back if they get modified
' for individual cells separate them by commas like (Range("A1, B1, C1")
' or use ranges
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("H12:O14"), Target) Is Nothing Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
End If
End Sub

请注意,需要启用宏才能正常工作。我把它添加到了一个电子表格中,我需要在上面保护单元格范围,它可以按预期工作。如果您键入的内容不是单元格中已存在的内容,它会自动将其改回。

最新更新