+ Reply to Thread
Results 1 to 5 of 5

vba code to restrict horizontal categories items repetition

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    vba code to restrict horizontal categories items repetition

    Hi

    I have chart as shown in attached file. It plots 12 months data.

    I want to restrict it to plot only such number of months as is mentioned in Cell H1.

    Thank you
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: vba code to restrict horizontal categories items repetition

    Is there no solution to this problem?

  3. #3
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: vba code to restrict horizontal categories items repetition

    This may work for this one..

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lR As Long
        Dim rng1, rng2 As String
        If Not Intersect(Target, Range("H1")) Is Nothing Then
            lR = CLng(Range("H1").Value) + 2
            rng1 = Range(Cells(5, 2), Cells(7, lR)).Address
            rng2 = Range(Cells(10, 2), Cells(10, lR)).Address
            ActiveSheet.ChartObjects(1).Activate
            ActiveChart.SetSourceData Source:=Range(rng1, rng2)
            Range("H1").Select
        End If
    End Sub

  4. #4
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: vba code to restrict horizontal categories items repetition

    Thank you Gerard, it works superbly but I want to move the cells H1 to some other sheet, say sheet xyz.

    Then what will be change in the code

  5. #5
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: vba code to restrict horizontal categories items repetition

    put this in the sheet you want to make the choise (H1 in this case)

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lR As Long
        Dim rng1, rng2 As String
        If Not Intersect(Target, Range("H1")) Is Nothing Then
            Application.ScreenUpdating = False
            lR = CLng(Range("H1").Value) + 2
            With Sheets("Yearly")
                .Activate
                rng1 = Range(Cells(5, 2), Cells(7, lR)).Address
                rng2 = Range(Cells(10, 2), Cells(10, lR)).Address
                ActiveSheet.ChartObjects(1).Activate
                ActiveChart.SetSourceData Source:=Sheets("Yearly").Range(rng1, rng2)
                .Cells(1, 1).Select
            End With
            Sheets("xyz").Activate
            Application.ScreenUpdating = True
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Creating horizontal line chart with categories in y-axis
    By riya.arora. in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 04-09-2013, 04:55 AM
  2. Repetition code with variables
    By superdonk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2013, 08:20 AM
  3. Replies: 1
    Last Post: 06-15-2012, 03:57 AM
  4. how can i loop this code to reduce the repetition?
    By jay11 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-11-2010, 04:22 AM
  5. Repetition of Code
    By Steve in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2006, 11:55 AM

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