简体   繁体   English

使用VBA在单元格中编写公式

[英]Use vba to write formula in cell

My basic formula is : =IF(COUNTIF($A:$A,$A2)=COUNTIFS($A:$A,$A2,$B:$B,$B2),"OK","NOT OK") 我的基本公式是: =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. 我用它来知道A列中是否有重复项,并检查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. 现在,我想用VBA编写它,所以我记录一个宏,选择单元格,按F2,然后按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. 我知道如何“手动”使用公式化器,也知道如何在vba中的单个单元格中使用它,但是我不知道在循环中使用它很热。 (I have to use the formula on 25+ column that's why I need loop and variable) (我必须在25+列上使用公式,这就是为什么我需要循环和变量的原因)

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. 您只需要在For r = 2 To 25中将25修改为要填充公式的最后一行。

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: 所以:

  1. my excel file has A to BQ column 我的excel文件具有A到BQ列
  2. from L to BQ, one out of two colum is data, and the next is the formula 从L到BQ,两个列中的一个是数据,第二个是公式
  3. My ID is always column C 我的ID始终是C列
  4. basically M column will use column C to check the ID and column L to check the value. 基本上,M列将使用C列检查ID,而L列则使用值。

Now the formulas: cell M2 with Excel: 现在,公式为:带Excel的单元格M2:

=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$L:$L;L2);"PRODUIT";"ARTICLE")

Cell M2 with VBA: 带有VBA的单元格M2:

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: 现在在Excel中将单元格O2(基于C和N列):

=IF(COUNTIF($C:$C;C2)=COUNTIFS($C:$C;C2;$N:$N;N2);"PRODUIT";"ARTICLE")

Finally cell O2 with VBA: 最后是带有VBA的单元格O2:

    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 现在我想使用一个循环(从L到BQ,执行步骤2)以变量填充M2,O2,Q2,S2 ...

I hope you get it this time, sorry again for my low english level 希望这次您能收到,对不起,我英语水平太低

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM