Consulting

Results 1 to 9 of 9

Thread: Using a Variable in a named range

  1. #1

    Using a Variable in a named range

    Hi All......

    Can someone please help me with my problem? - I have a worksheet that is set out as a form for a user to fill in.
    Near the top of the form there is a validation list that the user must select a code from.
    On a seperate hidden sheet I have several ranges of data, each range has its own code which I have defined with a named range that corresponds to the code in the validation list.
    The object is that depending on the code selected by the user, the form is populated with a range that corresponds to the code.
    I can get the macro to run if I hard code the Input code, but I cannot get it to run if I use a variable.
    Thanks in advance
    Dave
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
        Dim sc As Range    '** a variable to hold the contents of J4
        
    '  ** Check if Cell J4 is updated
      If Not Intersect(Target, Range("J4")) Is Nothing Then
      
       Set sc = Range("J4")
        
      Sheet3.Visible = xlSheetVisible    '** The hidden sheet with the ranges on
      Sheet3.Select
      
     ' [H1N].Select  '** IF I HARD CODE THE DETAILS IN J4 IT WORKS - H1N is a code in the list selected by the user
      [" & sc & "].Select   '** THIS FAILS HERE - ASKING FOR AN OBJECT
      Selection.Copy
      
      Sheet10.Select
      
      Range("A11").PasteSpecial xlPasteValues
          
      End If
      
      Application.ScreenUpdating = True

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Try using:

    Application.Range(sc).Copy
    instead of your two Select and Copy lines.
    Be as you wish to seem

  3. #3
    Wow!!!..... Thanks Aflatoon - that works perfectly!!!
    Thanks very much!!!
    Dave

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    or
    Private Sub Worksheet_Change(ByVal Target As Range)     '  ** Check if Cell J4 is updated
        If Not Intersect(Target, Range("J4")) Is Nothing Then
            Sheet10.Range("A11").Value = Target.Value
        End If
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Hi mdmackillop, - thanks very much for your response, I appreciate it! - I am very interested in learning new ways and improving my vba, and your reply looks very concise and impressive, but when I run the code, it just puts the contents of J4 into A11 and not the range that J4 is supposed to refer to. - It should fill A11:H16 with data specific to the code in J4.
    I don't know if it makes a difference, but the ranges that are referred to are on a different sheet ( sheet3)
    the ranges that are being referred to have Vlookup formulas in them, hence the need to paste values when copying.
    Regards

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    2 options (if i have it right this time)
    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.ScreenUpdating = False
        Dim sc As Range '** a variable to hold the contents of J4
         
         '  ** Check if Cell J4 is updated
        If Not Intersect(Target, Range("J4")) Is Nothing Then
            Sheet3.Range(Target).Copy
            Sheet10.Range("A11").PasteSpecial xlPasteValues
        End If
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
         '  ** Check if Cell J4 is updated
        Dim r As Range
        If Not Intersect(Target, Range("J4")) Is Nothing Then
            Set r = Sheet3.Range(Target)
            Sheet10.Range("A11").Resize(r.Rows.Count, r.Columns.Count).Value = r.Value
        End If
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Hi mdmackillop,
    Thanks a stack for these two options, - they both work perfectly!!!
    I think the second option seems to be quicker...... you can hardly see any transition
    THANKS again!!!!

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Address = "$J$4" Then
        With Sheet3.Range(Target)
          Sheet10.Range("A11").Resize(.Rows.Count, .Columns.Count) = .Value
        End With
      End If
    End Sub

  9. #9
    Wow!!!......... it keeps getting better and better!!
    Thanks Snb
    I have learn't a lot from this post - Thanks very much to you all....... GREATLY appreciated... you are ALL gurus.
    3 great snippets that I have saved........

Posting Permissions

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