从Excel中的COUNTIF函数中提取工作表名称和条件

时间:2015-06-05 14:23:40

标签: excel vba countif

您好我有一个COUNTIF公式: RawData_Chq!G2:G5000,"*ICS*" 。使用VBA,我希望能够在1个字符串中提取sheetname:RawData_Chq,在另一个字符串中提取标准ICS。 我该怎么办呢?

Dim formula_extract As String
Dim CI_Name As String
Dim ButtonName As String
Dim CI_Extract As String
Dim Sheet_Extract As String
Dim Split_Formula() As String
ButtonName = Application.Caller
 Set b = ActiveSheet.Buttons(ButtonName)
  r = b.TopLeftCell.Row
  CI_Name = Cells(r, 2).Value
 formula_extract = Cells(r, 3).Formula
Split_Formula = Split(formula_extract, ",")

 Sheet_Extract = Left(Split_Formula(0), InStr("!", Split_Formula(0)))
 CI_Extract = Split_Formula(1)

1 个答案:

答案 0 :(得分:0)

我认为应该这样做,只需将公式拆分为单个元素然后解析它们以删除任何不需要的数据:

CI_Name = Cells(r, 2).Value
formula_extract = [A1].Formula
Split_Formula = Split(formula_extract, ",")

Dim functionString As String
'Use this later to get rid of these characters with the sheet_extract
functionString = "=COUNTIF("

'The worksheet name:
Sheet_Extract = Left(Split_Formula(0), InStr(Split_Formula(0), "!") - 1)
Sheet_Extract = Replace(Sheet_Extract, functionString, vbNullString)

'The criteria:
CI_Extract = Split_Formula(1)