[英]Loop until last worksheet
I am trying to apply a format to every worksheet in my current workbook. 我正在尝试将格式应用于当前工作簿中的每个工作表。 I have tried "For Each" and I have tried to Loop through until I reach last worksheet but both error due to different reasons.
我尝试了“对于每个人”,并且尝试遍历直到到达上一个工作表,但是由于不同的原因,这两个错误。 Can someone please tell me what I'm doing wrong?
有人可以告诉我我在做什么错吗?
Method 1: It works on the 1st worksheet but not on the remaining worksheets. 方法1:它适用于第一个工作表,但不适用于其余工作表。
Sub format_worksheets()
Dim ws As Worksheet
For Each ws In Worksheets
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False,
Other:=True,TrailingMinusNumbers:=True
Range("A1").Select
Next ws
End Sub
Method 2: It doesn't recognise the last worksheet. 方法2:无法识别最后一个工作表。
Sub format_worksheets()
Dim ws As Worksheet
ws = Worksheet.Active
Do
code
Loop Until ws = Sheets(Sheets.Count).Active
End Sub
In method one, try changing: 在方法一中,尝试更改:
Columns("A:A").Select
To 至
ws.Activate
ws.Columns("A:A").Select
... to activate the worksheet and specify the worksheet you want to work with. ...激活工作表并指定要使用的工作表。
I was trying to figure out what you were trying to do with method 2, and I could not. 我试图弄清楚您要如何使用方法2,但我做不到。 But just because, here's a working version that does not use for each.
只是因为,这是每个版本都不使用的有效版本。
Sub format_worksheets()
dim x as Integer
For x = 1 To Sheets.Count
Sheets(x).Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=True, TrailingMinusNumbers:=True
Next x
End Sub
Note that this will error if any sheet has no data in Column A. I also left out the select statements because it's best practice to avoid them if they are not necessary. 请注意,如果任何工作表的A列中没有数据,这将出错。我还省略了select语句,因为最佳实践是避免不必要的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.