简体   繁体   中英

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

I have a table of monthly sales figures - FreqData1. Each column represents a month and is numbered 1 to 12. The user chooses one of these numbers from a dropdown list.

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.

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:

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()) .

Let's say "FreqData1" is the range 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.

A final misc. point: Avoid using .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. I thought it'd throw an error, but nope. Just to note.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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