Web Scrape Daily Treasury Rates - vba

I have tried to write some VBA that scrapes a table from the URL below. I would like to create a macro that pulls in the data on a daily basis. Any help is much appreciated. Thank you
https://home.treasury.gov/resource-center/data-chart-center/interest-rates/TextView?type=daily_treasury_yield_curve&field_tdr_date_value_month=202212
I am having problems understanding HTML and the elements function.

I know you want to scrape that table on the treasury page, but it just has daily values of rates for each maturity duration (2yr, 5yr, etc.). These same rates are published every day in their XML feed.
The XML feed is meant to be consumed and parsed by computer code, while the HTML on that page is NOT meant to be parsed by "screen-scrapers".
If your macro pulled in the data from their XML feed every day instead, and you kept a table/log/whatever of each day's rates, after two weeks, you'd have the exact same data as the treasury HTML table, but NOT have all the headaches of trying to parse HTML and always worrying about your code breaking if their HTML layout changes.
Would this later approach work for you?
Update: Eric Salazar's answer reminded me of something. I don't know if you're using Excel VBA, or some other Office app, but if you ARE using Excel, you can use the "'Data Menu'->Get Data->From Other Sources->From Web" feature to import that treasury table into an Excel table. Simply copy the URL of that page, paste it into the URL field of the first dialog that pops up (keep type as "Basic"), click OK, click "Table 0" under Display Options of the next ("Navigator") dialog, and lastly click "Load". By default it will create a table in the current worksheet at the currently selected cell. It will contain all the columns of what's on the treasury page and a few more columns that will be filled with "N/A" (not sure where Excel is getting the N/A field names from). You can get rid of those fields by going into the Power Query Designer before importing the data (instead of clicking "Load" in the previous "Navigator" dialog, click "Transform Data" instead. This will open up the "Power Query Editor" and you'll see a table editor in the main window. Select the column headers you don't want to end up in the table and hit the "Del" key (or right-click and choose "Remove Columns"). The selected columns will now be gone. Now click "Close and Load" in the upper-left corner of the Power Query Editor window.
And like magic, Excel will create a new table on your current worksheet with the data contained on that treasury page. You can then access the table with regular VBA code to refresh and read the values.

Related

VBA AddDataField CubeFields - Error 1004

I'm stumped...been building a PowerPivot sales report over the past few months and teaching myself VBA and DAX along the way. My report has several pre-configured views that can be chosen by VBA buttons, which work completely fine until you close the workbook and re-open it. Every time the workbook is opened for the first time - the VBA code errors out on the first AddDataField CubeFields line of code. It's as if excel does not pre-calculate the measure I am trying to add.
If I manually remove/add a value in the pivot table and then run the VBA code, it runs flawlessly. If I open PowerPivot manage data window and then close it, the VBA code runs perfectly fine. Something about manually changing values in the pivot table or simply opening the PowerPivot data tab, prompts Excel to pre-calculate all measures. Here is the exact line of code that gets hung up every time:
Worksheets("Dealer Install Sales").PivotTables("PivotTable1").AddDataField Worksheets("Dealer Install Sales").PivotTables( _
"PivotTable1").CubeFields("[Measures].[Sum of INVOICE_QTY]"), _
"PIECES"
How do I force excel to automatically calculate all measures upon opening the workbook?
I think what might be going on is the behaviour described in this joy-of-data blog post.
To paraphrase, a measure used in a pivot table which was created outside it, is "forgotten" in certain circumstances. A solution is to create a new measure within the pivot table field list (right click in field list and add measure), which simply references the original measure, e.g. =[measure name].
In your code, refer to the new name of measure, and when you do this, the data model should be stimulated into returning the value of the original measure, instead of falling over.

adding a new row to a table when adding another row in a different sheet

I have an Excel workbook in which I have several tables. On 3 of my sheets, there is some kind of "main" table which differ a bit from one another.
These tables are 2 lines by default (1 header and 1 data row) and extend when I add a new line below the one already existing. In some of the fields are formulas which are using values from the other tables. However, if for example, I insert a new value in a field below the final row, thus adding another row to the table, I want the tables in the other sheets to also add another row (and execute the formulas that are in the new row)
This is the sheet in which I have "Soorten Vezels" with their corresponding "max demping" and "soorten connectoren" with their corresponding "max demping":
This is the "maximale demping" file:
This is the measurement form, in this form, the user should not have to enter anything, except for the 2 dropdown menu's "type kabel" and "type connector":
What I would like is that when the user for example, inputs the length ("lengte") in the "maximale demping" file, a new row gets added (the fields "kabelnr", "van" and "naar" will be filled in automatically from the "gegevens" file)
But I would also like the workbook to add a new line in the measurement form, automatically fills in the "kabelnr", "van" and "naar" fields. So that I only have to choose the "type kabel" and "type connector" from the dropdown menu.
How do I do this (with VBA)?
Personally I like the MSDN when I am looking for answers to VBA and vb.net. I have found it to be thorough and comprehensive. The more I learn, the more I understand what they are teaching on this site.
This is a good tutorial to get started. Follow the links from this page.
Getting Started with VBA in Excel 2010
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx
Also remember to search Stack Overflow as you progress, there are an amazing number of questions covering specifics you may require when you start, or ask more questions.
If you require further assistance you can look at the links below.
This link is helpful. But I would navigate through the links below to get a broader view and to find an explanation that works for you.
Insert Row in Tables Multiple Sheets Excel 2010
http://chandoo.org/forums/topic/insert-row-in-tables-multiple-sheets-excel-2010
A forum- this link leads to a question about inserting rows.
MrExcel.com
http://www.mrexcel.com/forum/excel-questions/58685-insert-rows-between-different-data-2-a.html
If you persevere, down the page, there is good discussion, with code examples and trouble shooting (you will get a pop up when you go to this link asking to sign up for the newsletter, you can disregard this without a problem)
Insert Row in Tables Multiple Sheets Excel 2010
http://chandoo.org/forums/topic/insert-row-in-tables-multiple-sheets-excel-2010
A useful Q&A from Microsoft forum.
Insert blank rows in excel table with vba
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/insert-blank-rows-in-excel-table-with-vba/69e369a8-e656-4f68-adcd-c57e37253f12
Please let me know if this helps and ask new questions freely as you go.

Populate table in Word Template with VBA?

I'm filling in a Word template with data that's been collected from user input. In particular a (variable) number of documents is chosen, and information about each document fills a row of a table.
I've bookmarked several items in the template and successfully filled information in the header from my macro, but the table I'm not so sure with. I bookmarked the first cell and tried tabbing (with Chr(9)) through, and also tried passing an array. (In the template the table has only a first row. Usually tabbing past the last column creates an additional row.)
I can retieve cell contents with
Word.Application.ActiveDocument.Tables(1).Cell(3, 1).Range.Text
but can't write to the any cell except the first, where I placed a bookmark.
Can anyone offer a possible solution to populate the table?
To populate table, use this code
ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "Blah Blah"
This will write to the first cell in the first table. Use a loop to fill the rest of the cells.
I would also recommend see this link.
Topic: Automating Word Tables for Data Insertion and Extraction
Link: http://msdn.microsoft.com/en-us/library/aa537149%28v=office.11%29.aspx#Y1254
Extract:
Summary: Learn how to automate the creation and formatting of tables in Word. Get information about optimizing performance, populating a table with data, formatting table structure, linking table data, and extracting data from a table. (25 printed pages)
I've actually ran into a similar problem using Access to automate filling out a Word table template. I found that if I opened the template in Word prior to running the VBA script, then the Word document is successfully filled out with the table information. My code looks similar to yours as far as adding to it by row. Because the number of fields to be transferred to the form is dynamic it didn't seem like bookmarks for each section would work. If you have any update, I'd be happy to hear of a different way to resolve this.

Downloading and Formatting Web Data with Excel VBA

I need help creating a VBA macro that downloads closing price data directly from Yahoo Finance's Historic Quotes website and imports the data into an Excel spreadsheet column.
Background information:
This is the link to Yahoo Finance's Historical Quote database -
To download as a TXT file:
http://ichart.finance.yahoo.com/table.txt?s="StockTicker"&d="EndingMonth"&e="EndingDay"&f="EndingYear"&g=d&a="EndingMonth"&b="EndingDay"&c="EndingYear"&ignore=.txt
Formatting Issue:
By default, i.e. using Excel's Web Data Import Wizard, Excel imports the entire table which includes more columns than needed. I am trying to isolate the "close" column. I created a macro that formats the table to isolate the "close" column, but this macro requires me to manually download the data from Yahoo Finance as a txt file:
Sub Test_DownloadTextFile()
Dim text As String
'The variables for this URL (in light blue) should be retrieved from the spreadsheet, i.e. "StockTicker" will reference a cell with the ticker symbol "AAPL" in it and that will appear in the URL'
text = DownloadTextFile("http://ichart.finance.yahoo.com/table.txt?s="StockTicker"&d="EndingMonth"&e="EndingDay"&f="EndingYear"&g=d&a="EndingMonth"&b="EndingDay"&c="EndingYear"&ignore=.txt")
'At this point I should have the historical quotes table stored in the variable text. How do I select the 4th column and import it into a specific spreadsheet column?'
Debug.Print text
End Sub
How can I create a macro that:
1. Refers to the spreadsheet for key variables, e.g. "StockTicker", "EndingMonth", etc..
2. Downloads the corresponding historic data from Yahoo Finance
3. Imports the data closing price data as a single column into the spreadsheet
I would very much appreciate a practical solution to this problem. Let me know if I need clarify my question or the task at hand. Thank you!
Suggestion: this seems to be the perfect case for a Web Query.
Do you have any reason not to use that ? You can copy just the columns you need afterwards.
You did not specify you Excel version, but on 2003 it's on Data/Import External data.
Check out this SO discussion. Several suggestions that seem worthwhile.

How do I show data in the header of a SQL 2005 Reporting Services report?

Out of the box SSRS reports cannot have data exposed in the page header. Is there a way to get this data to show?
One of the things I want in my reports is to have nice headers for my reports. I like to have a logo and the user's report parameters along with other data to show to give more information for the business needs the report needs to clarify. One of the things that Microsoft SQL Server 2005 Reporting Services cannot do natively is show data from a Dataset in the header. This post will explain how to work around this and how easy it is.
Create the Report Server Project in the Business Intelligence Projects section and call it AdventureWorksLTReports. I use the AdventureWorksLT sample database from CodePlex.
alt text http://www.cloudsocket.com/images/image-thumb.png
Next show the Page Header by right clicking in the Report area with the designer.
alt text http://www.cloudsocket.com/images/image-thumb1.png
The Page Header will appear. If you want to show the Page Footer this can be accessed from the same menu as the Page Header.
alt text http://www.cloudsocket.com/images/image-thumb2.png
I created a stored procedure that returns data for the Sales Order to be presented in the Page Header. I will show the following information about the Sales Order in the Page Header:
Order Date
Sales Order Number
Company
Sales Person
Total Due
I create a TextBox for each of my data fields in the Page Header along with a TextBox for the corresponding label. Do not change the Expression in the TextBoxes that you want the Sales Order data in.
alt text http://www.cloudsocket.com/images/image-thumb3.png
In the Report Body, place a TextBox for each data field needed in the Page Header. In the Visibility for each TextBox, select True for Hidden. This will be the placeholder for the data needed in the Page Header.
alt text http://www.cloudsocket.com/images/image-thumb4.png
Your report should look similar to the screenshot shown below.
alt text http://www.cloudsocket.com/images/image-thumb5.png
The last step and most important is to reference the Hidden TextBox in the TextBoxes located in the Page Header. We use the the following Expression to reference the needed TextBoxes:
=ReportItems!.Value
Your report should now look similar to the following:
alt text http://www.cloudsocket.com/images/image-thumb6.png
Your Report preview should now have the Sales Order Header data in the Report Header.
alt text http://www.cloudsocket.com/images/image-thumb7.png
You have to do it through Parameters. Add a parameter for each piece of data you would like to display, then set the parameter to Hidden. Then set the default value to "From Query" and set the Dataset and Value field to the appropriate values.
I think the best option is creating a internal parameter, with the default value the field of the dataset you want to show.
Here are two possible workarounds:
You can place the databound field within the body of the report as a hidden textbox, and then in the header place another textbox with it's value pointed at the the one hidden within the body.
Try using report parameters to store the data, and use those parameters to access the data in the header.
This technique wouldn't work if your report spans over multiple pages, use queried parameters instead, and set the textbox value to =Parameters!Name.Value as per this article.
I'm with Orion Adrian here. Report parameters are the way to go.
I wanted to show a field, common to all returned rows, in the header, and for this scenario I went for the linked table solution (placing a table containing the field in the body and link a textbox in the header to this table).
I did that because if you are using the parameter solution and no data is returned to the field in question, the text "Parameter is missing a value" is shown instead of just a blank table. I reckoned this text would confuse users (as the parameter isn't even visible).