Excel VBA- How to loop through specific sheets in a workbook and format the same ranges in each sheet

huangapple go评论105阅读模式
英文:

Excel VBA- How to loop through specific sheets in a workbook and format the same ranges in each sheet

问题

Sub Macro1()
'

  1. Dim wsList() As String, wsName As Variant, ws As Worksheet
  2. wsList = Split("1 2 3 4 5 6 7 8 9 10", " ")
  3. For Each wsName In wsList
  4. Set ws = ThisWorkbook.Sheets(wsName)
  5. Range("A4:C7,L4:Q5,L6:L7,S24:U27,AD24:AF27,AO28:AR29,AO26:AO27").Select
  6. With Selection.Interior
  7. .Pattern = xlNone
  8. .TintAndShade = 0
  9. .PatternTintAndShade = 0
  10. End With
  11. Range("B4:C7,M4:Q5,T24:U27,AE24:AF27,AP28:AR29").Select
  12. Selection.Locked = False
  13. Selection.FormulaHidden = False
  14. Range("O31:AF34").Select
  15. ActiveCell.FormulaR1C1 = "FALL"
  16. Range("O35&quot).Select
  17. Next wsName

End Sub

英文:

I have 10 sheets labeled 1-10 respectively and I want to loop through each sheet and format the same ranges in each sheet.

The below code works but I cannot get it to loop through each sheet

  1. Sub Macro1()
  2. '
  3. Dim wsList() As String, wsName As Variant, ws As Worksheet
  4. wsList = Split("1 2 3 4 5 6 7 8 9 10", " ")
  5. For Each wsName In wsList
  6. Set ws = ThisWorkbook.Sheets(wsName)
  7. Range("A4:C7,L4:Q5,L6:L7,S24:U27,AD24:AF27,AO28:AR29,AO26:AO27").Select
  8. With Selection.Interior
  9. .Pattern = xlNone
  10. .TintAndShade = 0
  11. .PatternTintAndShade = 0
  12. End With
  13. Range("B4:C7,M4:Q5,T24:U27,AE24:AF27,AP28:AR29").Select
  14. Selection.Locked = False
  15. Selection.FormulaHidden = False
  16. Range("O31:AF34").Select
  17. ActiveCell.FormulaR1C1 = "FALL"
  18. Range("O35").Select
  19. Next wsName
  20. End Sub
  21. </details>
  22. # 答案1
  23. **得分**: 1
  24. Here's the translated content:
  25. 如果你写 `Range(...)`,VBA 将始终使用 `ActiveSheet`。你想要的是它在工作表 `ws` 上运行,因此你必须告诉 VBA:`ws.Range(...)`。
  26. 另外,没有必要使用 `Select` 和 `Selection`(你应该避免使用它,几乎从不需要):相反,要么将 Range 分配给一个变量并使用它,要么使用 `With` 子句:

Dim wsList() As String, wsName As Variant, ws As Worksheet

wsList = Split("1 2 3 4 5 6 7 8 9 10", " ")

For Each wsName In wsList
Set ws = ThisWorkbook.Sheets(wsName)

  1. ' 选项 a) 使用变量
  2. Dim myRange as Range
  3. Set myRange = ws.Range("A4:C7,L4:Q5,L6:L7,S24:U27,AD24:AF27,AO28:AR29,AO26:AO27")
  4. With myRange.Interior
  5. .Pattern = xlNone
  6. .TintAndShade = 0
  7. .PatternTintAndShade = 0
  8. End With
  9. ' 或者,不使用 With
  10. myRange.Interior.Pattern = xlNone
  11. myRange.Interior.TintAndShade = 0
  12. myRange.Interior.PatternTintAndShade = 0
  13. ' 选项 b) 直接使用中间变量的 With
  14. With ws.Range("B4:C7,M4:Q5,T24:U27,AE24:AF27,AP28:AR29")
  15. .Locked = False
  16. .FormulaHidden = False
  17. End With
  18. ws.Range("O31:AF34").FormulaR1C1 = "FALL"

Next wsName

  1. 顺便说一下,你的关于范围 "O31:AF34" 的公式看起来奇怪。这不是一个有效的公式。如果你想将文本 "&quot;FALL&quot;" 写入单元格,请使用 `ws.Range("O31:AF34").Value = "FALL"`
  2. <details>
  3. <summary>英文:</summary>
  4. If you write `Range(...)`, VBA will always use the `ActiveSheet`. What you want is that is works on worksheet `ws`, so you have to tell VBA it: `ws.Range(...)`.
  5. Also, there is no need to use `Select` and `Selection` (and you should avoid it, it is almost never needed): Instead, either assign the Range to a variable and use that, or use a `With`-clause:

Dim wsList() As String, wsName As Variant, ws As Worksheet

wsList = Split("1 2 3 4 5 6 7 8 9 10", " ")

  1. For Each wsName In wsList
  2. Set ws = ThisWorkbook.Sheets(wsName)
  3. &#39; Option a) Use a Variable
  4. Dim myRange as Range
  5. Set myRange = ws.Range(&quot;A4:C7,L4:Q5,L6:L7,S24:U27,AD24:AF27,AO28:AR29,AO26:AO27&quot;)
  6. With myRange.Interior
  7. .Pattern = xlNone
  8. .TintAndShade = 0
  9. .PatternTintAndShade = 0
  10. End With
  11. &#39; Or, without With
  12. myRange.Interior.Pattern = xlNone
  13. myRange.Interior.TintAndShade = 0
  14. myRange.Interior.PatternTintAndShade = 0
  15. &#39; Option b) Use With directly with intermediate variable
  16. With ws.Range(&quot;B4:C7,M4:Q5,T24:U27,AE24:AF27,AP28:AR29&quot;)
  17. .Locked = False
  18. .FormulaHidden = False
  19. End With
  20. ws.Range(&quot;O31:AF34&quot;).FormulaR1C1 = &quot;FALL&quot;
  21. Next wsName
  1. Your formula for the range &quot;O31:AF34&quot; looks odd, btw. This is not a valid formula. If you want to write the text `&quot;FALL&quot;` into the cell, use `ws.Range(&quot;O31:AF34&quot;).Value = &quot;FALL&quot;`
  2. </details>

huangapple
  • 本文由 发表于 2023年3月21日 02:39:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定