gpt4 book ai didi

vba - Excel 迭代工作表

转载 作者:行者123 更新时间:2023-12-02 10:43:16 25 4
gpt4 key购买 nike

我有一个包含许多工作表的工作簿,如果前面的单元格与给定的字符串匹配,我需要在其中输入一个值。

我的代码适用于我需要的工作表,但当它到达一些不需要的工作表(其中也有数据)时,它会出错。

调试器突出显示的行是For Each r In Intersect (ActiveSheet.UsedRange, Range("F:F") 我是 VBA/Excel 脚本编写的新手。抱歉,如果这有点盲目显而易见,但我搜索了该网站,但找不到合适的答案,或者我只是不认识它。

Sub AllOnePool()
Dim myStr As String
Dim myPool As String
Dim sh As Worksheet
Dim xlCalc As XlCalculation

myStr = InputBox(Prompt:="Input the Target Serial Number: e.g. 93127")
myPool = InputBox(Prompt:="Input the Pool to Use: ")

For Each sh In ActiveWorkbook.Worksheets
sh.Activate
For Each r In Intersect(ActiveSheet.UsedRange, Range("F:F"))
If r.Text = myStr Then
r.Offset(0, 1) = myPool
End If
Next r
Next sh

End Sub

最佳答案

您需要检查两个范围是否相交:

Sub AllOnePool()
Dim myStr As String
Dim myPool As String
Dim sh As Worksheet
Dim cel As Range
Dim xIng As Range

myStr = InputBox(Prompt:="Input the Target Serial Number: i.e. 93127")
myPool = InputBox(Prompt:="Input the Pool to Use: ")

For Each sh In ActiveWorkbook.Worksheets
With sh
Set xIng = Intersect(.UsedRange, .Range("F:F"))
If Not xIng Is Nothing Then
For Each cel In xIng
If cel.Text = myStr Then cel.Offset(0, 1) = myPool
Next
End If
End With
Next
End Sub

关于vba - Excel 迭代工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196867/

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