.CSV file problems

Mel Smith

Well-known Member
Joined
Dec 13, 2005
Messages
1,023
Office Version
  1. 365
Platform
  1. Windows
Hello folks,

I'm trying to use data imported from a .csv file but I have encountered a couple of problems in how it has interpreted/displayed some information.

Please see below the screen shot from the original data:

1632649541734.png


and see also the way the .csv file has displayed it:

20210926-103551-bowling-st.csv
MN
1Best Bowling in an InningsBest Bowling in a Match
2Mar-35Apr-71
3Mar-77Apr-77
40-60-6
5Mar-26Mar-87
6Feb-403-103
70-210-21
8Mar-396-139
20210926-103551-bowling-st


Is there any way to overcome this? These examples are just a small selection of a large file.

Thanks,

Mel
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Using the import wizard (Ribbon > Data tab > From Text), for each column changing its data format from General to Text should give you the expected result ...

ScreenShot230.jpg
 
Upvote 0
Solution
Insert a new code module ( Insert and run VBA macros in Excel - step-by-step guide - Ablebits.com )
And run this code to fix it (click the play button or press F5 with the code window open). (But of course, ideally, you want to make sure that your .csv is formatted as text, not dates . . . to prevent this from happening again.
VBA Code:
Sub Fix_Data()

ActiveSheet.UsedRange.NumberFormat = "@"

For Each cell In ActiveSheet.UsedRange
    cell.Value = Month(ActiveCell) & "-" & Right(Year(ActiveCell), 2)
Next cell

End Sub
 
Upvote 0
Thank you, both, for your helpful replies. I've gone with "cmowla's" solution.

Mel
 
Upvote 0
I just realize that the code I wrote will wreck the data! I'm fixing it! (Sorry!)
 
Upvote 0
I tried it on dummy data and decided to then go with the previous solution. No harm done…

Mel
 
Upvote 0
Just for the sake of it, I wrote code which looks like it actually fixes the data. ("ActiveCell" in my previous code, in addition to it being fundamentally wrong? Clearly I was careless!)

VBA Code:
Sub Fix_Data()

For Each cell In ActiveSheet.UsedRange
    If MyIsDate(cell.Value) = True Then
        cell.NumberFormat = "@"
        cell.Value = Month(cell.Value) & "-" & Right(Year(cell.Value), 2)
    Else
        cell.NumberFormat = "@"
    End If
Next cell

End Sub

Sub Test__MyIsDate()
MsgBox MyIsDate(ActiveCell.Value)
End Sub
Function MyIsDate(str As String)
MyIsDate = InStr(Format(str, "@"), "/") > 0
End Function
 
Upvote 0
Thank you for this. I’ll try it out tomorrow and see which method is more suited (and fewer steps!).

Mel
 
Upvote 0
Thank you for this. I’ll try it out tomorrow and see which method is more suited (and fewer steps!).

Mel
It doesn't work perfectly for me because, for some reason, I can't format dates as MonthName-Day (Mar-26 = March 26th) from the number format menu. I just have MonthName-Year (Mar-21 = March 2021).

But if your version of Excel is set to another country's date format (not US English) indeed has that formatting (it must, given on the comparison with original data and what shows up in your spreadsheet), then it should work.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

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