Only change values on active worksheet - vba

I have a workbook with 4 sheets. The 4 sheets are almost identical Some of the cells values are calculated by calling a macro in the cell. For the active worksheet this Works fint. The macro gets all the values from the correct sheet and the calculation is correct.
But, when the calculation is done in the active worksheet the cells in all the worksheet that calls the macro is changed with the value calculated in the active worksheet.
How do I make sure that only the cells in the active workseet that calls the macro is changed?
[EDIT]
Example:
I have 2 sheets.
In both sheets I have a cell that calls a function: .Value = "=FunctionName()"
When I do calculations in sheet 1, the cell with the call is showing correct result. But the cell in sheet 2 is showing the same result.
Only the cell in sheet 1 should show the result from the function calculated from sheet 1.

Say if you want to change in sheet1. Then
Worksheets("Sheet1").Activate
With ActiveWorksheet
Call calculationsmacro 'your macro to calculate the cells
End With
I had kind of similar problem and it was solved by this method.
If it works for you then it would be really grateful if you can mark my answer.

Related

Adding a new cell reference to an existing formula

New to these forums (posting anyway - been using them as a reference for a while!).
I have a formula that calculates average unit size by adding up cells from other sheets in the same book.
I have a macro that creates a new template sheet, and am now working on the macro to update the summary tab with the data from the newly created sheet.
For Example my formula reads:
=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7)/F8
F8 is the total number of units, each X7 is the total unit size.
What I can't work out is if a macro can edit the above formula so that when I have added sheet 5, I run the macro and it edits the formuala to read:
=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7,Sheet5!X7)/F8
You can change the formula to always include any new sheets.
You will just have to add the new sheet between the first and last sheet you are summing.
=SUM('Sheet2:SheetLast'!X7)/F8
You will not need to edit this formula, so long as any new sheets are between Sheet2 and SheetLast in your file.
As mentioned by Mooseman, you will not need to edit this formula, as long as any new sheets are between Sheet2 and SheetLast in your file.
But to address your specific question regarding the ability of a macro to modify the formula: yes, a macro can edit that formula. Assuming your formula is in a specific cell such as "A2", you could have a macro like this:
Sub MyMacro()
Range("A2").Select
ActiveCell.FormulaR1C1 ="=SUM(Sheet2!X7,Sheet3!X7,Sheet4!X7,Sheet5!X7)/F8"
End Sub

VBA EXcel - Subtracting active sheet cell from prior day sheet cell

I have a spreadsheet that I track the daily inventory by account and make sure I balance to the G/L. I have a VBA set up that creates my new sheet and names it as per current date. At the bottom of the sheet I have formulas that give me the change in inventory value from today minus yesterday. Since that formula isn't something I need to "update" daily I am looking to write the formula in the VBA.
I am getting myself caught up on how to write the VBA for the change in sheet name when the new sheet is created.
For example, I have a sheet named "05.10.16" that has a formula that takes B8 - '05.09.16'!B8 and shows the result. So I open the spreadsheet today and create today's sheet, "05.11.16".
What is the coding I need in VBA to update that formula ActiveCell.Formula = "B8 - ws2.cell(B8)"?
I have set ws1 = ActiveSheet, but having trouble defining ws2. ws2 will be a moving target once the new sheet is added for the day.
If you add a sheet with VBA you get a reference to that worksheet in return. Just save it to a variable and you can access it later on:
Set ws2 = ThisWorkbook.Sheets.Add("05.11.16")
You can use the workbook's Address property (possibly with the fourth parameter set to True) to retrieve the address that you have to change your formula to.

VBA Excel: How to assign range of cells to another range of cells?

New Excel VBA user here. I feel like I made a silly syntax mistake.
I am trying to copy some vertical cells in one worksheet, to horizontal cells in another worksheet.
Worksheets("Master").Range("D14:F14").Value = Worksheets("Temp").Range("B12:B14").Value
However, the above command seems to only paste the value of B12 to cells D14:F14. Is there a way to paste the cells of B12, B13, and B14 to D14:F14?
Use Application.Transpose():
Worksheets("Master").Range("D14:F14").Value = Application.Transpose(Worksheets("Temp").Range("B12:B14").Value)
Or PasteSpecial:
Worksheets("Temp").Range("B12:B14").Copy
Worksheets("Master").Range("D14").PasteSpecial xlPasteAll, , , True
If you only want values then the first is best. When transposing with PasteSpecial one must use the xlPasteAll. It will throw an error if one tries to paste values only with transpose as true.

