My basic formula is : =IF(COUNTIF($A:$A,$A2)=COUNTIFS($A:$A,$A2,$B:$B,$B2),"OK","NOT OK")
I use it to know if there are duplicate in column A, and i check the value in B.
ID Age My formula
1 15 NOT OK
2 50 OK
2 50 OK
3 35 OK
1 15 NOT OK
1 16 NOT OK
Now i'd like to write it with VBA, so I record a maccro, select the cell, press F2 then press Enter. I get:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(C3,RC[-10])=COUNTIFS(C3,RC[-10],C12,RC[-1]),""PRODUIT"",""ARTICLE"")"
Alright, it work. Now I'd like to use this formula with a new column:
Id Age Age formula Money Money formula Value3 Value3 formula ...
1 15 NOT OK 150 OK ... ...
2 50 OK 5 NOT OK ...
2 50 OK 800 NOT OK
3 35 OK 80 OK
1 15 NOT OK 150 OK
1 16 NOT OK 150 OK
I know how to use the formular "by hand" and I know how to use it for a single cell in vba, but I donĀ“t know hot to use it in a loop. (I have to use the formula on 25+ column that's why I need loop and variable)
Sorry for my bad english,
Thanks in advance
You just need to edit the 25 in For r = 2 To 25 to the last row you want to fill with the formula.
Code:
'Loops throug the rows
For r = 2 To 25
'Loops throug the columns
For c = 13 To 69 Step 2
'Converts the column-index into the column-letter
Dim col As String
col = Split(Cells(r, c - 1).Address(True, False), "$")(0)
'Writes the formula into the cell
Cells(r, c).Formula = "=IF(COUNTIF($C:$C,C" & r & ")=COUNTIFS($C:$C,C" & r & ",$" & col & ":$" & col & "," & col & r & "),""PRODUIT"",""ARTICLE"")"
Next
Next
Thank you tony for your time.
So:
my excel file has A to BQ column
from L to BQ, one out of two colum is data, and the next is the formula
My ID is always column C
basically M column will use column C to check the ID and column L to check the value.
Now the formulas:
cell M2 with Excel:
=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$L:$L;L2);"PRODUIT";"ARTICLE")
Cell M2 with VBA:
ActiveCell.FormulaR1C1 = _ "=IF(COUNTIF(C3,RC[-10])=COUNTIFS(C3,RC[-10],C12,RC[-1]),""PRODUIT"",""ARTICLE"")"
Now cell O2 (based on C and N columns) in Excel:
=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$N:$N;N2);"PRODUIT";"ARTICLE")
Finally cell O2 with VBA:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTIF(C3,RC[-12])=COUNTIFS(C3,RC[-12],C14,RC[-1]),""PRODUIT"",""ARTICLE"")"
Now i'd like to use a loop (from L to BQ with step 2) to fill M2, O2, Q2, S2... with variable
I hope you get it this time,
sorry again for my low english level
Related
I would really appreciate if you could help me out with this problem. I am trying to get vba to provide me all the numbers in increment of 1 from 1 to a value in cell G1 and populate it into column C. Afterwards, I want vba to check each cell in column C starting from C1 to see if the value is greater than a number and to populate into the column next to it( Column D1 onwards )
For example, cell G1 has the number 5.
So, I should see the following in column c , which are the 1, 2,3,4,5 and in column D I should see only the value for cells greater than say 3. So that means only the value 4, and 5 is populated in columnn D.
I would appreciate any help as I am quite new to VBA and am trying to get a hang of it.
Thnx.
Give this a try:
Sub elyas()
Dim i As Long, MagicNumber As Long
Dim k As Long
MagicNumber = 3
k = 1
For i = 1 To [G1]
Cells(i, "C").Value = i
If i > MagicNumber Then
Cells(k, "D").Value = i
k = k + 1
End If
Next i
End Sub
I'm trying to enter a formula in a sheet using VBA (for later use in Excel Solver). It worked fine until this morning.
For u = 1 To Row2
Sheets("Testa").Cells(u + 1, 14).FormulaArray = "=SUM(IF(B2:B2000=" & CStr(u) & ",F2:H2000,0))"
Next
For v = 1 To Row
Sheets("Testa").Cells(v + 1, 18).FormulaArray = "=SUM(IF(A2:A2000=" & CStr(v) & ",F2:H2000,0))"
Next
The first loop gives this as a result in the cell: "=SUM(IF(RC[-12]:R[1998]C[-12]=1,RC[-8]:R[1998]C[-6],0))", changing the absolute references to relative references and not translating the functions to the french equivalent. The second loop works just fine. Column A and B contain a list of numbers (from 1 to Row or Row2) that look like this :
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
I don't know what I'm missing. Any help is welcome !
The range where I was writing the formula was set up as a text range, explaining why it didn't compute the formula. Changing it manually to a standard range solved the problem.
I have a spreadsheet which I am cleaning and using macros to help. My column 'C' has temperature data. Like with all data, there is some missing. How would I write a macro that would auto-fill the missing spot with previous data?
For example:
C C
1 37 1 37
2 35 2 35
3 --------> 3 35
4 37 4 37
5 36 5 36
The spot C3 has been filled with C2's data.
Thank you for your help.
Do you really need VBA for this?
Do this
Select Col C
Press Ctrl + G
Click on Special
Next Click on Blanks
Click Ok
All Empty cells are now selected. Press the = key and then the Up arrow key
Lastly press Ctrl+Tab+Enter and you are done.
ScreenShot
Give this a try:
Sub FixC()
Dim N As Long, i As Long
N = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To N
If Cells(i, "C") = "" Then
Cells(i, "C") = Cells(i - 1, "C")
End If
Next i
End Sub
How would I write that macro:
This contains only snippets.
loop over all cells in the column:
for each cell in ActiveSheet.Columns(1).Cells
if the cell value is not empty -> save the value to a variable
If cell.value <> Empty then lastCellValue = cell.value
if the cell value is empty -> write the saved cell value into the cell
Else cell.value = lastCellValue
also:
if more than x (e.g. 20) cells in a row were empty, break from loop
I was hoping to write a Macro that does a very repetitive task for me but entering VBA is harder than expected. I will learn how to program macros for excel when I have some time because it seem extremely useful, but I can't spend 5 to 12 hours this week.
Maybe someone here can help!
I have a few excel files that follow this pattern:
Column C - Column D
--------------------
text | (empty)
number | (empty)
number | (empty)
text | (empty)
number | (empty)
text | (empty)
text | (empty)
number | (empty)
text | (empty)
number | (empty)
Where text and number alternate randomly for a few thousand cells. I need column D to hold, when column C is a number, the difference with previous number, otherwise it must stay blank:
Column C - Column D
--------------------
text | (empty)
3 | (empty)
14 | (=C3-C2) : 11
text | (empty)
16 | (=C5-C3) : 2
text | (empty)
text | (empty)
21 | (=C8-C5) : 5
22 | (=C9-C8) : 1
So the algorithm is:
var previousNumberCell
var i = 1
for all (selected) cells/rows
if (Row(i).column(C) holds number) {
Row(i).column(D).value = "=C"+i+"-"C"+previousNumberCell
previousNumberCell = i;
}
i++
End
I don't care if for the first or last cell it doesn't work.
Thank you so much for the help, or if you can point me to where I can find the answer to this.
EDIT: this is a simplified version of the problem, there are 2 things I don't know how do well with excel macros: select a cell, and tell if cell is a number... for the record, number cells have been converted from text to number format.
Give this a shot:
Sub MyMacro()
Dim rng as Range
Dim cl as Range
Dim lastNum as Range
Set rng = Range(Selection.Address) 'Make sure that your active SELECTION is correct before running the macro'
If Not rng.Columns.Count = 1 Then
MsgBox "Please select only 1 column of data at a time.",vbCritical
Exit SUb
Else:
For each cl in rng
If IsNumeric(cl.Value) Then
If lastNum Is Nothing Then
cl.Offset(0,1).Formula = "=" & cl.Address
Else:
cl.Offset(0,1).Formula = "=" & cl.Address & "-" & lastNum.Address
End If
set lastNum = cl
End If
Next
End If
End Sub
Do you require VBA?
Insert a new Column before column C
Column C with your values becomes column D
You might need columnheaders..
In cell C2 put: =IF(E2=0;0;SUM(E$2:$E2)) this identifies rows with number
In cell E2 put: =IF(ISNUMBER(D2);1;0) this sets order for each row with a number to use next in vlookup
in cell F2 put: =IF(ISNUMBER(D2);ABS(D2-VLOOKUP(MAX($C$1:C1);$C$1:D1;2;0));"")
Autofill columns C, E and F.
In column F you get your results, except first, which is "#VALUE"
Hi you can do this with an if formula and a named formula . if (isnumber ,named formula,0)
named formula (=lookup formula)
I am a new beginner of VBA and I am trying to write a sumif function in each row of column D but will change the criteria and sum column range for each row until the last value of the criteria column.
For example: start from row 4, the first sum & criteria range will be 4:4, the second one will be 4:5 and the third one will be 4:6, etc.
I want the answer should be looked like the example below but it only show 8 for those row with "x" in column A.
Dose anyone have any idea to make it show as column D in the example below.
Worksheet:
column A B C D E
4 x 1 1
5 x 2 3
6 y 1
7 y 3
8 z 4
9 x 5 8
Code:
llistrow = ThisWorkbook.Worksheets(Source).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row - 1
For i = 4 To llistrow Step 1
revisedbudget1314 = Application.WorksheetFunction.SumIf( _
Arg1:=ThisWorkbook.Worksheets(Source).Range(Cells(4, 1), Cells(i, 1)), _
Arg2:="x", _
Arg3:=ThisWorkbook.Worksheets(Source).Range(Cells(4, 3), Cells(i, 3)))
Next i
Enter this formula in cell D1
=IF(A1="x", SUMIF(A$1:A1, "=x", C$1:C1), "")
and just multiply it for the other cells of column D (select D1 and double click on the small rectangle in lower right corner)
Please use better tags for your questions (e.g. excel or spreadsheet) so more people can look at them.