+ Reply to Thread
Results 1 to 11 of 11

Named Ranges from VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33

    Named Ranges from VBA

    I have some named ranges that refer to 5 pieces of data organized into a row. For example, the name MyNamedRange might refer to $C$5:$C$10.

    I am trying to loop through each column and get the values in MyNamedRange, then change corresponding values in a different named range. However, when I try to use Offset to access the subsequent columns of MyNamedRange, it doesn't work. It only gets the value of the first column right, the rest return <EMPTY>.

    Sample code:

    For ColumnIndex = 0 to 5
         MsgBox wksMyWorksheet.Range("MyNamedRange").Range("A1").Offset(0, ColumnIndex).Value
    
    Next ColumnIndex
    I started using .Range("A1") because I was getting errors before.

    Is there a simpler way to do this (or simply a way that works)?
    Last edited by negcx; 12-30-2008 at 06:45 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    The .Range("a1") is redundant; you can take it out. Otherwise, the code is fine, assuming you're looking for it to get the values in C5:G5.

    If you want to see the values of the cells in myRange,
        Dim cell As Range
        Dim wks As Worksheet
    
        Set wks = some worksheet where myRange lives
        
        For Each cell In wks.Range("myRange")
            MsgBox cell.Value
        Next cell
    Last edited by shg; 12-30-2008 at 05:59 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    The behavior I'm getting is so bizarre. My sheet has an area called Setup. These are cells that the user changes to setup the sheet. This contains a named range Setup, which includes a variety of values. When the setup is changed, comments that calculate sales forecast recommendations need to be changed. That is the purpose of this code.

    When I change the setup, nothing seems to happen with the comments, even though SetupChanged() returns TRUE. When I step through it seems that the WBDays is never = 7 (this is a row of data indicating the number of days in that week, e.g. 5, 7 ,7, 7, 5).

    Here is the code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim WeeklySalesForecast As Double
        Dim WeekIndex As Integer
        Dim RecommendedSales As Double
        Dim RecommendedSalesString As String
        
        Dim wks As Worksheet
        Dim cell As Range
        
        Set wks = wksMonth
        
        ' Sales Forecast changed
        If SetupChanged(Target) Then
            ' Fill in the weekly sales forecast recommendation comments
            
            ' Clear all the comments
            For Each cell In wks.Range("WBSalesForecast")
                cell.ClearComments
            Next cell
            
            ' Fill in comments for full weeks
            For WeekIndex = 0 To constWeekCount - 1
                If wks.Range("WBDays").Offset(0, WeekIndex).Value = 7 Then
                    RecommendedSales = 7 / _
                        wks.Range("WBDays").Offset(0, constTotalDaysColumn).Value _
                        * wks.Range("SetupSalesForecast").Value
                    RecommendedSalesString = "Recommended: " & vbCrLf & Format(RecommendedSales, "Currency")
    
                    wks.Range("WBSalesForecast").Offset(0, WeekIndex).AddComment RecommendedSalesString
                End If
            Next WeekIndex
        End If
    End Sub
    When I change one of my setup variables - the total Sales Forecast - I get a type mismatch on the if = 7 statement. I am confused.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    wks.Range("SetupSalesForecast")
    as used must refer to a single-cell range.

  5. #5
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    Sadly the code is not fine because it doesn't work. For some reason it's returning <EMPTY> rather than the appropriate values.

    The reason I'm using an index is because I'm working in two different named ranges.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Sadly the code is not fine because it doesn't work. For some reason it's returning <EMPTY> rather than the appropriate values.
    You did see that it's returning the values in C5:G5, not C5:C10, right?

    The reason I'm using an index is because I'm working in two different named ranges.
    I don't know what that means.

  7. #7
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    Quote Originally Posted by shg View Post
    You did see that it's returning the values in C5:G5, not C5:C10, right?
    Yes, that was the intention.

    I don't know what that means.
    I'm getting values from one row and using them to modify another row, both of which are governed by my Index. Effectively I'm trying to access them like two arrays.

  8. #8
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    That is a single cell range.. not having problems with that one

  9. #9
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    wks.Range("WBDays").Offset(0, WeekIndex).Value
    seems to be the culprit. It seems like this should work but it's simply not working.

  10. #10
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    I seem to have gotten it to (mostly) working by using .Cells instead of .Offset. Now my problem is that the routine accesses cells that are in the process of being updated. When these cells are being updated my routine simply clears the comments, and does not add new ones. When I change a Setup cell that does not cause the WBDays named range to be in the process of updating, the comments are added..

    Is there a way to call my function after the worksheet has already recalculated?

    Thanks for your help.

  11. #11
    Registered User
    Join Date
    11-26-2008
    Location
    San Francisco
    Posts
    33
    Created new subroutine, moved to Worksheet_Calculate. Solved-

    thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1