Optimize code

TorrO

Board Regular
Joined
Feb 13, 2003
Messages
118
Office Version
  1. 2013
Platform
  1. Windows
Hi

I present my pulse in a graph like this:
1671025211308.png


To present graph per exercise I use a macro that deletes the graph, copies new data and past data into graph. ( I did not manage to solve it in other ways.)
The macro the goes back and forth to the raw data copy date, max puulse and info text.

I have navigation (next + previous)
1671025283419.png


I have recorded and configured the macro. The macro runs slow.... any way to optimize?

What this macro does is in simple terms
1 choose the graph line
2 delete graph
3 paste new values

Here is my code next dataset:

Rich (BB code):
Sub venstre()
'
'

'
    Application.ScreenUpdating = False
    Sheets("Presentasjon").Select
    ActiveSheet.Range("A1").Select
    Sheets("GrafData").Select
    ActiveCell.Offset(3, -1).Range("A1").Select ' flytter 2 ned 1 til venstre
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy 'pulse
    Sheets("Presentasjon").Select
    ActiveSheet.ChartObjects("Chart 3").Activate
    ActiveChart.FullSeriesCollection(1).Select
    Selection.Delete
    ActiveChart.Paste
    Sheets("GrafData").Select
    Selection.End(xlUp).Select
    Selection.Copy 'copy max
    Sheets("Presentasjon").Select
    ActiveSheet.Range("A1").Select
    ActiveCell.Offset(1, 19).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("GrafData").Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.Copy 'date
    ActiveCell.Offset(-1, 0).Range("A1").Select
    Sheets("Presentasjon").Select
    ActiveCell.Offset(0, -9).Range("A1").Select
    ActiveSheet.Paste
    Sheets("GrafData").Select
    ActiveCell.Offset(2, 0).Range("A1").Select
    Selection.Copy 'copy info
     Sheets("Presentasjon").Select
      ActiveCell.Offset(0, 3).Range("A1").Select
      ActiveSheet.Paste
   
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You have these lines.
VBA Code:
    Sheets("GrafData").Select
    ActiveCell.Offset(3, -1).Range("A1").Select ' flytter 2 ned 1 til venstre
But after selecting the sheet "GrafData" I don't know which cell the cursor is in, then you move it 3 cells down and one to the left.

Assuming that cell is "C1", I'll give you the code, just change "C1" to the cell where it starts. In this line of the macro:
VBA Code:
Set rng = sh2.Range("C1").Offset(3, -1)

Since I don't know what the starting cell is, I also don't know what cells you're copying in these lines:
VBA Code:
    Sheets("GrafData").Select
    Selection.End(xlUp).Select
    Selection.Copy 'copy max

Assuming, again, that the starting cell is C1, then you would be copying cells B1, B2, and B3. If they are other cells, update the cells in the macro:

VBA Code:
Sub venstre_v2()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim rng As Range
  Dim ChObj As ChartObject
  
  Set sh1 = Sheets("Presentasjon")
  Set sh2 = Sheets("GrafData")
  Set ChObj = sh1.ChartObjects("Chart 3")
  Set rng = sh2.Range("C1").Offset(3, -1)
  sh2.Range(rng, rng.End(xlDown)).Copy
  
  ChObj.Chart.FullSeriesCollection(1).Delete
  ChObj.Chart.Paste
    
  sh1.Range("T2").Value = sh2.Range("B1").Value
  sh1.Range("N2").Value = sh2.Range("B2").Value
  sh1.Range("K2").Value = sh2.Range("B3").Value
End Sub

If you have difficulty adapting the code, then share your file on google drive.
You could upload a copy of your file to a free site such google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,214,656
Messages
6,120,762
Members
448,991
Latest member
Hanakoro

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