VBA copy WebTable to Excel - vba

I have a macro, that opens IE and navigates to a webpage. So now on the webpage, there are multiple tables. I'm trying to copy the last one to the page in Excel for further calculations but it copies all the values from the whole table in a single cell.How do I get the code to split the text into cells? The columns will be fixed for each page, but the number of rows might vary.
Worksheets("temp").Range("A1").Value = ie.document.getelementsbytagname("Table")(32).innertext

Here is what I have used in the past:
Because the table will more likely have rows and columns (td,tr) you can not assign the whole data to a single Range/Cell.
The method I ended using was to copy the whole table to the clipboard and Paste it on Cell/Range A1.
Set clipboard = New MSForms.DataObject
clipboard.SetText ie.document.getelementsbytagname("Table")(32).outerHTML
clipboard.PutInClipboard
Worksheets("temp").Cells(1, 1).PasteSpecial
Edit: You will need to add the Reference to Microsoft Forms 2.0 Object Library

Related

Excel table does not expand automatically when data added by vba includes a hyperlink

I have several excel workbooks that I want to consolidate in one excel table using the GetValue = ExecuteExcel4Macro(arg) method.
When data is added by the macro to the bottom of the table, the table should expand automatically and transfer formulas and formats.
This works pretty well up to the point where I want to add a hyperlink:
ActiveSheet.Hyperlinks.Add anchor:=ActiveWorkbook.Worksheets("Series").Cells(Irow, 3), _
Address:=fullpath, TextToDisplay:=GetValue(pfad, datei, blatt, "F3")
When including this line, the hyperlink is correctly entered, but the table will not expand. Without the hyperlink the table expands as supposed to.
Can anyone support here?
Thanks!
Use this code a line, before yours code :
ActiveWorkbook.Worksheets("Series").Cells(Irow - 1, 3).ListObject.ListRows.Add AlwaysInsert:=True

VBA Excel - copy, paste loop with offset

I am new to VBA and need some help - if anyone could help me.
I need to copy a range, say A1:F1, and paste it in the row directly under, so A2:F2, on an automatic loop.
The top-most row contains data from a different sheet, therefore I will be copying the formulas of that row which links to the secondary sheet. I would like the loop to end once the other sheet has been exhausted and the macro hits an empty row from the sheet.
Any ideas?
You can use a do until loop to find the length of your list. Then u can use this index to use range, push down the data and push your new data on the now empty top row.
I understand, that u want to push data always in the first row and everything else down, so you add always one new row.

Referencing a new inserted column Excel VBA

I am trying to reference a cell in the below formulaes. 'AUA Summary'!$D$9 . Each time the macro runs a new column D is inserted.
The Problem: When the column is inserted my reference moves to **
'AUA Summary'!$E$9. How do I get to reference 'AUA Summary'!$D$9 even if a new cell is inserted using VBA. My formuleas are below.
=IF(ROUND((SUM('BLL UTADS'!$D:$D)-'AUA Summary'!$D$9)+
(SUM('BLL UTADS'!$E:$E)-'AUA Summary'!$D$15),2)=0,"OK",
"Balances don't tie on BLL UTADS to AUA Summary Sheet")
=IF((SUM('BLL Prestige'!$D:$D)-'AUA Summary'!$D$10)+
(SUM('BLL Prestige'!$E:$E)-'AUA Summary'!$D$16)=0,"OK",
"Balances don't tie on BLL Prestige to AUA Summary Sheet")
=IF((ROUND('AUA Detail'!$D$9+'AUA Detail'!$D$23-'AUA
Summary'!$D$11,1)+ROUND('AUA Detail'!$D$15+'AUA Detail'!$D$29-'AUA
Summary'!$D$17,1))=0,"OK","Check the Totals tie")
The issue is on the 'AUA Summary' Tab the reference keeps changing. I have tried a VBA recorder , but i keep getting the same issue.
Each of these formuleas will be in a Cell.
You can use Indirect()
For example
'AUA Summary'!$D$9
can be written as
INDIRECT("'AUA Summary'!$D$9")
This way even when the columns move, it will refer to the same cell.
The other way is to use Index
For example D9 in Excel 2007+ can be written as INDEX(1:1048576,9,4) or INDEX(INDIRECT("1:" & ROWS(A:A)),9,4) for any version of Excel

VBA code on Hidden Sheet

I am building a small piece of VBA code to update the pivot table automatically so that my chart gets updated. After recording the code, I created stored it in the vb script of the sheet.
Here is my code:
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
I do not want to show the sheet containing the pivot table. So I hide the sheet, and then the code fails to work.
Try changing ActiveSheet to Worksheets("WorksheetName")
So you'd have
Worksheets("WorksheetName").PivotTables("PivotTable2").PivotCache.Refresh
Using ActiveSheet means it performs it on the sheet that is currently selected, last I checked you can't have a hidden sheet selected ;)

Convert xls File to csv, but extra rows added?

So, I am trying to convert some xls files to a csv, and everything works great, except for one part. The SaveAs function in the Excel interop seems to export all of the rows (including blank ones). I can see these rows when I look at the file using Notepad. (All of the rows I expect, 15 rows with two single quotes, then the rest are just blank). I then have a stored procedure that takes this csv and imports to the desired table (this works on spreadsheets that have been manually converted to csv (e.g. open, File--> Saves As, etc.)
Here is the line of code I am using for my SavesAs in my code. I have tried xlCSV, xlCSVWindows, and xlCSVDOS as my file format, but they all do the same thing.
wb.SaveAs(aFiles(i).Replace(".xls", "B.csv"), Excel.XlFileFormat.xlCSVMSDOS, , , , False) 'saves a copy of the spreadsheet as a csv
So, is there some additional step/setting I need to do to not get the extraneuos rows to show up in the csv?
Note that if I open this newly created csv, and then click Save As, and choose csv, my procedure likes it again.
When you create a CSV from a Workbook, the CSV is generated based upon your UsedRange. Since the UsedRange can be expanded simply by having formatting applied to a cell (without any contents) this is why you are getting blank rows. (You can also get blank columns due to this issue.)
When you open the generated CSV all of those no-content cells no longer contribute to the UsedRange due to having no content or formatting (since only values are saved in CSVs).
You can correct this issue by updating your used range before the save. Here's a brief sub I wrote in VBA that would do the trick. This code would make you lose all formatting, but I figured that wasn't important since you're saving to a CSV anyway. I'll leave the conversion to VB.Net up to you.
Sub CorrectUsedRange()
Dim values
Dim usedRangeAddress As String
Dim r As Range
'Get UsedRange Address prior to deleting Range
usedRangeAddress = ActiveSheet.UsedRange.Address
'Store values of cells to array.
values = ActiveSheet.UsedRange
'Delete all cells in the sheet
ActiveSheet.Cells.Delete
'Restore values to their initial locations
Range(usedRangeAddress) = values
End Sub
Tested your code with VBA and Excel2007 - works nice.
However, I could replicate it somewhat, by formatting an empty cell below my data-cells to bold. Then I would get empty single quotes in the csv. BUT this was also the case, when I used SaveAs.
So, my suggestion would be to clear all non-data cells, then to save your file. This way you can at least exclude this point of error.
I'm afraid that may not be enough. It seems there's an Excel bug that makes even deleting the non-data cells insufficient to prevent them from being written out as empty cells when saving as csv.
http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-bug-save-as-csv-saves-previously-deleted/2da9a8b4-50c2-49fd-a998-6b342694681e
Another way, without a script. Hit Ctrl+End . If that ends up in a row AFTER your real data, then select the rows from the first one until at least the row this ends up on, right click, and "Clear Contents".