PDA

View Full Version : Solved: Why doesn't this work? Apply macro to every sheet in book



pcollins
11-08-2007, 03:05 PM
I was able to write it a different way to do what I wanted but why does this code not work? I wanted it to do this to every sheet in the workbook but it would only do it to the first sheet.


Sub parse_csv_data()
Dim wsheet As Worksheet
For Each wsheet In Workbook
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1)), TrailingMinusNumbers:= _
True
Next wsheet


End Sub

rory
11-08-2007, 04:27 PM
You need to either activate each sheet in the loop, or specify that the Columns belong to each sheet:
Sub parse_csv_data()
Dim wsheet As Worksheet
For Each wsheet In Workbook
wsheet.Columns("A:A").TextToColumns Destination:=wsheet.Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1)), TrailingMinusNumbers:= _
True
Next wsheet


End Sub

pcollins
11-08-2007, 04:58 PM
Ah Thanks I solved it with a For-Next loop and did activate each sheet. I didnt think you needed that for a For Each but I guess you do.

rory
11-08-2007, 05:09 PM
You don't need to activate each sheet if you specify the sheet that the Columns belong to, as in my post above - i.e. you use:
wsheet.Columns("A:A").Select rather than just:
Columns("A:A").Select
which assumes you are referring to the active sheet.