0

I have an excel workbook with two sheets, named "Dashboard Supplier View' and "ComputingSolutions". In the dashboard supplier view tab, the user has to write down a value in the named cell "categoryName". When the user writes down "Computing Solutions" in this named cell and runs the code, I want the code to go to the ComputingSolutions sheet and to store all the suppliernames in column A in that sheet into an array, to then print out all suppliernames in the original sheet Dashboard Supplier View.

The "n" variable refers to a named cell on the ComputingSolutions sheet and is the number of suppliers on that sheet. (This number is written down already, the user does not need to change this)

When I run the code, nothing shows up. Nothing is printed on the dashboard supplier view sheet at the designated location. I get no error either. I checked all names and everything is spelled correctly. I'm not sure what could be the issue, but I suspect it has something to do with the activation of the worksheets.

Public Sub DashboardSupplier()

Dim category As String
Dim supplierArray() As String
Dim c As Integer

Worksheets("Dashboard Supplier View").Activate

category = Names("categoryName").Value

'Suppliers for selectec category

If category = "Computing Solutions" Then

Worksheets("ComputingSolutions").Activate

n = Names("supplierAmount").Value

ReDim supplierArray(1 To n) As String

    For c = 1 To n

    supplierArray(c) = Cells(3 + c, 1)

    Next c

Worksheets("Dashboard Supplier View").Activate

    For c = 1 To n

    Cells(6 + c, 4) = supplierArray(c)

    Next c

   End If

   End Sub
16
  • how does the n variable relate to a named cell? Is the cell named n? Commented Aug 1, 2016 at 15:48
  • Have you tried debugging? Put a break (eg) inside the If category = "Computing Solutions" Then block - does your code reach that point? Commented Aug 1, 2016 at 15:50
  • n refers to the named call supplierAmount via n = Names("supplierAmount").Value Commented Aug 1, 2016 at 15:50
  • Yep, sorry, I just read that, my bad. Commented Aug 1, 2016 at 15:50
  • 1
    Change all your Names by Range Commented Aug 1, 2016 at 16:18

1 Answer 1

2

OK, I think I have a solution to this because it was bothering me. I set up your environment as a test, and found that for some reason, Names("categoryName").Value was returning the cell address, i.e. it was returning Names("categoryName").RefersTo. Try swapping the .value part for .RefersToRange.Value and see if you have better luck. Here is my working formatted code:

Public Sub DashboardSupplier()
    Dim category As String
    Dim supplierArray() As String
    Dim c As Integer
    Dim n As Long
    Worksheets("Dashboard Supplier View").Activate
    category = Range("categoryName").Value
    'Suppliers for selectec category
    If category = "Computing Solutions" Then
        Worksheets("ComputingSolutions").Activate
        n = CLng(Range("supplierAmount").Text)
        ReDim supplierArray(1 To n) As String
        For c = 1 To n
            supplierArray(c) = Cells(3 + c, 1)
        Next c
        Worksheets("Dashboard Supplier View").Activate
        For c = 1 To n
            Cells(6 + c, 4) = supplierArray(c)
        Next c
    End If
End Sub

Unfortunately, I have no explanation for this behavior.

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

9 Comments

This gives me Run-Time error 1004:Application-defined or object-defined error. I copied your entire code and used it. I'm baffled
F8 through it and tell me where that pops up.
Here: n = Names("supplierAmount").RefersToRange.Value
What version of Office are you using?
If you just change Names by Range you'll get the value contained in that named range cell.
|

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.