VBA code on Hidden Sheet - vba

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 ;)

Related

VB.net copy and paste excel cell to elsewhere in the workbook

So i am currently using Visual Studio to create a application that takes info out of an excel sheet and then does some calculations on the data and then pushes back to excel.
This bit i have managed to do but the bit i am struggling on is using a 'Parameters' sheet. I want to be able to enter a formula into a cell in one sheet of the workbook and then paste that formula into another sheet but to have it updating,e.g. as the cells go down the formula changes like it would in excel. I used a manual work around by hard coding the formula and then having variable as the row number, however i want to be able to just change the formula in the excel sheet and then when the code runs it applies to the rest.
Currently i have tried saving the cell value/text into a variable and then making the new cells equal that variable, however this then applies the same identical formula to the whole of the column(All required rows).
What i am currently trying to do is paste the variable into the top row and then copy and paste that cell down to the last one,
I have tried making the variable a formula but it evaluates the formula before it is equal to the variable and therefore just sets all the new cells to the formula answer, so i changed the cell to be text instead which then meant the formula did appear in the new cell however it was the identical formula for all cells.
The copy code works as below
bjExcel.cells(rown, colval) = param1
objExcel.cells(rown, colval).copy
This is working fine
But when i use the below the paste won't work
Do Until rown = 10
objExcel.cells(rown, colval).copy
rown = rown + 1
objExcel.cells(rown, colval).paste
Paste is not a recognized with the error:
System.MissingMemberException: 'Public member 'Paste' on type
'ApplicationClass' not found.'
Could be you need to use PasteSpecial instead?
https://learn.microsoft.com/en-us/office/vba/api/excel.range.pastespecial
Depends on what you're using to interop with excel.
shWorkSheet.Range("C7:C7").Copy()
shWorkSheet.Range("V7:V7").PasteSpecial(Excel.XlPasteType.xlPasteAll)

VBA copy WebTable to Excel

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

Point to the Excel sheet where the Macro actually stored

I have a sheet named "State List Generation" in which I have written a code which can extract City and State list from some text. I wanted to run this macro in another sheet which contains the text. I will be able to run this through macro window of excel if the first file is open. The problem is my state and city list is in the first sheet (State List Generation). When i run the macro from the second sheet which has data, the second sheet becomes the active sheet!!. I am not able to point the first excel where the the actual macro is present. Is there any way??
I have found the answer from https://support.microsoft.com/en-us/help/291308/how-to-select-cells-ranges-by-using-visual-basic-procedures-in-excel
I have just used Workbooks("FT_State_update.xlsm") to point the particular workbook. Thanks to Maddy Nikam

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

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.