VBA Bug TextToColumns in Date

rodrigo_m_almeida

New Member
Joined
Jan 13, 2022
Messages
42
Office Version
  1. 2021
Platform
  1. Windows
Good Morning,

I'm trying to extract only the days from this date string

This code

SQL:
        Range("Query01[Data]").Select
        Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _
        TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="/", FieldInfo:=Array(Array(1, 2), Array(2, 9), Array(3, 9)), _
        TrailingMinusNumbers:=True

It's just that I'm not getting it, he keeps repeating the dates

antes.PNG
depois.PNG


Does anyone have any ideas ?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If it is a valid date entry (and not text), there really aren't slashes in the date - that is just in the format applied to the cell.
Dates in Excel are stored as numbers, specifically the number of days since 1/0/1900 (to see this, just change the format of the cell to General).

To get the Day value from a date, just use the DAY function, i.e.
=DAY(A2)
 
Upvote 0
If it is a valid date entry (and not text), there really aren't slashes in the date - that is just in the format applied to the cell.
Dates in Excel are stored as numbers, specifically the number of days since 1/0/1900 (to see this, just change the format of the cell to General).

To get the Day value from a date, just use the DAY function, i.e.
=DAY(A2)
How can I use this function in vba to get the days of all cells in that column ?
 
Upvote 0
The DAY function works in VBA too.

What do you mean to get the days of all cells in that column?
Where do you want this value to show?
In the next column over (column B), or somewhere else?
 
Upvote 0
The DAY function works in VBA too.

What do you mean to get the days of all cells in that column?
Where do you want this value to show?
In the next column over (column B), or somewhere else?
Extract days and show in the respective cell
 
Upvote 0
So, you want to overwrite the date with the day?

This VBA code should do that:
VBA Code:
Sub MyReplaceCode()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows starting on row 2
    For r = 2 To lr
'       Replace date entry in column A with the Day
        Cells(r, "A") = Day(Cells(r, "A"))
    Next r
    
'   Change the format of column A to General
    Range("A:A").NumberFormat = "General"
        
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
If Joe4 is correct in what you want, that can be done without using a loop as well...
VBA Code:
Sub ReplaceDatesWithDayNumbers()
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate("DAY(" & .Address & ")")
    .NumberFormat = "General"
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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