The formula on Column C is
=VLOOKUP($B2,Sheet1!$A$2:$C$100,2,0)
which gets the value from Sheet1 and it works okay.
Now I want to put a condition
If cell D2:D100 is empty then make C2:C100 empty
C2:C100 contains a vlookup formula, and I'd like to clean the formula from the cell.
Secondly, how do we run this in a macro =VLOOKUP($B2,Sheet1!$A$2:$C$100,2,0)?
manimatters has a good solution to use a ISBLANK with an IF formula. If you still want a macro for your second question, I've provided it below.
This macro will set the formula in C2 to =VLOOKUP($B2,Sheet1!$A$2:$C$101,2,FALSE) and then fill this formula down to C100.
Sub RestoreVLookup()
Range("C2").FormulaR1C1 = "=VLOOKUP(R[0]C2,Sheet1!R1C1:R101C3,2,FALSE)"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C100"), Type:=xlFillDefault
End Sub
Let's look at the different parts:
Range("C2").FormulaR1C1 - Assign a formula to cell C2.
"=VLOOKUP(R[0]C2,Sheet1!R1C1:R101C3,2,FALSE)" This is the formula assigned
R[0] - Relative row with an offset of zero. R[1] would point to the row above and R[-1] would refer to the row below.
C2 - The 2nd column (aka column B)
Range("C2").Select - Select cell C2
Selection.AutoFill ... Type:=xlFillDefault - autofill the formula from C2 to C100.
Related
For i = 1 To staffelRange.Rows.Count
.DataBodyRange.Cells(i, countHeader).Formula = staffelRange.Cells(i).Formula
Next
the code works as intended and copies the formulas from my range into the desired range in my table. first to the formula, the formula always refers to the column header and the cell left of it, if i copy the code now with my function from column 1 to for example column 4, the formula still refers to column 1 instead of column 4. but if i copy it by dragging the formula over to the other cell, the formula adapts. How can I achieve such an adjustment in my code?
Formula:
=$G39*(1+SVERWEIS(Stückpreise_neu_19[[#Kopfzeilen];[Staffel1]];Rabattstaffel_new_24;2;FALSCH))
If the formula is always the same except for the cell reference, you can just write the formula to sheet as a string value with a variable for the correct row number.
Something like...
For i = 1 To staffelRange.Rows.Count
.DataBodyRange.Cells(i, countHeader).Formula = "=$G" & i & "*1+SVERWEIS(Stückpreise_neu_19[[#Kopfzeilen];Staffel1]];Rabattstaffel_new_24;2;FALSCH))"
Next
This is assuming your variable i represents the correct row number for the formula (which to my understanding is correct).
I want to copy the cell values only and not the formula which is calculating the value of the cell which i am coping
Range("V186").Select
Selection.Copy
Range("N215").Select
ActiveSheet.Paste
copying cell value with formula and showing #REF! instead of value
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
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
im new to this and i want to ask one question if anyone can help.
im working with excel 2003 and what i want is that i have one column which at cell A1 i have name and in cell A2 i have surname, in cell A3 i have name and in cell A4 i have surname and so on.
what i want to do is to merge cell A1 with A2 and then A3 with A4 and so on.
its about 3000 rows.
is there any vba code to do it automatically?
You don't really need a VBA program for this, just use Excel formulas as so:
In the column next to your names fill a sequential list of numbers starting with 1. So this should have 1, 2, 3, 4 and so forth. You can go to the bottom of your A column, but half-way will be fine. TO do this quickly, type a 1 in the first row (B1), then in the second (B2) add a formula: =B1+1. Then copy this formula all the way down B column to the extent of the A column. The trick here is to go to B2, select Edit/Copy, then move to A column and Ctrl-Down, move to the B column and then use Shift+Ctrl-Up, then paste.
In C column you will put a formula to merge two cells together from A column, as so:
=INDEX($A$1:$A$<ENDROW>,(B1-1)*2+1,1)&" "&INDEX($A$1:$A$<ENDROW>,(B1-1)*2+2,1)
Where <ENDROW> is the last row in A.
Copy this formula down at least half the rows of the A column. If you go beyond this, you will get #REF! error; just clear those cells.
Now you want to copy the formulas in C to another column, say D. Select them, use Edit/Copy, then move to D1 and right-click and choose "Paste special". In the dialog click on "Values" and then "OK". Now you have a column with the values of every two rows concatenated together.
to do this in vba:
Sub nameMerger()
Range("A1").Select
While ActiveCell.Value <> vbNullString
ActiveCell.Value = ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 1).EntireColumn.Delete
ActiveCell.Offset(0, 1).Select
Wend
End Sub
Tested and works