简体   繁体   English

使用VLOOKUP将单元格引用传递给公共变量?

[英]Use VLOOKUP to pass cell reference to a public variable?

I have a userform that opens on cell change in a column. 我有一个用户窗体,该窗体在列中的单元格更改时打开。 That userform contains checkboxes, which all trigger a second userform with a text box which looks up a cell on a hidden sheet for its contents. 该用户窗体包含复选框,所有复选框都触发带有文本框的第二个用户窗体,该文本框在隐藏的工作表上查找其内容的单元格。 (The checkbox that's ticked determines which cell the textbox looks for). (选中的复选框将确定文本框要查找的单元格)。 The user then edits the box, clicks a button, and the new text is written back to the same cell. 然后,用户编辑该框,单击一个按钮,然后将新文本写回到同一单元格。

This is the VBA for when the checkbox is ticked. 选中复选框时的VBA。 It works great. 效果很好。 Hooray! 万岁!

Dim vln As Variant
Dim reta As Worksheet

Set reta = ActiveWorkbook.Sheets("RetailerActivity")
Set vln = ActiveCell.Offset(-1, -3)

UserForm2.TextBox1.Text = Application.WorksheetFunction.VLookup(vln, reta.Range("A1:Z100"), 3, False)

UserForm2.TescoSave.Visible = True
UserForm2.Show

End Sub

When the textbox has been edited, I would like to write it back to the same cell it came from. 编辑完文本框后,我想将其写回到原来的单元格中。 I figure the easiest way to do that is to have a public variable (as range), and to pass the result of the vlookup into that variable so the second userform can have a line which reads 我认为最简单的方法是拥有一个公共变量(作为范围),并将vlookup的结果传递到该变量中,以便第二个用户窗体可以包含一行

Private Sub ASave_Click()

publicvariable.Value = TextBox1.Value
userform1.hide

End Sub

Nice and easy, rather than doing a VLookup again. 方便又好,而不是再次执行VLookup。 Right? 对?

Either way, I can't seem to set the public variable as the lookup. 无论哪种方式,我似乎都无法将公共变量设置为查找。

Outside of any sub I have 除了任何我有的子

Public bums As Range

And in the code above, after the bit where I've set the text box, I've tried to add the line 在上面的代码中,在设置文本框的位置之后,我尝试添加该行

Set bums = Application.WorksheetFunction.VLookup(vln, reta.Range("A1:Z100"), 3, False)

But the code errors with a "type mismatch". 但是代码错误带有“类型不匹配”。

If I try 如果我尝试

Set bums = Range(Application.WorksheetFunction.VLookup(vln, reta.Range("A1:Z100"), 3, False))

I get method "Range" of object "_global" failed. 我得到对象“ _global”的方法“ Range”失败。

I code by cobbling bits off the internet, as you can probably tell, so this is I don't doubt a complete kludge. 正如您可能会说的那样,我通过在互联网上乱码来进行编码,所以我毫不怀疑这是一个完全的错误。

Any advice would be super appreciated. 任何建议将不胜感激。

VLookup returns a value, not a Range. VLookup返回一个值,而不是范围。 You could use Match to find the row and then Cells to get the actual reference - for example: 您可以使用Match查找行,然后使用Cells获取实际引用-例如:

Dim vMatch
vMatch = Application.Match(vln, reta.Range("A1:A100"),0)
If Not IsError(vMatch) then 
Set bums = reta.Cells(vMatch, "C")
else
msgbox "No match for " & vln
Exit Sub
End If

Personally I would also not use a public variable, but create a property for Userform2 to which you can assign the range. 就个人而言,我也不会使用公共变量,而是为Userform2创建一个属性,您可以向其分配范围。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM