TextToColumns for cells not containing spaces without overwriting cells already split before.

Kalahan

New Member
Joined
Oct 27, 2014
Messages
1
I wished I was better in VBA because I've been searching how to solve this problem since last friday. I'm hoping you guys can help me out or put me in the right direction.


Basically what I need is to find a way to Text-To-Columns, in a fixed range, but only for cells that contain spaces. I need to do this because the range on which TextToColumns is being executed is being filled up over time. If I don't check if a cell to be split contains a space the TextToColumns function will overwrite some of the cells that have been split before. if you just use TextToColumns cells that don't contain the character where the cell should be split still overwrite the cell next to it.
This didn't seemed so hard at first. I know how to to text-to-columns, and I know how to only execute a block of code when a cell contains a space. However putting 1 and 2 together is harder than it seems.
What I have now is this.


Code:
<code>For Each cell In Worksheets("Sheet1").range("B3:B100").Cells 
       
    'First check to see if one cell contains spaces. This seems to work.        
    Dim checkSpaces As Integer             
    checkSpaces = InStr(1, cell.Value, " ", vbTextCompare)         

    'Attempt to split cell by cell but can't because the TextToColumns function belongs to the range object and not to a cell.         
    If checkSpaces = 0 Then            
        cell.TextToColumns _                
            Destination:=range("B3"), _                
            DataType:=xlDelimited, _                
            Space:=True, _                
            Other:=False, _                
            OtherChar:="-"        
    Else        
    End If

Next
</code>


What's the best way to solve this? I thought it was easiest to get the range for each individual cell but it seems I can't get the range for a cell. I can get the column number and the row number of each cell but can't generate a range that can be used for TextToColumns.
I hope I'm being clear enough.


Already thanks for anyone that might have read this!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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