简体   繁体   中英

Excel VBA - close an active table/document by name only - NOT filepath

i have a workbook where i regularly update stock prices from a yahoo link. it opens up a read only file named 'table'. below is an example link

http://real-chart.finance.yahoo.com/table.csv?s=INTC&a=05&b=9&c=2000&d=06&e=7&f=2016&g=d&ignore=.csv

things get messy when i have multiple 'table' file open that results in several additional clicks to import the data.

is there a way i can have a macro where any csv/xls files open named 'table' can be closed?

the function used to get this data is

=HYPERLINK("http://real-chart.finance.yahoo.com/table.csv?s=INTC&a=05&b=9&c=2000&d=06&e=7&f=2016&g=d&ignore=.csv","link")

and i've used similar close workbook scripts before, but i cannot for the life of me figure out how to reference this open file for it to be closed

This assumes that the files open in the same Excel instance as the one running the code, and also that you don't want to save any changes to the open files.

Sub CloseTableWorkbooks()

Dim wb as Workbook

For each wb in Application.Workbooks
    If Instr(1, wb.Name, "table") Then wb.Close False
Next

End Sub
Dim book As Workbook
    For Each book In Application.Workbooks
        if book.name = "table.xls" or book.name = "table.csv" then
            book.close
        end if
    Next

You'll get some messages like "Do you want to save before you close?" or the like, which Excel normally does. You can prevent them too but I'm not sure if you want that or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM