如何通过VBA在特定单元格中添加文本

时间:2017-03-08 01:06:10

标签: excel vba excel-vba

我想把我的床单放在牢房里:

"I7" the word "Bronze"
"I8" the word "Silver"
"I9" the word "Gold"
"I10" the word "Platin"

因为在单元格“J7”上我有一个总计青铜的公式。 这就是这个:

Sheets("Download").Select
Cells.Select
Range("J7").Select
ActiveCell.Formula = "= COUNTIF(C:C,""Bronze"")"

  Sheets("Download").Select
Cells.Select
Range("J8").Select
ActiveCell.Formula = "= COUNTIF(C:C,""Silver"")"

  Sheets("Download").Select
Cells.Select
Range("J9").Select
ActiveCell.Formula = "= COUNTIF(C:C,""Gold"")"

  Sheets("Download").Select
Cells.Select
Range("J10").Select
ActiveCell.Formula = "= COUNTIF(C:C,""Platin"")"

  Sheets("Download").Select
Cells.Select
Range("J11").Select
ActiveCell.Formula = "= COUNTIF(C:C,""PlPlus"")"

  Sheets("Download").Select
Cells.Select
Range("J12").Select
ActiveCell.Formula = "= COUNTIF(C:C,""Ambass"")"

  Sheets("Download").Select
Cells.Select
Range("J13").Select
ActiveCell.Formula = "= SUM(J7:J12)"

它给了我数字,但为了方便我想看看青铜,银等的总数......

2 个答案:

答案 0 :(得分:0)

类似的东西:

Dim arr, i

arr = Array("Bronze","Silver","Gold","Platin")'<< add the rest here...

For i = lbound(arr) to UBound(arr)
    With Sheets("Download").Range("I7")
       .Offset(i, 0).Value = arr(i)
       .Offset(i, 1).Formula = "= COUNTIF(C:C,""" & arr(i) & """)"
    End With
Next i

答案 1 :(得分:0)

Sub TEST()
Const kFml As String = "=COUNTIF(C[-7],RC9)"    'Use a constant to define the formula
Dim aValues As Variant
aValues = [{"Bronze","Silver","Gold","Platin","PlPlus","Ambass"}]   'Use an array to contain all the values

    With Sheets("Download").Range("I7:I12")                         'Work with the target range
        .Columns(1).Value = WorksheetFunction.Transpose(aValues)    'Applies the values to 1st column `I`
        .Columns(2).FormulaR1C1 = kFml                              'Applies the formula to 2nd column `J`
    End With
    Sheets("Download").Range("J13").Formula = "= SUM(J7:J12)"       'Enters total formula
End Sub