Risky macros - how to streamline and improve

Adendum

New Member
Joined
Feb 15, 2021
Messages
24
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,
I have 3 individual macros that I have been using but realise that sometimes these can be problematic.

What I need to do is after importing 200-300 rows of data is to move the last part of the text (Added dd mmm yyyy) from column A into column B. Ideally I would like to have the data in column B to be actual dates so instead of "Added 17 Jan 2021" it becomes 17/01/2021 (so I can later sort on column B). The problem I have found is that the imported data isn't always 17 characters - so "Added 5 Jan 2021" is only 16 whereas "Added 21 Jan 2021" is 17. So I am stumped!

For my education I would prefer to see examples that break down the processes. Any ideas?


VBA Code:
Sub B_CopyAdded()
  For Each cell In Range("A2:A2000").Cells
    cell.Offset(0, 1).Value = Right(cell.Value, 17)
  Next cell
  Columns("A:A").Select
     
End Sub
Sub C_RemoveAdded()
' Deletes last 17 characters
' Defines variables
On Error Resume Next
    For Each cel In Range("A2:A2000")
        myVal = cel.Value
        cel.Value = Left(myVal, Len(myVal) - 17)
    Next cel
End Sub
Sub D_TrimCells()
Dim Cl As Range
   For Each Cl In Range("B2:B2000", Range("A" & Rows.Count).End(xlUp))
      Cl.Value = Trim(Cl.Value)
   Next Cl
End Sub
 
Sounds like you have some odd characters in there, how about
VBA Code:
Sub Adendum()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Offset(, 1).Value = Evaluate(Replace("if({1},trim(mid(substitute(@,char(160),"" ""),search(""added"",@)+6,11)))", "@", .Address))
      .Value = Evaluate(Replace("if({1},trim(left(@,search(""added"",@&""added"")-1)))", "@", .Address))
   End With
End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Sounds like you have some odd characters in there, how about
VBA Code:
Sub Adendum()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Offset(, 1).Value = Evaluate(Replace("if({1},trim(mid(substitute(@,char(160),"" ""),search(""added"",@)+6,11)))", "@", .Address))
      .Value = Evaluate(Replace("if({1},trim(left(@,search(""added"",@&""added"")-1)))", "@", .Address))
   End With
End Sub
Hi Fluff,

I suspected something like that so I did dump my incoming data into Notepad so that I just had pure text but the same issue was occurring.

However, your last tweak has done the job! So, once again, many thanks.

:)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Fluff,

A new discovery that is baffling me (as much does!!!).....

Most days we process data from the previous day, so today for example we process those entries with "Added 13 Apr 2021" so when we run the macro all is good. Similarly if we are working on the day when we have data showing "Added 7 Apr 2021" the macro works perfectly.

But sometimes we gather data that is a mix, like we did today, and when trying to process "Added 7 Apr 2021" and similar single date digits with those with double digit dates , e.g. "Added 17 Apr 2021" the macro fails.

When I say 'fail' I mean we get a mixed date style in the date column - all the double digit dates work properly and we get 17-Apr-2021 in the cells but the single digit values become text, so we get "7 Apr 2021".

So to be clear - the macro works if the incoming data is all single digit dates; it also works if all the incoming data is double digit dates. Mix the two and it breaks.

Can you resolve that?

:)
 
Upvote 0
Works quite happily for me.
+Fluff 1.xlsm
AB
1
2Fred Flintstone Added 9 Mar 2021 1 day overdue09/03/2021
3Barney Rubble Added 19 Mar 2021 1 day overdue19/03/2021
4Wilma Flintstone Added 20 Mar 2021 1 day overdue20/03/2021
5Betty Rubble Added 09 Mar 2021 1 day overdue09/03/2021
Main
 
Upvote 0
Works quite happily for me.
+Fluff 1.xlsm
AB
1
2Fred Flintstone Added 9 Mar 2021 1 day overdue09/03/2021
3Barney Rubble Added 19 Mar 2021 1 day overdue19/03/2021
4Wilma Flintstone Added 20 Mar 2021 1 day overdue20/03/2021
5Betty Rubble Added 09 Mar 2021 1 day overdue09/03/2021
Main

Fluff,

And it does for me too - when I use the data I supplied here. But when we take the real incoming data it isn't working. So my guess is that there is something (code or hidden characters) that are affecting this process. The data comes from a web page, so there is a high possibility that 'crap' is getting in there. Although the copy process we use is to select the '123' option when pasting.

I can test the theory by pasting to Notepad first to see if that strips out garbage so I'll go away and do some more tests and will report back!

:(
 
Upvote 0

Forum statistics

Threads
1,214,419
Messages
6,119,389
Members
448,891
Latest member
tpierce

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