Excel iterate through worksheets

前端 未结 1 983
独厮守ぢ
独厮守ぢ 2021-01-13 15:44

I have a workbook with many sheets and into some I need to enter a value if the preceding cell matches a given string.

My code works for the sheets I need it to but

相关标签:
1条回答
  • 2021-01-13 15:57

    You need to check that the 2 ranges intersect:

    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
    
    0 讨论(0)
提交回复
热议问题