Recorded macro does not execute on full range?

danieldk

New Member
Joined
Aug 4, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have been struggling with this error for weeks.

I have an Excel document where I copy a generated data workbook into a new workbook where i paste it into a dedicated DATA tab.

From here I have recorded a macro that selects column B and replaces every instance of "." with "/". This is to change dd.mm.yyyy to dd/mm/yyyy. I have tried simply changing the format but it does not work. The goal of doing this is to have Excel recognize the cell value as a date and compiling the dates when i put a filter on the header. Somehow when i do this manually, or even when recording the macro, it works perfectly, but when i run the macro, it seems to only replace half (or less) of the cells, and it changes cell values to date that have not yet been such as 06/12/2022.

I have tried debugging, I have compared the lines in VBA to other examples found on google but i cant find what the error is.

Can anyone help me?


VBA also posted beneath
Sub UPDATE_DATA()
'
' UPDATE_DATA Macro
'

'
Columns("B:B").Select
Selection.Replace What:=".", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
 

Attachments

  • 2022-08-04_10-48-43.jpg
    2022-08-04_10-48-43.jpg
    106.7 KB · Views: 5
  • 2022-08-04_10-49-35.jpg
    2022-08-04_10-49-35.jpg
    112.2 KB · Views: 5
  • 2022-08-04_10-58-50.jpg
    2022-08-04_10-58-50.jpg
    103.7 KB · Views: 6
  • 2022-08-04_11-00-10.jpg
    2022-08-04_11-00-10.jpg
    38.5 KB · Views: 5

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi, it's probably something to do with the way VBA is expecting dates to be in the US format of mm/dd/yyyy - you could try this alternative method of converting the text dates to proper dates.

VBA Code:
Sub UPDATE_DATA()
'
' UPDATE_DATA Macro
'
    Columns("B:B").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 4), TrailingMinusNumbers:=True
End Sub
 
Upvote 0
You sir, are an Excel wizard! It works!

Your title of MrExcel MVP is earned :D
 
Upvote 0
1659605563615.png



It seems i replied to quickly. The date format is now correct in all cells but it still changes the some cells to dates that have not been yet?
 
Upvote 0
It changes dd.mm.yyyy to mm/dd/yyyy if im not wrong. So the code correctly made Excel recognize the values as dates, but also converted the value to a new format. In order to have this work together with the rest of the workbook, it would be a great help if it could remain, dd/mm/yyyy format.

Thanks for you willingness to help, its much appreciated!!

Have taken screenshot of the values that gets changed to December

12.01.2022 becomes 01/12/2022
12.05.2022 becomes 05/12/2022
12.07.2022 becomes 07/12/2022
 
Upvote 0
It might be best to do them one by one.

Here's something you can try, it assumes you only have the text dates in column B and that they start on row 2.

VBA Code:
Sub UPDATE_DATA()
'
' UPDATE_DATA Macro
'
Dim V As Variant, I As Long, D As Variant
V = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For I = 1 To UBound(V)
    D = Split(V(I, 1), ".")
    V(I, 1) = DateSerial(D(2), D(1), D(0))
Next I
Range("B2").Resize(UBound(V)).Value = V
End Sub
 
Upvote 0
Solution
Just tested it, and now everything works! :)

Thanks for you help!!
 

Attachments

  • 2022-08-05_06-13-48.jpg
    2022-08-05_06-13-48.jpg
    110.3 KB · Views: 1
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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