简体   繁体   English

将范围从其他工作簿分配给变量

[英]Assign range from a different workbook to variable

I want to use the WorksheetFunction.Match function, and need to load this with an area. 我想使用WorksheetFunction.Match函数,并需要用一个区域加载它。

Row = WorksheetFunction.Match(country, Countries, 0)

Can you help me construct the Countries variable? 您能帮我构造Countrys变量吗? I tried lots of ways (including the below) but none seem to work... 我尝试了很多方法(包括以下方法),但似乎都没有效果...

Countries = Workbooks("PERSONAL.XLSB").sheets("Sheet1").Range("B:B") 
Countries = Worksheets("PERSONAL.XLSB!Sheet1").Range("B:B") 
Countries = Worksheets.Range("PERSONAL.XLSB.Sheet1!B:B")  ...

I also tried some of the above with "Set" in front but that didn't help either... 我也尝试了上面的一些“ Set”,但是那还是没有帮助。

Well I used the code below and it works OK. 好吧,我使用下面的代码,它可以正常工作。 You do have a PERSONAL.XLSB file which is loading and hidden when you start Excel? 您确实有一个PERSONAL.XLSB文件,该文件在启动Excel时正在加载并隐藏吗? What is the error message - is it subscript out of range? 错误消息是什么-下标是否超出范围?

Public Sub test()

 Dim r As Integer
 Dim Countries As Range
 Dim Country As Range

 Set Countries = Workbooks("PERSONAL.XLSB").Worksheets("Sheet1").Range("C1:C8")
 Set Country = Workbooks("PERSONAL.XLSB").Worksheets("Sheet1").Range("A1")

 r = WorksheetFunction.Match(Country, Countries, 0)

 MsgBox (r)

End Sub

OK problem solved, thanx Dave! OK问题解决了,thanx Dave!

I actually use it as a Function that returns the "area" to which the country belongs: 我实际上将其用作返回国家所属“区域”的函数:

Function AreaEMEA(Country As String)
 Dim Countries As Range
 Dim Areas As Range
 Set Countries = Workbooks("PERSONAL.XLSB").Worksheets("Sheet1").Range("B:B")
 Set Areas = Workbooks("PERSONAL.XLSB").Worksheets("Sheet1").Range("A:A")
 Row = WorksheetFunction.Match(Country, Countries, 0)
 AreaEMEA = WorksheetFunction.Index(Areas, Row)
End Function

which i call using 我称之为使用

"=PERSONAL.XLSB!Module1.areaemea(A1)"

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

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