Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Selecting Cells between blanks for use in graphs.

    I'm trying to write a VBA code that'll select cells in column B that contains text until it comes to a blank row, the amount of data changes each time, i've tried two different approches but i'm unable to get it to hold the cell, it only moves down one cell each time i run the macro, no errors though.

    If anyone could take a looksie at my code so far and give me a point in the right direction it would be greatly appreciated

    Code:
    Sub Test()
    
    With Range("B2", Range("B65536").End(xlUp))
    
    Do
    If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
    End If
    Loop Until IsEmpty(Active) = True
    
    
    End With
    End Sub
    I altso tried a slighty different approach but that didn't even move the selected cell down, no errors though..

    Code:
    Sub Tester()
    
    Dim Var1 As Long, Var2 As Long
        
        Var2 = 0
        Do While Var2 > 0
            Var2 = ActiveCell.Value
            ActiveCell.Offset(1, 0).Select
            If Var1 = Var2 Then
            MsgBox "works" ' this is just to see if the code runs properly to this point.
            End If
        Loop
    
    End Sub
    Last edited by prefix; 09-21-2009 at 10:54 AM.

  2. #2
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    This has been driving me nuts for the past few days. I'll offer up the entire code if you guys help me. Its about 2000 lines of code

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Selecting Cells between blanks for use in graphs.

    Rather than the code can you post small example of the data that appears to be causing the problem.

    Let us know what range you expect to be used.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    Righto, here's a sample of the sheet i am working on, I want it to start at B2 and work its way down, selecting all cells in that column until it reaches a blank row.
    Attached Files Attached Files

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Selecting Cells between blanks for use in graphs.

    This will select B1:B55 in your example file.

    Code:
    range("B1",range("B1").End(xlDown)).Select
    Cheers
    Andy
    www.andypope.info

  6. #6
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    I don't think you understood my question, i could easily write a 1 line of code that selects a predetermined range, but i want it to be able to search down the column and select all the cells until it hits a blank one, Since the amount of data will change over time. The only range i know that will be the same is B2. by the way that code will probably come in handy i didn't know you could express it like that and it'd walk down until the closest empty row(?), perhaps i could use it in some way or another.

    Upon experimenting with that code i find that i cannot select more than 1 range, i need to select another column as well, but once i issue another range the first one will be lost.

    Code:
    Range("B2", Range("B2").End(xlDown)).Select
    Range("E2", Range("E2").End(xlDown)).Select
    Last edited by prefix; 09-17-2009 at 08:51 AM.

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Selecting Cells between blanks for use in graphs.

    So not to the blank cell but rather all cells in the column that have a value.

    Code:
    range("B:B").SpecialCells(xlCellTypeConstants).Select
    Cheers
    Andy
    www.andypope.info

  8. #8
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    Sorry, i should have made myself alot clearer, I want to use the selected cells to make a graph, so i would need to select two columns, then make a graph, then move on to the next set of cells that are below the blank rows.

    I wish it was as easy as selecting all the cells in that row that has data in them but that won't enable me to create X number of graphs will it? it will result in 1 giant graph for the entire sheet, which is not what i want.. I know, its a pain :X

  9. #9
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    After experimenting abit i was able to come up with this code, think i may be able to work it into doing what i want

    Code:
    Sub Test()
    
    Dim Var1 As Range
    Dim Var2 As Range
    Dim myMultiAreaRange As Range
    
    Set Var1 = Range("B2", Range("B2").End(xlDown))
    Set Var2 = Range("E2", Range("E2").End(xlDown))
    
    Set myMultiAreaRange = Union(Var1, Var2)
    myMultiAreaRange.Select
    
    End Sub
    Thanks for the help so far )

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Selecting Cells between blanks for use in graphs.

    Let's stop for a minute and work out exactly what the problem is.

    You want to make multiple charts from the non contiguous data, yes?

    If thats the case then we need more information.

    Chart sheets or chartobjects?
    If chartobjects, 1 per new worksheet or laid out on existing/new worksheet?
    What type of chart?
    What 2 columns will the chart be based on?
    Cheers
    Andy
    www.andypope.info

  11. #11
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    Chart Objects, Laid out on Existing Worksheet(001) More than 1 per worksheet, will differ depending on the amount of data.

    columns B and E contain the data used for the graphs.

    This is what i've got so far, Actually i think it just might be working now lol
    Had some error earlier but reloaded the entire Worksheet and now it worked like a charm :D

    Now i just need to have it jump down 4 blank rows and do the exact same thing for the next set of data on the spreadsheet, then for the next set of data the offset would be 2(Since the first breaker is 4 rows and the next ones are 2, dont ask me why, that's how i had to do it, but its the same for all my spreadsheets and it is a fact, and does not change.) order to create graphs for all the data in the spreadsheet(s).
    Code:
    Sub Test()
    
    Dim Var1 As Range
    Dim Var2 As Range
    Dim mRange As Range
    
    Set Var1 = Range("B2", Range("B2").End(xlDown))
    Set Var2 = Range("E2", Range("E2").End(xlDown))
    
    Set mRange = Union(Var1, Var2)
    mRange.Select
        Charts.Add
        ActiveChart.ChartType = xlXYScatter
        ActiveChart.SetSourceData Source:=Sheets("001").Range(mRange.Address), _
            PlotBy:=xlColumns
        ActiveChart.Location Where:=xlLocationAsObject, Name:="001"
        With ActiveChart
            .HasTitle = True
            .ChartTitle.Characters.Text = "Norm Type vs Xbar"
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Norm Type"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Xbar"
        End With
    
    
    End Sub
    Last edited by prefix; 09-17-2009 at 10:11 AM.

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Selecting Cells between blanks for use in graphs.

    Column B of your example files contains Norm_Type data such as A010 and B040. If the X values are Text then you do not need to specify a range.

    try this on your sample file.
    Code:
    Sub Test()
    
        Dim rngData As Range
        Dim rngArea As Range
        Dim objChart As ChartObject
        Dim xx As Series
        
        Set rngData = Range("E2", Cells(Rows.Count, 5).End(xlUp)).SpecialCells(xlCellTypeConstants)
        
        For Each rngArea In rngData.Areas
            Set objChart = ActiveSheet.ChartObjects.Add(rngArea.Left + rngArea.Width, rngArea.Top, 400, 350)
            With objChart.Chart
                .PlotBy = xlColumns
                Do While .SeriesCollection.Count > 0
                    .SeriesCollection(1).Delete
                Loop
                With .SeriesCollection.NewSeries
                    .Values = rngArea
                    .Name = rngArea.Cells(1, 1).Offset(0, -3)
                    .ChartType = xlXYScatter
                End With
                .HasTitle = True
                .ChartTitle.Characters.Text = "Norm Type vs Xbar"
                .Axes(xlCategory, xlPrimary).HasTitle = True
                .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Norm Type"
                .Axes(xlValue, xlPrimary).HasTitle = True
                .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Xbar"
            End With
        Next
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  13. #13
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    Awesome!! That's using the old noggin! :D I'd have never figured this out by myself, i would've written variables until my fingers were bleeding

    Thanks a bunch, i'll be trying to integrate this code into my worksheet and see how it goes )

  14. #14
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    !%&!" Amazing.. It worked like a charm for all my sheets. You have no idea how much time you've saved me with this code :D 1000 Thanks!

  15. #15
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    143

    Re: Selecting Cells between blanks for use in graphs.

    Ran into an issue when i was working on this to change it from Column B2 to C2 on the worksheet, It graphs two graphs for each graph and the graph behind the first one includes the entire worksheet.. whats wrong is it that the column contains only numbers? How can i fix this?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0