+ Reply to Thread
Results 1 to 5 of 5

Select range of data VBA

Hybrid View

hanif Select range of data VBA 08-25-2016, 10:34 AM
WasWodge Re: Select range of data VBA 08-25-2016, 10:43 AM
hanif Re: Select range of data VBA 08-25-2016, 11:00 AM
hanif Re: Select range of data VBA 08-25-2016, 11:06 AM
WasWodge Re: Select range of data VBA 08-25-2016, 11:40 AM
  1. #1
    Forum Contributor
    Join Date
    11-09-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    391

    Select range of data VBA

    Hi All,

    I am trying to select a range of data using VBA. I have months (Jan - Dec) from A10 to A21 and a range of data for each month. I want to select the data from A10 to the last col used in row 21. for example if the last col is H then A10:H21.

    I used following code but doesn't work. Any idea how to fix it?

        mnthlylc = wsElecOtpt.Cells(10, Columns.Count).End(xlToLeft).Column
        mnthlylr = wsElecOtpt.Range(wsElecOtpt.Cells(21, mnthlylc)).End(xlUp).Row
        Set rng = .Range(.Cells(10, "A"), .Cells(mnthlylr, mnthlylc))

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Select range of data VBA

    In what way is it not working? and can you post all the code inside the With statement.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Forum Contributor
    Join Date
    11-09-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    391

    Re: Select range of data VBA

    well I am plotting a chart on a userform using vba and trying to select the range of data I want to display on chart. it does work if I select rng=.range("A10:H21") but no of Cols are varies could be 3 could be 5 could be 2 so I try to define to select a range from A10 to the cell in row 21 and last col used.

    since the whole code is too long I only posted the bit I need to change but if that is help here's the complete code;

    If cmboChrt.Text = "" Then
    
        MsgBox "Please select a chart from dropdown list"
        Exit Sub
        
    End If
    
    If cmboChrt.Text = "Monthly Electricity Consumption" And Sheets("Energy Summary").Range("B10").Value = "" Then
        MsgBox ("Please upload electricity data to create a chart")
    End If
    
    If cmboChrt.Text = "Monthly Electricity Consumption" And Sheets("Energy Summary").Range("B10").Value <> "" Then
    
        '~~> Set the sheet where you have the charts data
        Set wsElecOtpt = Sheets("Energy Summary")
        mnthlylc = wsElecOtpt.Cells(9, Columns.Count).End(xlToLeft).Column
        mnthlylr = wsElecOtpt.Range(wsElecOtpt.Cells(21, mnthlylc)).End(xlUp).Row
    
        '~~> This is your charts range
        With wsElecOtpt
    '            x = .Range(.Cells(10, mnthlylc), .Cells(mnthlylr, mnthlylc)).Address(False, False)
                Set rng = .Range(.Cells(9, "A"), .Cells(mnthlylr, mnthlylc))
         End With
    
        '~~> Delete the temp sheeet if it is there
        Application.DisplayAlerts = False
        On Error Resume Next
        ThisWorkbook.Sheets("TempOtpt").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
        '~~> Add a new temp sheet
        Set wsTemp = ThisWorkbook.Sheets.Add
    
        With wsTemp
            '~~> Give it a name so that I can delete it as shown above
            .Name = "TempOtpt"
    
            '~~~> Add the chart
            Set oChrt = .ChartObjects.Add _
                (Left:=50, Width:=560, Top:=75, Height:=370)
    
            '~~> Set the chart's source data and type
            With oChrt.Chart
                .SetSourceData Source:=rng
                .ChartType = xlLine
                .ApplyDataLabels xlDataLabelsShowLabelAndPercent
                .HasLegend = False
                .HasTitle = True
                .ChartTitle.Text = "Monthly Electricity Consumption"
                .ChartStyle = 232
                'X Axis
                .Axes(xlCategory, xlPrimary).HasTitle = True
                .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Months"
                'Y Axis
                .Axes(xlValue, xlPrimary).HasTitle = True
                .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "MWh"
                .Axes(xlValue, xlPrimary).DisplayUnit = xlThousands
                .Axes(xlValue, xlPrimary).DisplayUnitLabel.Delete
            End With
            
        End With
    
        '~~> Export the chart as bmp to the temp drive
        oChrt.Chart.Export Filename:=TempPath & "TempChart.bmp", Filtername:="Bmp"
    
        '~~> Load the image to the image control
        Me.Image1.Picture = LoadPicture(TempPath & "TempChart.bmp")
    
        '~~> Delete the temp sheet
        Application.DisplayAlerts = False
        wsTemp.Delete
        Application.DisplayAlerts = True
    
        '~~> Kill the temp file
        On Error Resume Next
        Kill TempPath & "TempChart.bmp"
        On Error GoTo 0
    End If

  4. #4
    Forum Contributor
    Join Date
    11-09-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    391

    Re: Select range of data VBA

    I was able to figure it out. If anyone having same problem or interested I changed the code as follow

                x = .Range(.Cells(10, "B"), .Cells(21, mnthlylc)).Address(False, False)
                Set rng = .Range("A10:A21," & x)

  5. #5
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Select range of data VBA

    With your original method....

    Dim wsElecOtpt As Worksheet, mnthlylc As Long, mnthlylr As Long, Rng As Range
    '...MORE CODE
        mnthlylc = wsElecOtpt.Cells(9, Columns.Count).End(xlToLeft).Column
        mnthlylr = wsElecOtpt.Cells(Rows.Count, mnthlylc).End(xlUp).Row
    
        '~~> This is your charts range
        With wsElecOtpt
            Set Rng = .Range(.Cells(10, "B"), .Cells(mnthlylr, mnthlylc))
            Rng.Select
        End With

+ 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. Select range of data dynamic
    By Florian56 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2016, 04:32 PM
  2. Help with Date Range to select data
    By Bobbetski in forum Excel General
    Replies: 1
    Last Post: 10-12-2014, 09:51 PM
  3. Replies: 3
    Last Post: 09-03-2014, 09:59 AM
  4. Select specific range in last row with data
    By violetmind in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-13-2014, 01:55 AM
  5. Replies: 4
    Last Post: 06-17-2013, 05:21 AM
  6. Select a set of data that matchs a range then input said data into a new column SOS
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-04-2012, 07:46 PM
  7. Select / range data
    By ciprian in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2011, 02:49 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