简体   繁体   English

Excel 2007 VBA将命名范围的一部分分配给数组

[英]Excel 2007 VBA Assigning part of a named range to an array

I have a table of monthly sales figures - FreqData1. 我有一张每月销售数字表-FreqData1。 Each column represents a month and is numbered 1 to 12. The user chooses one of these numbers from a dropdown list. 每列代表一个月,编号为1到12。用户从下拉列表中选择这些数字之一。

I have the code to find the column number and I have tried to assign the data in that column to an array so I can use it copy it to a different spreadsheet but with my basic VBA knowledge and despite lots of searching I have been unable to find the code as to how to do this or a different method to carry this out. 我有找到列号的代码,并且尝试将该列中的数据分配给数组,以便可以使用它将其复制到其他电子表格中,但是具有我的基本VBA知识,尽管进行了大量搜索,但我仍然无法查找有关如何执行此操作的代码或执行此操作的其他方法。

Can anyone help please 任何人都可以帮忙吗

Sub AnnualFreqMacro()

   Dim TPNoInt As Long, BranchNoInt As Long, ColNo As Long
   Dim FreqArray()

   Worksheets("Freq data").Activate
   TPNoInt = Range("B42").Value
   BranchNoInt = Range("B41").Value

   ColNo = Application.Match(TPNoInt, Range("TPBr1"), 0)

   CharaArray = Range("FreqData1").Cells (1, ColNo), Cells(16, ColNo))

   End Sub

Many thanks in advance 提前谢谢了

I think this is your answer: It's how you're using the range. 我认为这就是您的答案:这就是您使用范围的方式。

Delete your CharArray = ... line and replace with: 删除您的CharArray = ...行,并替换为:

With Range("FreqData1")
   CharaArray = .Range(.Cells(1, ColNo), .Cells(16, ColNo)) 
End With

The issue is how you're setting the range, Range().Cells(), Cells() isn't the context, you'd want something more like Range(Cells(),Cells()) . 问题是如何设置范围, Range().Cells(), Cells()不是上下文,您想要更多类似Range(Cells(),Cells())

Let's say "FreqData1" is the range A10:A20 . 假设“ FreqData1”是范围A10:A20 If you use 如果您使用

With Range("FreqData1")
    .Cells(1,1).Select 
End With

this will select the top left most cell (row 1, column 1) in the range "FreqData", so cell A10 would be selected. 这将在“ FreqData”范围内选择最左上角的单元格(第1行,第1列),因此将选择单元格A10

A final misc. 最后的杂项。 point: Avoid using .Select / .Activate . 要点: 避免使用.Select / .Activate You can activate a sheet of course so you can follow your macro, but when setting variables to ranges/cell values, etc. it's best to qualify which sheet you are referring to. 当然,您可以激活一张纸,以便您可以跟随宏,但是在将变量设置为范围/单元格值等时,最好先确定要指的是哪张纸。

Sub AnnualFreqMacro()
Dim TPNoInt As Long, BranchNoInt As Long, ColNo As Long
Dim FreqArray()
Dim freqWS  As Worksheet
Set freqWS = Worksheets("Freq data")
' Worksheets("Freq data").Activate ' removed this, since we have a variable for it now.

TPNoInt = freqWS.Range("B42").Value    ' see how I added the worksheet that we want to get B42's value from?
BranchNoInt = freqWS.Range("B41").Value
ColNo = Application.Match(TPNoInt, Range("TPBr1"), 0)
With freqWS.Range("FreqData1")    ' I'm assuming "FreqData1" is on this worksheet
    CharaArray = .Range(.Cells(1, ColNo), .Cells(16, ColNo))
End With
End Sub

I'm not positive if you have to qualify a named range's sheet, since it's a named range, but I added that just to be safe. 如果您必须限定命名范围的工作表,我不是很肯定,因为它是一个命名范围,但是我补充说是为了安全。

Edit2: Hm, oddly enough, if your named range "myRange" is A1:A10 , you can still do myRange.Range(myRange.cells(1,1),myRange.cells(1,2)) , even though there's no second column in the range, it just expands it. Edit2:嗯,奇怪的是,如果命名范围“ myRange”为A1:A10 ,即使没有,也仍然可以执行myRange.Range(myRange.cells(1,1),myRange.cells(1,2)) 。范围的第二列,它只是将其扩展。 I thought it'd throw an error, but nope. 我以为会抛出错误,但是没有。 Just to note. 只是要注意。

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

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