2 questions: sheet reference and adding chart

moradisndat

New Member
Joined
Feb 19, 2021
Messages
15
Office Version
  1. 2007
Platform
  1. Windows
I hope it's ok to ask 2 questions in 1 post if they are not related.

Is there any difference in how fast the statements below will run? I'm just trying to find the best practice.

ThisWorkbook.Sheets("NHL").Visible=False
or
Sheets("NHL").Visible=False

or can object variable work
wsNHL.Visible=False
---------------------------------------
2nd question:
I was trying to use object variables to speed up code but had problems adding charts. The only way I could make it work was by using "ActiveSheet." Is there a better way to add an embedded chart than what I have below?

LR = ActiveCell.CurrentRegion.End(xlDown).Row
wsNew.Range("H13", "I" & LR).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart. ' several lines follow to change ' chart properties

Thanks in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Is there any difference in how fast the statements below will run? I'm just trying to find the best practice.

ThisWorkbook.Sheets("NHL").Visible=False
or
Sheets("NHL").Visible=False

or can object variable work
wsNHL.Visible=False
My guess is that any difference in efficiency would be negligible.

However, there's a difference between ThisWorkbook.Sheets("NHL").Visible=False and Sheets("NHL").Visible=False. The former refers to the workbook running the code, whereas the latter refers to the active workbook since the workbook isn't specified.

Also, it's always a good idea to assign your sheet to an object variable so that you can easily refer to it without having to make sure that the appropriate workbook is the active workbook, as would be the case with Sheets("NHL").Visible=False.

I was trying to use object variables to speed up code but had problems adding charts. The only way I could make it work was by using "ActiveSheet." Is there a better way to add an embedded chart than what I have below?

LR = ActiveCell.CurrentRegion.End(xlDown).Row
wsNew.Range("H13", "I" & LR).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart. ' several lines follow to change ' chart properties
Here's an example...

VBA Code:
Option Explicit

Sub test()

    'get the source worksheet (change the workbook and sheet reference accordingly)
    Dim sourceWorksheet As Worksheet
    Set sourceWorksheet = ThisWorkbook.Worksheets("Sheet1")
    
    'get the last used row in Column H from the source worksheet
    Dim lastRow As Long
    With sourceWorksheet
        lastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
    End With

    'get the source range
    Dim sourceRange As Range
    Set sourceRange = sourceWorksheet.Range("H13:I" & lastRow)

    'create the chart (change the sheet reference accordingly)
    Dim shp As Shape
    With ActiveSheet
        Set shp = .Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=.Range("B2").Left, Top:=.Range("B2").Top, Width:=360, Height:=220)
    End With
    
    'set the properties
    With shp
        'set Shape object properties
        .LockAspectRatio = msoTrue
        '
        '
        With shp.Chart
            'set Chart object properties
            .SetSourceData sourceRange
            '
            '
            '
        End With
    End With

End Sub

Hope this helps!
 
Upvote 0
Solution
My guess is that any difference in efficiency would be negligible.

However, there's a difference between ThisWorkbook.Sheets("NHL").Visible=False and Sheets("NHL").Visible=False. The former refers to the workbook running the code, whereas the latter refers to the active workbook since the workbook isn't specified.

Also, it's always a good idea to assign your sheet to an object variable so that you can easily refer to it without having to make sure that the appropriate workbook is the active workbook, as would be the case with Sheets("NHL").Visible=False.


Here's an example...

VBA Code:
Option Explicit

Sub test()

    'get the source worksheet (change the workbook and sheet reference accordingly)
    Dim sourceWorksheet As Worksheet
    Set sourceWorksheet = ThisWorkbook.Worksheets("Sheet1")
   
    'get the last used row in Column H from the source worksheet
    Dim lastRow As Long
    With sourceWorksheet
        lastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
    End With

    'get the source range
    Dim sourceRange As Range
    Set sourceRange = sourceWorksheet.Range("H13:I" & lastRow)

    'create the chart (change the sheet reference accordingly)
    Dim shp As Shape
    With ActiveSheet
        Set shp = .Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=.Range("B2").Left, Top:=.Range("B2").Top, Width:=360, Height:=220)
    End With
   
    'set the properties
    With shp
        'set Shape object properties
        .LockAspectRatio = msoTrue
        '
        '
        With shp.Chart
            'set Chart object properties
            .SetSourceData sourceRange
            '
            '
            '
        End With
    End With

End Sub

Hope this helps!
Thanks so much for your help. Have a wonderful day.
 
Upvote 0
Best practice would be to use xlSheetHidden, xlSheetVisible or xlSheetVeryHidden, since Worksheet.Visible is not a Boolean property.
I didn't know that. It seemed to work, but there is a long delay. Maybe that's why it's running so slowly. I will make the changes. Thanks for your advice. Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top