Macro to insert dates into cells based on values from another cells

Jibroni

New Member
Joined
Apr 7, 2022
Messages
25
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,

I need a macro that inserts dates into cells based on values from cells in another column.

Here's what my spreadsheet looks like:
CLASS_NOSTATUSCOURSEDOMAINSTART_DATE
06052022_GST_402
100​
11202VShared
07042022_GMT_401
100​
21201VShared
07062022_GMT_330
100​
3267​
Shared

I need to fill in the START_DATE column with start dates in format MM/DD/YYYY. The dates are based on the part of the string of numbers and letters from the CLASS_NO column that appear as a date. For example, in the first row you see "06052022_GST_402" and the first string of numbers are a date, June 5, 2022. The format for all cells in that column is such that the first eight numbers always form a date "MMDDYYYY" and are followed by some string for example "_" three letters "_" some more numbers.

I was able to come up with this macro:

Sub Insert_Saba_Start_Date()

string_date = Split("06052022_GST_402", "_")(0)

string_year = Right(string_date, 4)
string_day = Mid(string_date, 3, 2)
string_month = Left(string_date, 2)

date_date = DateValue(string_month & "/" & string_day & "/" & string_year)

Range("F2").Value = date_date

End Sub

However, this macro does only the specific value mentioned in my example. How do I create a macro that will create insert the date when the date string in CLASS_NO column is different every time?
 
Ok, how about
VBA Code:
Sub Jibroni()
   Range("E2:E" & Range("A" & Rows.Count).End(xlUp)).Formula = "=DATE(MID(A2,5,4),LEFT(A2,2),MID(A2,3,2))"
End Sub
In order for this to work, wouldnt the formula need to be different for each cell? This macro just puts the same value from A2 into every cell in column E. That's not what I want to do, because the value in every cell in column A will give a different date value. So I would have repeat the formula over and over, for example, A3 formula would need to be "=DATE(MID(A3,5,4),LEFT(A3,2),MID(A3,3,2))"
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
In order for this to work, wouldnt the formula need to be different for each cell? This macro just puts the same value from A2 into every cell in column E. That's not what I want to do, because the value in every cell in column A will give a different date value. So I would have repeat the formula over and over, for example, A3 formula would need to be "=DATE(MID(A3,5,4),LEFT(A3,2),MID(A3,3,2))"
Please disregard the post above. I got the answer from @Fluff and marked it above. I tried to delete the post above but it was past the time limit to edit. Thank you @Fluff !!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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