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
Related
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)
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.
I have two sheets on my template. Sheet one contains a SKU in column A and a descr. in column B
On sheet 2, I have a pre-written template that has generic SKU and Description. That means under the item sku, it says D999 and under the description it says xxx.
I want to replace "999" with the value in A2 on my first sheet, but the 999 is in multiple columns through the sheet. Next, I want to replace the "xxx" with the data in B2 from sheet 1. I can't seem to find a VBA code that will do this specifically.
Lastly, once the find and replaces are done, I need to have it save me a new workbook using sheet2, in the same format, and name it as the value in A4 on sheet 2. I think you should be able to find the workbook here My Workbook
Basically, I want to be able to enter a bulk amount of new SKUs and Description, and be able to generate a new workbook for each SKU I enter. Right now I have 78 waiting to have sheets made.
In the cell to the right of the cells where you want to type in the SKU, add the following formula: =Match(A2, LookupsheetName!B:B,0). This will return the row the SKU was found on. In the next cell to the right use the Index function: =Index(LookupSheetName!A1:B1000, B2, 0). This will bring the label in column B to the current page. The Match function locates the row the SKU was found on.
Try these for looking up the information, because they are more efficient than VLookups.
The code for copying a worksheet to another workbook can be created using the Macro recorder on the Developer tab. Turn the recorder on then do the steps to manually move/copy to another workbook. When done, open the VBA editor and figure out how to save the workbook to a new name each time.
Good luck.
I am new to Excel VBA and need some help writing a macro.
From Worksheet1 of Workbook1 I need to copy each column of range D1:Z100, one at a time, and pasteValue it to cells B1:B100.
This triggers a calculation in Worksheet2 of Workbook1. Here I need to copy cells A1:B200 into a new workbook.
This new workbook is to be renamed with the text string in Worksheet1, cell B1. The new workbook is to be saved into the same folder as Workbook1.
The loop is then to continue with the next column from Worksheet1, and continue until all columns in the range have been treated this way.
I have used two days searching the net to find an answer without any luck....
Start the way most of us did:
Record new macro.
Manually perform all the steps you want to automate.
Stop recording.
Go to the Developer tab and inspect the auto-generated code.
Write your own VBA based on what you learn in Step 4.
You will have to figure out a few things on your own, or come back here and ask specific questions.
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