+ Reply to Thread
Results 1 to 6 of 6

Can Anyone Help Speed Up My Macro?

Hybrid View

smcmahon83 Can Anyone Help Speed Up My... 12-02-2015, 04:02 PM
gmr4evr1 Re: Can Anyone Help Speed Up... 12-02-2015, 04:25 PM
smcmahon83 Re: Can Anyone Help Speed Up... 12-02-2015, 04:40 PM
jaslake Re: Can Anyone Help Speed Up... 12-02-2015, 04:46 PM
gmr4evr1 Re: Can Anyone Help Speed Up... 12-02-2015, 05:01 PM
smcmahon83 Re: Can Anyone Help Speed Up... 12-02-2015, 05:20 PM
  1. #1
    Registered User
    Join Date
    03-05-2014
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    22

    Can Anyone Help Speed Up My Macro?

    Hi everyone,

    I have the following macro and heard that if you bypass using the clipboard, it can speed things up. Can someone please take a look at my macro below and change anything that you think will make it more efficient? Many thanks to anyone who can help.

    Sub Formatter()
    '
    ' Formatter Macro
    '
        Do Until IsEmpty(ActiveCell)
        Range("L3:AX3").Select
        Selection.Copy
        Range("L4").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("B15").Select
        Selection.Delete Shift:=xlUp
        Range("B9").Select
        ActiveCell.FormulaR1C1 = "=R[6]C"
        Range("B15").Select
    
    Call GetData
    Call DeleteNamedRanges
    Loop
    End Sub
    Last edited by smcmahon83; 12-02-2015 at 04:14 PM.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Can Anyone Help Speed Up My Macro?

    This doesn't address the clipboard part, but, try adding this after Sub Formatter()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    And this before End Sub
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
    This may help speed things up a bit.
    Last edited by gmr4evr1; 12-02-2015 at 04:26 PM. Reason: Corrected typos
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    03-05-2014
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Can Anyone Help Speed Up My Macro?

    Thanks, Gmr. That did speed things up a bit.

    If anyone has any other modifications that can increase speed, please let me know.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Can Anyone Help Speed Up My Macro?

    Hi smcmahon83

    You're doing unnecessary Selecting. One rarely needs to Select in Code.

    Please post the rest of your Code, particularly these and I'll look at it.
    Call GetData
    Call DeleteNamedRanges
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Can Anyone Help Speed Up My Macro?

    smcmahon,
    Glad it helped a bit.
    As for what John said.....I knew that using Select wasn't good, I just didn't know how to correct it for OP so I decided to not say anything about, I had hoped that someone else would. Thanks John

  6. #6
    Registered User
    Join Date
    03-05-2014
    Location
    NYC
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Can Anyone Help Speed Up My Macro?

    Thank you both John and Gmr for you willingness to help.

    John, the first macro being called upon is below. In short, it pulls data from a website and pastes it into a nested table within Excel. There is a lot of code to work with time format conversions.

    Sub GetData()
        Dim ParameterSheet As Worksheet
        Dim DataSheet As Worksheet
        Dim ticker As String
        Dim exchange As String
        Dim interval As Integer
        Dim numPastTradingDays As Integer
        Dim qurl As String
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
    
        Set ParameterSheet = Sheets("Parameters")
        Set DataSheet = Sheets("Data")
    
        DataSheet.Cells.Clear
        ticker = ParameterSheet.Range("ticker").Value
        exchange = ParameterSheet.Range("exchange").Value
        interval = ParameterSheet.Range("interval").Value
        numPastTradingDays = ParameterSheet.Range("numTradingDays").Value
    
        qurl = "http://www.google.com/finance/getprices?" & _
               "q=" & ticker & _
               "&i=" & interval & _
               "&p=" & numPastTradingDays & "d" & _
               "&f=d,o,h,l,c,v"
    
    QueryQuote:
        With DataSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=DataSheet.Range("a1"))
            .BackgroundQuery = True
            .TablesOnlyFromHTML = False
            .Refresh BackgroundQuery:=False
            .SaveData = True
        End With
    
        DataSheet.Range("a1").CurrentRegion.TextToColumns Destination:=DataSheet.Range("a1"), DataType:=xlDelimited, _
                                                          TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                                                          Semicolon:=False, Comma:=True, Space:=False, other:=False
    
        DataSheet.Columns("A:G").ColumnWidth = 12
    
        '===Convert Google timestamp to Excel timestamp (only for Windows)
        Dim timeStamp As Double
        Dim timeStampRaw As String
        Dim timeZoneOffsetRaw As String
        Dim timeZoneOffset As Variant
        Dim numRows As Integer
        Dim i As Integer
        numRows = DataSheet.UsedRange.Rows.Count - 1
    
        timeZoneOffsetRaw = DataSheet.Range("a7")
        timeZoneOffset = (Mid(timeZoneOffsetRaw, InStr(timeZoneOffsetRaw, "=") + 1, 10))
    
        For i = 8 To numRows
    
            If Not IsNumeric(DataSheet.Range("a" & i)) Then
    
                timeStampRaw = DataSheet.Range("a" & i)
                timeStamp = (Mid(timeStampRaw, 2, Len(timeStampRaw) - 1))
                timeStamp = (timeStamp + timeZoneOffset * 60)
                DataSheet.Range("g" & i) = timeStamp / 86400 + 25569
    
            Else
    
                DataSheet.Range("g" & i).FormulaR1C1 = "=(RC[-6]*" & interval & "+" & timeStamp & ")/86400+25569"
    
            End If
    
        Next
    
        DataSheet.Range("g8:g" & numRows).NumberFormat = "d mmm yyyy h:mm;@"
        DataSheet.Range("G:G").Columns.AutoFit
    
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    The second macro being called upon is below. All this macro does is delete these pesky "named ranges" that get created every time the code above runs. So if you run the getdata macro 500 times, 500 named ranges will being created, without this deleter macro.

    Public Sub DeleteNamedRanges()
      Dim NamedRange As Name
      
      For Each NamedRange In ThisWorkbook.Names
        If InStr(NamedRange.Name, "External") > 0 Then NamedRange.Delete
      Next
    End Sub
    The formatter macro that I included in my original post, essentially runs the web query using the 'getdata' macro, takes data as it appears in row 1, pastes it to row 2, and inserts a row above the row it just pasted (to keep pushing the pasted data down as the loop runs). Once this is complete, the formatter macro deletes the value in cell C15 (keyword used for web query) and shifts up so the keyword in C16 will appear in C15, (as this is the active cell constantly being used to fetch data by the web query). I hope this makes some sense, and if you can help at all I would be much obliged. Thanks.

+ 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. Everage Speed km/time (european speed)
    By GerryZucca in forum Excel General
    Replies: 3
    Last Post: 02-23-2015, 03:02 PM
  2. How to speed up this macro?
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2014, 08:18 PM
  3. vba to speed up macro
    By dulitul in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 09-25-2013, 09:17 AM
  4. [SOLVED] Speed Up Macro / VBA
    By zhb12810 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2012, 02:37 PM
  5. [SOLVED] Speed up a macro
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 02-17-2010, 05:05 PM
  6. Speed Up Macro
    By Pindacko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2007, 12:06 PM
  7. Macro Speed
    By BN-CD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2007, 01:41 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