1

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.

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.

Sub format_worksheets() Dim ws As Worksheet ws = Worksheet.Active Do code Loop Until ws = Sheets(Sheets.Count).Active End Sub 

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That worked - thank you very much. I can't believe it was that simple. Appreciate your input.
Thanks again - appreciate the help.
You should mark the question as solved by clicking the green tick. This prevents other developers (like my self) opening your questions thinking you still need help.
1

I was trying to figure out what you were trying to do with method 2, and I could not. 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.