我想在每张工作表中以相同的方式格式化工作表,目前代码工作正常,但for each ws in workbook
部分除外。
所有代码执行得足够好,但仅在活动表中。 我在这里缺少什么?
提前致谢,
Option Explicit
Sub prepareForInput()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim answer As Variant
Dim i As Integer, lastcol As Integer
answer = MsgBox("Would you like to update the tracker sheet automatically?", vbYesNo)
If answer = vbYes Then
For Each ws In ActiveWorkbook.Worksheets
Range("A1:A100").EntireRow.Hidden = False
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column - 11
'hide newly unneeded columns
For i = 1 To 3
Columns(lastcol).Hidden = True
lastcol = lastcol + 1
Next i
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
'paste across columns
For i = 1 To 3
Columns(lastcol).Copy Columns(lastcol + 3)
lastcol = lastcol - 1
Next i
Next ws
End If
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:3)
For Each Ws In ActiveWorkbook.Worksheets
With Ws
.Range("A1:A100").EntireRow.Hidden = False
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 11
'hide newly unneeded columns
For i = 1 To 3
.Columns(lastcol).Hidden = True
lastcol = lastcol + 1
Next i
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'paste across columns
For i = 1 To 3
.Columns(lastcol).Copy .Columns(lastcol + 3)
lastcol = lastcol - 1
Next i
End With
Next Ws
答案 1 :(得分:0)
你需要使用ws为Range(“a1:a10”)添加前缀,所以ws.range(“a1:a10”)或在每个循环中选择ws。没有Range&细胞将作用于活性表。