What have I messed up in the VBA loop for each worksheet?

前端 未结 1 1574
悲&欢浪女
悲&欢浪女 2021-01-27 00:35

I currently have to send multiple letters out at one time and often replace only 1 or two words within a cell. The problem is that I need those words to be bolded and it would b

相关标签:
1条回答
  • 2021-01-27 00:52

    You are setting up a loop to go through each worksheet (using ws as your reference to the sheet currently being processed), but then processing a range on the ActiveSheet. Use ws instead of ActiveSheet.

    You should also set rng to Nothing before attempting to set it to the UsedRange.SpecialCells or else, if that crashes, your If rng Is Nothing Then statement won't work (because rng will still be set to whatever it was set to on the previous iteration through the loop).

    '...
    For Each ws In ActiveWorkbook.Worksheets
        Set rng = Nothing
        On Error Resume Next
        Set rng = ws.UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)
        On Error GoTo ErrHandler
        If rng Is Nothing Then
    '...
    
    0 讨论(0)
提交回复
热议问题