I want to keep my existing formula after I copy a different value to the related cell.
I am beginner of Excel VBA and there is a problem that I need to solve. I have value of previous period cumulative total at cell A1. Cell B1 is the value of this month. Lastly C1, it sums the previous period and this month and gives the cumulative total value.
So I've already written a macro for this but next month I need to copy the value at C1 and paste it to A1 so it will automatically sum a1+b1 again and write to c1. However when I apply the macro for copying the value it destroys the sum formula.
Sub sumfunc()
Range("C1").Formula = "=A1+B1"
Range("C1:C3").FillDown
End Sub
Sub copyfunc()
Worksheets("Sheet1").Range("C1:C3").Copy _
Destination:=Worksheets("Sheet1").Range("A1")
End Sub
If you copy that formula without absolute cell references, you end up with #REF! error because you are trying to reference a column that is left of column A. If you use absolute references then you get a circular reference because the formula contains a reference to the cell it is in.
You need to copy the value returned from the formula and add it to the target, not the formula itself. In this way, the value from the formula in column C will reflect a growing sum.
Sub sumfunc()
Worksheets("Sheet1").Range("C1:C3").Formula = "=A1+B1"
End Sub
Sub copyfunc()
with Worksheets("Sheet1")
.Range("C1:C3").Copy
.Range("A1").pastespecial paste:=xlpastevalues, operation:=xladd
end with
End Sub
Related
How to Copy/Paste a value from 1 cell to 2nd Cell using a 3rd Cell which holds the address of the 2nd Cell. I want to trigger the copy/past action by a 4th Cell going 'true'. I do not want to use VB as I am not competent with it.
You will need to use VBA I think. The only way to get a cell to show a value other than by programming is to enter a formula into your 2nd cell - but by definition you don't know what that is. Assume that A1 holds the value to copy, A3 is the cell holding the cell address and A4 is the true/false cell. Then you need this code in the module page of the sheet you wish to affect:
Private Sub Worksheet_Calculate()
If range("a4") then
range(range("a3").text).value = range("a1").value
End If
End Sub
Note this will error if A3 doesn't hold a valid cell address
This might be too easy for many but i need an help.
I have following formula in Sheet1 which takes the value from Sheet2 M3 Cell
='Sheet2'!M3
M3 cell in Sheet2 has a formula inside which is following:
=INT(NETWORKDAYS(K3;L3)/5)+1
I want to paste the formula inside M3 to my formula in Sheet1 but when i do this as following i get an error.
='Sheet2'!INT(NETWORKDAYS(K3;L3)/5)+1
Can anyone tell me what is wrong here and how i can paste this formula correctly so it works?
Your question was a bit confuse, but:
Do you want the NETWORKDAYS formula to get data from Sheet1 itself?
If yes, just erase the 'Sheet2'! part of your formula.
2. Do you want the NETWORKDAYS formula to get data from Sheet2?
If yes, this is the final formula:
=INT(NETWORKDAYS('Sheet2'!K3;'Sheet2'!L3)/5)+1
The problem is that you should use Sheet reference ('Sheet2'!) before cells, not before formula, so that Excel knows where it should get the data.
say we have:
Sheet1 cell A1 contains the formula:='Sheet2'!M3Sheet2 cell M3 contains the formula:=INT(NETWORKDAYS(K3;L3)/5)+1Select the A1 cell and run this short macro:
Sub FormulaGrabber()
Dim s As String, r As Range
With ActiveCell
s = .Formula
Set r = Range(s)
s = r.Formula
.Formula = s
End With
End Sub
The macro will go to Sheet2, grab the formula in M3 and put it back in Sheet1
Sub Copy8()
'H0544
Worksheets("PCR AND HPC").Sum(.Range("D3:D5")).Copy
Worksheets("CARE").Range("c25").PasteSpecial Paste:=xlValues
End Sub
I am trying to sum 3 cells on one sheet and then paste that value in my other sheet
Sum is part of WorksheetFunctions:
Application.WorksheetFunction.Sum(Worksheets("PCR AND HPC").Range("D3:D5"))
You can't copy a memory value, just assign it to the output cell:
Worksheets("CARE").Range("c25").Value = Application.WorksheetFunction.Sum(Worksheets("PCR AND HPC").Range("D3:D5"))
I have a formula in cell c15 that I need to copy down the entire column. However, I want to copy the formula down one cell at a time, calculate the value and then paste special values over that cell.
I want to repeat the code below to calculate one cell at a time. I can't repeat until the last cell is empty because they are all empty except cell c15 so I'm not sure how to stop the loop.
Sub Macro5()
Range("c16:c3000").Formula = Range("c15").Formula
Range("c16:c250").Calculate
Range("c16:c250").Value = Range("c16:c250").Value
End Sub`
I have a column of numeric data, where I need to sum the values from a specified cell, down to the last value before the first blank cell in the Column. I have used a Range function previously to complete this, but on this occasion the number of rows before the blank cell is unknown and cannot be defined in a range. the simple explanation is I need the result in cell A1 to be
=sum (A6:AX)
where X is one before the first blank cell.
I will then write this into my VBA loop to complete it for nth columns over nth sheets.
Use the following sub:
Sub SumTillBlankCell()
Dim BeforeFirstBlankCell
BeforeFirstBlankCell = Range("A6").End(xlDown).Row
Range("A1").Value = Application.Sum(Range("A6:A" & BeforeFirstBlankCell))
End Sub