+ Reply to Thread
Results 1 to 6 of 6

code for data range selection to create a line graph in a macro

  1. #1
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    code for data range selection to create a line graph in a macro

    Hello

    I have some code in the start of my macro that looks for the last row in column P and then selects all that column all the way upto row 17 which works fine. The next bit of code was generated by me recording a macro of inserting a line graph of the data previously selected at the start of the macro and naming the graph "subtwist" and scaling it a bit to suit the size of my screen.

    It all works fine but the problem is that it does not quite work how I need it when I run it on a different file. On the original file there was 3677 lines of data which you can see from the section of code where it is making the graph. If I run this macro on a new sheet that for example only has 1500 lines of data it produces a graph from 3677 lines. How can I make the graph select the data range that the first bit of code identifies?

    thanks

    here is the code I have in the macro.

    Sub RawLogProcessing()
    '
    ' RawLogProcessing Macro
    '
    ' Keyboard Shortcut: Ctrl+d
    '
    ' Select Data from bottom of column P up and stop at P17
    '
    Dim lr As Long
    Dim rngAllData As Range

    lr = Worksheets("Sheet1").Cells(Rows.Count, "P").End(xlUp).Row

    Set rngAllData = Worksheets("Sheet1").Range("P17:P" & lr)
    rngAllData.Select

    'insert the subtwist graph

    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    ActiveChart.SetSourceData Source:=Range("Sheet1!$P$17:$P$3677")
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.85625, msoFalse, _
    msoScaleFromBottomRight
    ActiveSheet.Shapes("Chart 1").ScaleHeight 0.9670140712, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.430976431, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleHeight 1.0143626544, msoFalse, _
    msoScaleFromTopLeft
    ActiveChart.ChartTitle.Select
    ActiveChart.ChartTitle.Text = "Subtwist"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Subtwist"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select


    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    01-10-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    651

    Re: code for data range selection to create a line graph in a macro

    change:
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Hope I could help - if your post has been solved don't forget to mark it as such.

    Clicking the * below is a great way to say thanks!

    "Drowning in 10 feet of water isn?t any different than drowning in a million feet. And if you can swim, it doesn?t matter how deep the ocean is. At some level, once you realize you?re in water that?s too deep to stand, you have to have a very different approach," - Joi Ito

  3. #3
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    Re: code for data range selection to create a line graph in a macro

    That worked just fine thanks. I want to add a graph underneath the first graph based on a different column of data. I tried to copy and past all the code and just change the references to column P to column J and give the graph a different title and thought that would work but it does not. When I run the macro it stop stops at the second line of "Dim lr As Long" and an error box pops up saying "duplicate declaration in current scope".

    Another query is should I be splitting the code up into chunks, ie subroutine 1 for the first graph and subroutine 2 for the second one. If so how do I do this? I was somehow wanting manageable sections for each thing the macro is doing more for housekeeping to understand the macro when reviewing it say several months later.

    Here is what I have in the code so far

    Sub RawLogProcessing()
    '
    ' RawLogProcessing Macro
    '
    ' Keyboard Shortcut: Ctrl+d
    '
    ' Select Data from bottom or column P up and stop at P17
    '
    Dim lr As Long
    Dim rngAllData As Range

    lr = Worksheets("Sheet1").Cells(Rows.Count, "P").End(xlUp).Row

    Set rngAllData = Worksheets("Sheet1").Range("P17:P" & lr)
    rngAllData.Select

    'insert the subtwist graph

    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select

    ActiveChart.SetSourceData Source:=Range("Sheet1!$P$17:$P$" & lr)
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.85625, msoFalse, _
    msoScaleFromBottomRight
    ActiveSheet.Shapes("Chart 1").ScaleHeight 0.9670140712, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.430976431, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleHeight 1.0143626544, msoFalse, _
    msoScaleFromTopLeft
    ActiveChart.ChartTitle.Select
    ActiveChart.ChartTitle.Text = "Subtwist"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Subtwist"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select

    'Select Data from bottom of column J up and stop at J17
    '
    Dim lr As Long
    Dim rngAllData As Range

    lr = Worksheets("Sheet1").Cells(Rows.Count, "J").End(xlUp).Row

    Set rngAllData = Worksheets("Sheet1").Range("J17:J" & lr)
    rngAllData.Select

    'insert the Vibration graph

    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select

    ActiveChart.SetSourceData Source:=Range("Sheet1!$j$17:$j$" & lr)
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.85625, msoFalse, _
    msoScaleFromBottomRight
    ActiveSheet.Shapes("Chart 1").ScaleHeight 0.9670140712, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleWidth 1.430976431, msoFalse, _
    msoScaleFromTopLeft
    ActiveSheet.Shapes("Chart 1").ScaleHeight 1.0143626544, msoFalse, _
    msoScaleFromTopLeft
    ActiveChart.ChartTitle.Select
    ActiveChart.ChartTitle.Text = "Vibration"
    Selection.Format.TextFrame2.TextRange.Characters.Text = "Subtwist"
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
    End With
    With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
    End With
    ActiveChart.ChartArea.Select

    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    01-10-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    651

    Re: code for data range selection to create a line graph in a macro

    1) you can remove the second line "dim lr as long" you've already declared lr as long within this sub and do not need to do so again. You'll also need to remove the second dim rngAllData line
    2) it is pretty much entirely a matter of preference as to whether you break the code into two sections or not. do you want to be able to run these to subs separately? i.e. only create the second graph but not the first sometimes? if so then you could probably handle that with an if then type statement, but it may be easier / cleaner to break them into separate routines and associate each one with a different button or shortcut (or however you want to call them...)
    3) to break the code into two separate subroutines, simply put End Sub at the end of the first and open up the second with a new title "sub MySecondSubroutine()" would do the trick. If you do so you will need to duplicate the dim lines i just told you you can remove.

    As for the housekeeping:
    I'd generally suggest this code is not all that complex and it is relatively straightforward to follow it, so you don't necessarily need any additional housekeeping, but your idea to break it into two sections would be fine, especially if they gradually get more complex over time...

    some other ideas / rules of thumb -
    whenever you have a block of code which is related in some way (as in after creating a loop or declaring with ___) i usually will indent the following lines of code - this helps keep things straight so people can jump straight through entire loops, if or with statements without necessarily reading every line but still understanding what they are doing.

    generally you can delete any time you see ".select" as this basically does nothing as far as vba is concerned and is usually only input to help you in troubleshooting or stepping through each line to understand what it's doing. in some cases you may see back to back lines (especially from recording macros) where something ends with .select and the next line starts with selection. ... these can be merged into a single line by deleting ".select" and "selection"

    it is generally good practice to avoid calling "active" anything - if the code is run while the user has selected another sheet vba will start performing operations on the new sheet... i'd suggest you are generally better off declaring your objects up front (i.e. dim ws as worksheet), setting those objects (i.e. set ws = sheets("MySheetName") and then calling the object later instead of the activesheet (i.e. ws.Shapes.AddChart2(332, xlLineMarkers))

    some of the objects / variables / properties you see getting specified after recording a macro are optional and can be deleted as well. i'd suggest googling "excel chart objects" or visiting https://msdn.microsoft.com/en-us/lib.../ff194426.aspx or http://www.java2s.com/Code/VBA-Excel...sinVBACode.htm for more information on what the various methods / properties etc... are when working with charts.

    i'd consider none of this mandatory, but these are the things i've picked up on over the last few months of trying to develop my vba coding abilities, and i think they help to give you a cleaner end result which is a little easier to understand (for those who are accustomed to reading vba anyway...)
    Last edited by simarui; 04-09-2015 at 12:00 PM.

  5. #5
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    Re: code for data range selection to create a line graph in a macro

    Simarui, Thanks very much for the time for your hints and tips and solving my problem. your help is very much appreciated and I will use your suggestions.

  6. #6
    Valued Forum Contributor
    Join Date
    01-10-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    651

    Re: code for data range selection to create a line graph in a macro

    glad i could help and thanks for the rep! don't forget to mark the post solved

+ 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. Create a code for offsetting range selection by 1 column
    By amartino44 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2013, 12:56 PM
  2. Code for Macro for Line-Column 2 axis (y) Graph
    By jackofspades4395 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 05:17 AM
  3. Replies: 1
    Last Post: 09-11-2012, 03:07 AM
  4. How to create 2d line Graph using connected cells of about 750 times using Macro???
    By diljeet1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-18-2012, 01:54 AM
  5. How do you create a colored probability range around a graph line
    By Caerus in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 12-22-2005, 05:20 PM

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