0

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

1
  • 1
    I think this may work: Delete your CharArray = ... line and replace with: With Range("FreqData1") // CharaArray = .Range(.Cells(1, ColNo), .Cells(16, ColNo)) // End With (note the // is just to show where a line break is, don't actually include those). 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()). Commented Feb 8, 2016 at 16:31

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.