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.
Related
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
I had a hard time formulating the title, but I'll explain better here.
What I want to do is use a VBA-macro that I'm activating from a button click to copy the data found in the range B13:E52 on sheet1 to the same range on sheet"B3", where B3 is found on sheet1 containing the sheet name I want to copy to. This dynamically updates depending on what item is chosen in a list.
I know how to create my button, copy between sheet etc but I'm having a hard time figuring out how to reference the target sheet name that's contained in B3 for the target in VBA.
with sheet1
.range("b13:e52").copy sheets(.range("b3").value).range("b13")
end with
If you are looking to copy only the vlaues (without the formats or formulas), then you can use :
With Worksheets("Sheet1")
Worksheets(.Range("B3").Value).Range("B13:E52").Value = .Range("B13:E52").Value
End With
I am trying to move data from one workbook 'input worksheet' to another workbook 'master workbook'. Both sheets are in the same file and if possible, it would be great if both files didn't have to be open at the same time in order to transfer the data but the master workbook would autosave once the data was transferred across. Links to images of the files below to make it easier to understand what I am trying to do.
The data in the input worksheet is in row 6, columns A-J with each user inputting details of the tasks they get asked to complete. I would like when a button is clicked, the data from the input worksheet is transferred into row 2, columns B-K in the master workbook so that each time a new task is entered and transferred across, it appears in the row below (so that it can be pivoted later, etc.).
http://i.stack.imgur.com/b2cyI.jpg - input sheet
http://i.stack.imgur.com/JZr0a.jpg - master sheet
Use the macros here to get the last row in the master sheet.
Then simply write the values from the input sheet to the corresponding cell in the master sheet.
That is all. This is how you refer cells:
tbl_master.cells(1,3) = tbl_input.cells(3,5).value
Make sure that the row in the tbl_input is a variable, coming from the function, calculating the last row. Give it a try!
Edit:
This is what I use for last row:
Public Function last_row_with_data(ByVal lng_column_number As Long, shCurrent As Variant) As Long
last_row_with_data = shCurrent.Cells(Rows.Count, lng_column_number).End(xlUp).Row
End Function
If you want to find the last row of column B of sheet "tbl_main" you call it like this:
last_row_with_data(2,tbl_main)
Edit2:
Change the names of your sheets here, and reference them by their names.
In order to get this window, select the sheet on the left and press F4.
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 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