Replace reference in formula with precedent

I have an excel file with three sheets (1,2,3), in the first sheet are primary data, in the third sheet are final data for output and the second sheet is bridge between them. I need to update formulas in the third sheet so I can delete the second sheet.
For example, there is value in sheet "1" in cell A1, then in sheet "2" is C5='1'!A1, in sheet "3" B4='2'!C5, and I need to get in sheet "3" B4='1'!A1. I need to do this for thousands of cells, therefore it is impossible to do it manually and I was thinking about using macro, but I was unable to create such macro. Can somebody help me?
This question raised because the bridge in sheet "2" is complex and not only assign one value from sheet "1" to one cell but it also add up several cells from sheet "1" together, therefore there is not any simple way to replace the bridge by vlookup or similar function.
It's pretty difficult to answer this question the way you have written it. This is because it seems all the cells in Sheet3 point to a "random" spot in sheet 2. If there is no rhyme or reason to how sheet pulls its numbers, replacing the formulas could be hard. However, try this:
If sheet 3 cells only EVER refers to a single cell in sheet 2, and sheet to only EVER refers to sheet 1, then you can go to sheet 2, and press CTRL + H. This will bring up the "find and replace" function. Replace all instances of "=" with "ZZZZ=". When you look to sheet3, you will see that instead of giving the values calculated off of sheet1 through sheet 2, a cell in sheet3 could say something like "ZZZZ='sheet1'!A1+'sheet1'!B1", because that's the value of sheet 2 that it points at. From there, copy and paste all of sheet3 to values only. Then, CTRL + H again and replace all instances in sheet of "ZZZZ=" with "=".
However I doubt this will work as if sheet2 contains values used elsewhere in sheet2 to calculate, or if sheet3 refers to things in other ways, this will fall apart.

How do I increment cell in Excel VBA script?

I have a data in excel which I want to make a VBA script to copy it into a new worksheet but in a different way.
For example, I have this in sheet1 in A1~A3 cells.
Adam(A1)
Sam(A2)
Smith(A3)
I want to use these cells and create the following in another worksheet using refedit control.
Adam(A1)
Adam(A2)
Adam(A3)
Adam(A4)
Sam(A5)
Sam(A6)
Sam(A7)
Sam(A8)
Smith(A9)
Smith(A10)
Smith(A11)
Smith(A12)
I have refedit control in place in VBA script, but I'm not sure how to increment cell numbers to make it copy and paste into a new worksheet. I would like to use refedit control so that I can assign any cells and make it copy and repeat itself. How do I do this in VBA script?
Check out the Range Rows, Cells, and Address properties. This should help. Your question is too vague for a direct answer.
(This will get you started.)
Range.Row Property
http://msdn.microsoft.com/en-us/library/bb221550(office.12).aspx
Returns the number of the first row of the first area in the range. Read-only Long.
Example
For Each rw In Worksheets("Sheet1").Rows
If rw.Row Mod 2 = 0 Then
rw.RowHeight = 4
End If
Next rw
To increment cells in Excel VBA, you can use the Offset-property of the Range-object, e.g.
ActiveCell.Offset(1, 1).Select
will select the cell one row down and one column to the right of the active cell.
To add to Geoffrey's answer about active cell - it would also require that you activate the sheet you are looking to input your values if it is a different sheet from the one that is currently active. Additionally you would have to activate a cell to use activecell and the activecell offset property.
For example
'Activates the name of the sheet you would like to activate
Sheets("Sheet2").Activate
'Activates cell A1
Range("A1").Activate
'Activates cell one row down, one column right
ActiveCell.Offset(1,1).Select
'if current sheet is not activate you just do Sheets("Sheet2").Range("A1").Activate
The offset property of ActiveCell refers to other cells based off of the current active cell.
For example-
Offset(row,column) -
First Argument -Positive values as the first argument refer you to rows below the current active cell and Negative values refer you to rows above the current active cell
Second Argument-Positive values as the second argument refer you to columns right of the current active cell and Negative values refer you to columns left the current active cell