VBA - Text to Columns

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
256
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have some code which works fine, but I now want to add some coding to utilise the Text to Columns function.
I need column B to be fixed width of 5 characters and the data format to be set as 'Date' (UK date though not US).

Please can someone help?

Cheers

Chris
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If you turn on the Macro Recorder, and record yourself performing those steps manually, you will have recorded the VBA code that you require!
So then you can copy/paste it into your other VBA code.
 
Upvote 0
If you turn on the Macro Recorder, and record yourself performing those steps manually, you will have recorded the VBA code that you require!
So then you can copy/paste it into your other VBA code.

Ah yes, I hadn't thought of that. Feel a little bit silly now!
 
Upvote 0
No worries!

It is still early in the morning here. I usually blame a lack of caffeine in the system!
;)
 
Upvote 0
Hmm, that doesnt actually seem to have worked as I had hoped.

In column B there are dates, all of them are from October but the years can vary. When I manually use the Text to Columns function it works fine, but when I use it with the code I've copied and pasted not all the dates pull through correctly. Here is a link to my One Drive that show screen shots. If any one could help I'd appreciate it. https://1drv.ms/u/s!AvMWaXTcbDBtlF8sQtXWvC0h2wbN?e=9oEDWh

The code I'm using is;

Code:
    Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 4), Array(5, 1)), TrailingMinusNumbers:=True

Cheers
 
Upvote 0
Unfortunately, I am unable to view/download external links form my current location (work security prevents it).
Are you sure that you chose the proper date format in working through Text to Columns? Which one did you choose?

If you could post a few examples directly in your post, showing some that worked, and some that didn't, that would be a big help.

If you like pretty images, there are tools you can use to post screen images. They are listed in Section B of this link here:
http://www.mrexcel.com/forum/board-a...forum-use.html.
Also, there is a Test Here forum on this board that you can use to test out these tools to make sure they are working correctly before using them in your question.
 
Upvote 0
Yes I am definitely using the Text to Columns correctly. I get the correct result when doing it manually, but in VBA it brings back some errors.

But, I think I know what the problem is (just don't have a resolution) - when VBA is reading the date it is doing so in US format, not UK. So when it then when it presents the new data that is in US format. I need some how for VBA to read it in UK format.

Any ideas?
 
Upvote 0
If you post some examples of the data, and what the results should look like, maybe we can come up with some alternative VBA methods to di what you want (I will do my best, but since I do not have the UK version, I won't be able to verify that it will work exactly the same way for you).
 
Upvote 0
I am struggling to post some examples other than the screenshots I've used in the previous link. My workplace has some restrictions so I can not download the HTML maker etc.

However, I do think I may have found a way round it (albeit a long winded way).

I want to use the below code to set the dates I have to US format but as text, then let the Text to Columns code run and then convert it back to UK format.

Code:
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""mm/dd/yyyy"")"
    
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C" & Lastrow), Type:=xlFillDefault

I am however having a problem when I'm trying to autofill the formula. I guess it's something very simple, but how do I autofill the formula in cell C2 down to the last row?
 
Upvote 0
Ignore my last post, I've managed to work it out using;

Code:
ActiveCell.AutoFill Range(ActiveCell, ActiveCell.Offset(0, -1).End(xlDown).Offset(0, 1))
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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