Cleaning up a Text To Column Macro Recording Replacing "Select"

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
Hi all, again, TIA!
I'm trying to figure out how to clean up the "select" in a simple single column (A) text to column. When I try to revise "select" to a range, I get a "Can only convert one column." error.
2nd question. Why does the array start with "0" and not "1"?

Recording
VBA Code:
Range("A2").End(xlDown).Select
    Selection.TextToColumns  Destination:=Range("A2"),   DataType:=xlFixedWidth,  FieldInfo:=Array(Array(0, 2),  Array(9, 9))
This won't work
VBA Code:
Dim LastCol         As Integer
Dim LastRow         As Long
Dim Rng             As Range

Set Rng = Range("$A2", Cells(LastRow))
With Rng
              .TextToColumns Destination:=Range("A2"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(9, 9))
End With 'Rng

Ron
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I would think you could replace the recorded macro:
VBA Code:
Range("A2").End(xlDown).Select
    Selection.TextToColumns  Destination:=Range("A2"),   DataType:=xlFixedWidth,  FieldInfo:=Array(Array(0, 2),  Array(9, 9))

to:
VBA Code:
Range("A2").End(xlDown).TextToColumns Destination:=Range("A2"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2), Array(9, 9))
 
Upvote 0
Solution
You would usually do your "Set Rng" line more like this:

VBA Code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("$A2:$A" & LastRow)
 
Upvote 0
Alex, thanks. 99% of what I can thumb finger in VBA I've learned in this forum. I can stumble my way through some code on my own, but if I found something here that worked, I've just kept using it. Many things, like a loop for example, I understand the concept, but have no idea about the why or when. So I just stumble along, in an environment where the one eyed man is king. :) Copy/paste and adapt actually since ~ 1999 but I had to re-register, forget why now.

Every bit of help is deeply appreciated!

Ron
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,806
Members
449,337
Latest member
BBV123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top