• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Manipulating elements in Named Range

chirayu

Well-Known Member
Hi All,

I have a Named range that has hardcoded values inside it. These Values are populated from another Named Range whenever the macro is run.

What I would like to do is be able to manipulate values in the generated Named Range.

Code:
Sub Test()

'Create RngB from RngA - RngA is A1:A10
ThisWorkbook.Names.Add "RngB", [RngA].Value

'Here I need a code that can create an Array from RngB
'& then replace values basis whatever criteria I specify
'e.g. check each value & if #N/A then replace with 0

End Sub
 
Do it using formula in A1:A10 before loading the named range
in A1: =Iferror(current formula, 0)
 
Can we just go to the source? By that, rather than have RngB based on RngA, and then try to extract from A, why not have our array pull from RngA directly?
Code:
Sub Test()
Dim myArray As Variant
Dim i As Long

myArray = [RngA].Value
For i = LBound(myArray) To UBound(myArray)
    If WorksheetFunction.IsError(myArray(i, 1)) Then
        myArray(i, 1) = 0
    End If
Next i

[RngA].Value = myArray

'Now we make B based on A

'Create RngB from RngA - RngA is A1:A10
ThisWorkbook.Names.Add "RngB", [RngA].Value

End Sub
 
Thanks @Hui and @Luke M

Luke I made a few changes as I don't need to change the values of RngA. Directly load the correct values into RngB

Removed
Code:
[RngA].Value = myArray

Changed
Code:
ThisWorkbook.Names.Add "RngB", myArray

Going to use it with conjunction with this post to handle #N/A
http://chandoo.org/forum/threads/dynamic-chart-vba-smoother-animation.24866/

So main code in that becomes

Code:
Public Sub AnimateChart()

    Dim i As Integer
    Dim myArray As Variant
    Dim x As Long
       
    bFinished = False
   
    myArray = [chart_values]
    For x = LBound(myArray) To UBound(myArray)
        If WorksheetFunction.IsError(myArray(x, 1)) Then
            myArray(x, 1) = 0
        End If
    Next x
   
    ThisWorkbook.Names.Add "old", myArray
   
    ThisWorkbook.Names.Add "switch_to_record", [record_index].Value
    If [b_animation] Then
        For i = 1 To [animation_steps]
            [step] = i
            DoEvents
            If bFinished Then Exit For
        Next i
    End If
    bFinished = True

End Sub
 
Last edited:
Back
Top