+ Reply to Thread
Results 1 to 2 of 2

Update Data in Closed Workbook

  1. #1
    Registered User
    Join Date
    11-18-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    1

    Update Data in Closed Workbook

    VBA novice here,

    Is possible to update data that is queried from an external source in a closed workbook?

    I use the following code to retrieve stock data from Yahoo Finance:

    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
    Dim LastRow As Integer

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationAutomatic

    Sheets("Data").Cells.Clear

    Set DataSheet = ActiveSheet

    StartDate = DataSheet.Range("startDate").Value
    EndDate = DataSheet.Range("endDate").Value
    Symbol = DataSheet.Range("ticker").Value
    Sheets("Data").Range("a1").CurrentRegion.ClearContents

    qurl = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol
    qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
    "&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
    Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Sheets("Data").Range("a1") & "&q=q&y=0&z=" & _
    Symbol & "&x=.csv"

    QueryQuote:
    With Sheets("Data").QueryTables.Add(Connection:="URL;" & qurl, Destination:=Sheets("Data").Range("a1"))
    .BackgroundQuery = True
    .TablesOnlyFromHTML = False
    .Refresh BackgroundQuery:=False
    .SaveData = True
    End With

    Sheets("Data").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("Data").Range("a1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, other:=False

    Sheets("Data").Columns("A:Z").ColumnWidth = 12


    LastRow = Sheets("Data").UsedRange.Row - 2 + Sheets("Data").UsedRange.Rows.Count
    Sheets("Data").Sort.SortFields.Add Key:=Range("A2"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Sheets("Data").Sort
    .SetRange Range("A1:G" & LastRow)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    .SortFields.Clear
    End With
    End Sub

    I would like to run this macro from another workbook while this workbook is closed. This is so I can then reference the values from this closed workbook in the open workbook I am working with.

    I hope that all makes sense.

    Thanks for taking a look!
    -Steve

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,447

    Re: Update Data in Closed Workbook

    Please wrap your code between tags as per forum rules - Thx

+ 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. Update closed workbook
    By BuZZarD73 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2015, 01:09 PM
  2. Update Cell Value from a Closed Workbook to Another Closed Workbook
    By glennchung in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2014, 04:44 PM
  3. Replies: 0
    Last Post: 03-27-2014, 12:38 PM
  4. How to read update from a closed workbook
    By Gooford in forum Excel General
    Replies: 3
    Last Post: 12-11-2012, 10:27 AM
  5. Update a closed workbook daily using VBA
    By timmycl_7 in forum Excel General
    Replies: 6
    Last Post: 09-23-2011, 06:49 AM
  6. Cant update data to closed workbook
    By kwik98 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2010, 10:57 PM
  7. Update cells whose source is in a closed workbook
    By OilMan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-10-2010, 07:39 PM

Tags for this Thread

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