请写一段VBA代码,帮我解决以下问题:我有一个exl表格,表格中有多个sheet,请在所有sheet中检索文本“特瑞普利单抗”,并统计文本“特瑞普利单抗”出现的次数,将结果写入到sheet2中。注意,“特瑞普利单抗”可能是单元格中数据的一部分片段,这样也计入次数。
时间: 2024-02-11 12:04:06 浏览: 71
好的,以下是VBA代码,请将其复制到Excel文件的VBA编辑器中,然后运行它即可:
```
Sub countOccurrences()
Dim searchValue As String
Dim count As Long
Dim sheetCount As Long
Dim i As Long, j As Long
Dim cellValue As String
'设置搜索值
searchValue = "特瑞普利单抗"
'设置初始计数器
count = 0
'获取sheet数
sheetCount = Sheets.Count
'循环所有sheet
For i = 1 To sheetCount
'获取当前sheet
ActiveWorkbook.Sheets(i).Activate
'获取当前sheet的行数和列数
Dim rowCount As Long
Dim colCount As Long
rowCount = ActiveSheet.UsedRange.Rows.Count
colCount = ActiveSheet.UsedRange.Columns.Count
'循环每个单元格,查找搜索值
For j = 1 To rowCount
For k = 1 To colCount
cellValue = ActiveSheet.Cells(j, k).Value
If InStr(cellValue, searchValue) > 0 Then
count = count + 1
End If
Next k
Next j
Next i
'将结果写入sheet2
Sheets("Sheet2").Cells(1, 1).Value = "特瑞普利单抗出现次数:"
Sheets("Sheet2").Cells(1, 2).Value = count
End Sub
```
注意,这里假设你要将结果写入到名为“Sheet2”的sheet中。如果你要写入到别的sheet中,请将代码中的“Sheet2”替换成相应的sheet名。
阅读全文