You are here:

Excel/Adding a Loop to macro

Advertisement


Question
QUESTION: Hi Tom,

I have 172 columns of data. I want to format the columns to text. I have recorded the macro below for the action to do.
I need your help to modify the macro to repeat the action to all the columns on the file; for the same number of rows.



   Range("B1:B8").Select
   Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
       Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
       :=Array(1, 1)
       

   Range("C1:C8").Select
   Selection.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, _
       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
       Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
       :=Array(1, 1)

Pls assist.

Thanks in advance

Kauther

ANSWER: Kauther,

so it looks like you want to process  the range $B$1:$FQ$8 one column at a time.  Then

for i = 2 to 173  ' columns B to
Cells(1,i).Select
Selection.TextToColumns Destination:=Cells(1,i), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1)
Next



--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Hi Tom,

I pasted the code below the way you gave and it returning an error; runtime error '1004' - No data was selected to parse.

How do i fix this? Pls help.Thanks.

Kauther.

Sub RepeatColTexttoColumn()
'
' RepeatColTexttoColumnMacro
'

'
   For i = 2 To 173  ' columns B to
Cells(1, i).Select
Selection.TextToColumns Destination:=Cells(1, i), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1)
Next

End Sub

Answer
Kauther,

I tested this and it worked for me as long as there was data in each column of cells in rows 1 to 8.  It processed the range B1:FQ8

Sub RepeatColTexttoColumn()
'
' RepeatColTexttoColumnMacro
'

'
   For i = 2 To 173  ' columns B to
Cells(1, i).Resize(8, 1).Select
Selection.TextToColumns Destination:=Cells(1, i), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1)
Next

End Sub


I can't say it will do what you want because I know nothing about your data, but it will do text to columns on each 8 row column (individually) in columns B to FQ.

I had left out the Resize(8,1) but I have added it in.
--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.