gpt4 book ai didi

excel - 检查工作簿 VBA 中的出现次数

转载 作者:行者123 更新时间:2023-12-04 21:52:47 25 4
gpt4 key购买 nike

我正在尝试遍历工作簿中的许多工作表,看看是否存在 foo 和 bar 然后添加到这个特定的单元格。出于某种原因,我的代码会遍历工作表,但不会添加到单元格中。

Sub Main()

Dim ws As Worksheets
Dim starting_ws As Worksheet
Set starting_ws = ActiveSheet
ws_num = ThisWorkbook.Worksheets.Count
ind = 9
For I = 1 To ws_num
ThisWorkbook.Worksheets(I).Activate
Do While ind <= 39
If Worksheets(I).Range("A" & ind).Value = "bar" And Worksheets(I).Range("G" & ind).Value = "foo" Then
Worksheets("scrap").Range("C7").Value = Worksheets("scrap").Range("C7").Value + 1
Exit For
End If
ind = ind + 1
Loop

Next

End Sub

最佳答案

也许

Sub tgr()

Dim ws As Worksheet
Dim rOutput As Range
Dim lCount As Long

Set rOutput = ThisWorkbook.Sheets("scrap").Range("C7")

For Each ws In ThisWorkbook.Worksheets
lCount = lCount + WorksheetFunction.CountIfs(ws.Range("A9:A39"), "bar", ws.Range("G9:G39"), "foo")
Next ws

rOutput.Value = rOutput.Value + lCount

End Sub

关于excel - 检查工作簿 VBA 中的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com