Macro stops working when I copy sheet

excelgeekgirl

New Member
Joined
Sep 22, 2009
Messages
14
Hi,

I put together some code to calculate the stock volatility. It requires that you enter a start date, end date, and ticker symbol, and then you push the button and excel retrieves the data from yahoo finance & runs some formulas. My problem is that I'd like to have multiple tabs for different stocks, each with it's own button, but when I create a copy of the sheet, the macro doesn't work. I'm sure it has something to do with how I'm referencing the sheet, but I don't know enough to figure it out. Also, my ideal would be that I could create a copy of a worksheet with the code in it, and the macro button on that sheet would update automatically for the new sheet reference.

Thank you in advance for all the help!

Code:
Sub GetData()
    Dim DataSheet As Worksheet
    Dim EndDate As Date
    Dim StartDate As Date
    Dim Symbol As String
    Dim qurl As String
    Dim nQuery As Name
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    
    Set DataSheet = ActiveSheet
  
        StartDate = DataSheet.Range("B1").Value
        EndDate = DataSheet.Range("B2").Value
        Symbol = DataSheet.Range("B3").Value
        Range("C7").CurrentRegion.ClearContents
        
        qurl = "[URL]http://ichart.yahoo.com/table.csv?s[/URL]=" & Symbol
        qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
            "&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
            Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Range("E3") & "&q=q&y=0&z=" & _
            Symbol & "&x=.csv"
        
                   
QueryQuote:
             With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=DataSheet.Range("C7"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = False
                .Refresh BackgroundQuery:=False
                .SaveData = True
            End With
            
            Range("C7").CurrentRegion.TextToColumns Destination:=Range("C7"), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                Semicolon:=False, Comma:=True, Space:=False, other:=False
            
            Range(Range("C8"), Range("C8").End(xlDown)).NumberFormat = "mm/dd/yy"
            Range(Range("D8"), Range("G8").End(xlDown)).NumberFormat = "0.00"
            Range(Range("H8"), Range("H8").End(xlDown)).NumberFormat = "0,000"
            Range(Range("I8"), Range("I8").End(xlDown)).NumberFormat = "0.00"
            Range(Range("J8"), Range("J8").End(xlDown)).NumberFormat = "0.0000"
            Range(Range("K8"), Range("K8").End(xlDown)).NumberFormat = "0.0000"
 
  
  Dim Column_J_J As Long
    Column_J_J = Range("I" & Rows.Count).End(xlUp).Row - 1
    Range("J8").Formula = "=RC[-1]/R[1]C[-1]-1"
    Range("J8").AutoFill Destination:=Range("J8:J" & Column_J_J), Type:=xlFillSeries
  
    Column_K_K = Range("I" & Rows.Count).End(xlUp).Row - 1
    Range("K8").Formula = "=IF(R[1]C[-2]<0.0000001,no data,LN(RC[-2]/R[1]C[-2]))"
    Range("K8").AutoFill Destination:=Range("K8:K" & Column_K_K), Type:=xlFillSeries
      

    Range("J7").Select
    ActiveCell.FormulaR1C1 = "Return"
    Range("J7").Select
    Range("K7").Select
    ActiveCell.FormulaR1C1 = "Natural Log"
    Range("K7").Select
Application.Calculation = xlCalculationAutomatic
Columns("C:K").EntireColumn.AutoFit

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,

This worked for me when I copied it to another sheet provided I used an ActiveX button.
It failed when using a Form button in the worksheet.

Check to see which you are using and change it to an ActiveX button if needed.

The sheet reference shouldn't be the problem since the only sheet references are to ActiveSheet.
 
Upvote 0
Thanks for the response. I've never created an ActiveX button, but I'm thankful for the introduction.

Thank you!

You insert it into your worksheet from the Developer > Toolbox the same way as you did for your Form button.

When you insert a Form button, it pops up and asks what macro you want to associate. With the ActiveX button, you click on the button when you are in Design mode and it sends you to the CommandButton1_Click code.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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