gpt4 book ai didi

excel - 点击按钮跳转到单元格

转载 作者:行者123 更新时间:2023-12-04 20:41:46 26 4
gpt4 key购买 nike

我有一个 excel vb 代码,当单击转到下一张表并根据员工编号搜索值时,我在 excel 中创建了一个按钮。我在这段代码中使用了 Vlookup。我的问题是光标应该直接转到搜索的单元格。我的代码如下: -

Private Sub CommandButton3_Click()  
Sheets("Sheet2").Activate

Dim rng As Range
Dim ws1, ws2 As Worksheet
Dim MyStringVar1 As String

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Set rng = ws1.Range("c4")

With ws1

MyStringVar1 = Application.WorksheetFunction.VLookup(rng, ws2.Range("b3:n13").Value, 5, False)

On Error GoTo 0
If MyStringVar1 = "" Then MsgBox "Item not found" Else MsgBox MyStringVar1

End With


End Sub

最佳答案

您是否尝试过使用 select而不是 msgbox

Private Sub CommandButton3_Click()  

Sheets("Sheet2").Activate

Dim rng As Range
Dim ws1, ws2 As Worksheet
Dim MyStringVar1 As String

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Set rng = ws1.Range("c4")

With ws1

MyStringVar1 = Application.WorksheetFunction.VLookup(rng, ws2.Range("b3:n13").Value, 5, False)

On Error GoTo 0
If MyStringVar1 = "" Then MsgBox "Item not found" Else MyStringVar1.Select

End With


End Sub

关于excel - 点击按钮跳转到单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com