vba code require to separate date from code

VBABEGINER

Well-known Member
Joined
Jun 15, 2011
Messages
1,232
Hi All,

I need a vba code, where I can split range value in code and date.

This are the values in Col A.
CGU 002 - 05/89
ECG 21 501 - 05/00
ECG 21 762 - 04/14
CG 21 73 - 01/00
CG 21 06 - 05/14
CG 21 35 - 10/01
CG 21 47 - 12/07
CG 21 54 - 01/96
CG 21 55 - 09/99
CG 21 86 - 12/04
CG 21 96 - 03/05
CG 22 34 - 04/13
CG 22 43 - 04/13
CGU 002 - 05/89
ECG 21 501 - 05/00

<colgroup><col></colgroup><tbody>
</tbody>

<colgroup><col></colgroup><tbody>
</tbody>

Here, "05/89" these are dates. I want, "CGU 002" these in A range and "05/89" these in B Col with addition of "05/01/89"..

Can some one pls help..
 
some words for you...I really don't understand, how you all so high level of Experts..!!! How come you understand this are form no and date's...
There are large parts of Excel that I still am not familiar with, so I am not sure calling me and "expert" is entirely correct; at least not in the same way I consider my fellow volunteers here to be experts, but I do thank you for the kind words. As for what I do know, which is programming, that is a skill I have picked up over time... I have been programming since 1981, so I have had a lot of years to acquire the programming knowledge that I now possess.



Sir, Final one...Pls explain me how this is working...line by line pls...I really not understand...
Below is my code with extensive commenting added to help you understand what the code is doing. What I did to produce the original code I posted was to try and understand what your needs were in general. It appeared to me that the last four digits in a cell's text were the month and year for the date you wanted and that the lead-in text that you wanted always appeared to end with the last digit in the cell's text that occurred before the four digits that made up the date. That is the overall structure behind what my code is doing. Here is the commented code...
Code:
Sub SplitCodeDate()
  Dim R As Long, X As Long, Cnt As Long, Data As Variant
[B][COLOR="#008000"]  ' Put all the data into an array so that the code does not keep
  ' asking the worksheet for data (arrays are much faster than
  ' continually referencing cells on the worksheet directly). Since
  ' there will be two columns of data outputted, I resize the single
  ' column of data to two columns in the array in order to output
  ' the correct size data at the end.
[/COLOR][/B]  Data = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resize(, 2)
[B][COLOR="#008000"]  ' Examing the value in each cell
[/COLOR][/B]  For R = 1 To UBound(Data)
[B][COLOR="#008000"]    ' Cnt is used to count digits in a cell's value, so we want
    ' it set to zero before each individual cell is examined.
[/COLOR][/B]    Cnt = 0
[B][COLOR="#008000"]    ' While I assume Column B will be blank when I resized the range
    ' that I copied into the Data array originally, here I am making
    ' sure I did not accidentally pick up a value.
[/COLOR][/B]    Data(R, 2) = ""
[B][COLOR="#008000"]    ' This loop starts at the end of the text from the cell's value
    ' being examined and iterates toward the beginning of the text.
[/COLOR][/B]    For X = Len(Data(R, 1)) To 1 Step -1
[B][COLOR="#008000"]      ' If the digit count is less than 4 (remember, Cnt starts at 0),
      ' then we are still building the date value.
[/COLOR][/B]      If Cnt < 4 Then
[B][COLOR="#008000"]        ' If the character being examined is a digit, then concatenate
        ' it into the second dimension of the Data array.
[/COLOR][/B]        If IsNumeric(Mid(Data(R, 1), X, 1)) Then
[B][COLOR="#008000"]          ' Because we found a digit, increase the digit counter by one.
[/COLOR][/B]          Cnt = Cnt + 1
[B][COLOR="#008000"]          ' Concatenate the digit on to the front of the digits that
          ' have already been found (remember, we are iterating backwards).
[/COLOR][/B]          Data(R, 2) = Mid(Data(R, 1), X, 1) & Data(R, 2)
        End If
[B][COLOR="#008000"]        ' Once Cnt is equal to 4, that means we have found all of the digits
        ' that make up the date value.
[/COLOR][/B]      ElseIf Cnt = 4 Then
[B][COLOR="#008000"]        ' We use VB's Format function to add the first day of the month to
        ' the date and place the slashes that the US date uses as its delimiter
        ' which is stored in the second dimension of the Data array.
[/COLOR][/B]        Data(R, 2) = Format(Data(R, 2), "@@/01/@@")
[B][COLOR="#008000"]        ' Incase there are no more digits in the text, all the remaining text
        ' is placed in the first dimension of the Data array.
[/COLOR][/B]        Data(R, 1) = Left(Data(R, 1), X)
[B][COLOR="#008000"]        ' Since Cnt will no longer be increased, we set it to a number so that
        ' the If and ElseIf statements above will not be triggered, that way
        ' the ElseIf statement below will handle the rest of the characters
        ' being examined from the cell's text.
[/COLOR][/B]        Cnt = 5
[B][COLOR="#008000"]      ' As soon as we come across a digit, we are done searching
[/COLOR][/B]      ElseIf IsNumeric(Mid(Data(R, 1), X, 1)) Then
[B][COLOR="#008000"]        ' All the text from the beginning of the text to the digit that was
        ' just found are assigned to the first dimension of the Data array.
[/COLOR][/B]        Data(R, 1) = Left(Data(R, 1), X)
[B][COLOR="#008000"]        ' Since we are done searching the cell's text, we exit this inner
        ' loop so that we can move on to the next cell's text.
[/COLOR][/B]        Exit For
      End If
    Next
  Next
[B][COLOR="#008000"]  ' Now that all of the text in all of the cells of Column A have be processed,
  ' then first we format the cells in Column D as a Date
[/COLOR][/B]  Range("D1").Resize(UBound(Data)).NumberFormat = "mm/dd/yyyy"
[B][COLOR="#008000"]  ' then we place all of the data in Columns C and D (Column C gets the code,
  ' and Column D gets the date).
[/COLOR][/B]  Range("C1").Resize(UBound(Data), 2) = Data
End Sub
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Rick Sir...Thank so much for the explanation. These helps me, but having one problem..
actually my form numbers are start from B3 range..so i have modified this way...but output comes on C4...wherein, C4 = heading name comes and C5 onwards, output comes..

Can you please fix me this..B3 I am copying the data...and output will on C3..form number's and D3 Dates..


There are large parts of Excel that I still am not familiar with, so I am not sure calling me and "expert" is entirely correct; at least not in the same way I consider my fellow volunteers here to be experts, but I do thank you for the kind words. As for what I do know, which is programming, that is a skill I have picked up over time... I have been programming since 1981, so I have had a lot of years to acquire the programming knowledge that I now possess.




Below is my code with extensive commenting added to help you understand what the code is doing. What I did to produce the original code I posted was to try and understand what your needs were in general. It appeared to me that the last four digits in a cell's text were the month and year for the date you wanted and that the lead-in text that you wanted always appeared to end with the last digit in the cell's text that occurred before the four digits that made up the date. That is the overall structure behind what my code is doing. Here is the commented code...
 
Upvote 0
Can you please fix me this..B3 I am copying the data...and output will on C3..form number's and D3 Dates..
Give this a try...
Code:
Sub SplitCodeDate()
  Dim R As Long, X As Long, Cnt As Long, Data As Variant
  ' Put all the data into an array so that the code does not keep
  ' asking the worksheet for data (arrays are much faster than
  ' continually referencing cells on the worksheet directly). Since
  ' there will be two columns of data outputted, I resize the single
  ' column of data to two columns in the array in order to output
  ' the correct size data at the end.
  Data = Range("[B][COLOR="#FF0000"]B3[/COLOR][/B]", Cells(Rows.Count, "[B][COLOR="#FF0000"]B[/COLOR][/B]").End(xlUp)).Resize(, 2)
  ' Examing the value in each cell
  For R = 1 To UBound(Data)
    ' Cnt is used to count digits in a cell's value, so we want
    ' it set to zero before each individual cell is examined.
    Cnt = 0
    ' While I assume Column C will be blank when I resized the range
    ' that I copied into the Data array originally, here I am making
    ' sure I did not accidentally pick up a value.
    Data(R, 2) = ""
    ' This loop starts at the end of the text from the cell's value
    ' being examined and iterates toward the beginning of the text.
    For X = Len(Data(R, 1)) To 1 Step -1
      ' If the digit count is less than 4 (remember, Cnt starts at 0),
      ' then we are still building the date value.
      If Cnt < 4 Then
        ' If the character being examined is a digit, then concatenate
        ' it into the second dimension of the Data array.
        If IsNumeric(Mid(Data(R, 1), X, 1)) Then
          ' Because we found a digit, increase the digit counter by one.
          Cnt = Cnt + 1
          ' Concatenate the digit on to the front of the digits that
          ' have already been found (remember, we are iterating backwards).
          Data(R, 2) = Mid(Data(R, 1), X, 1) & Data(R, 2)
        End If
        ' Once Cnt is equal to 4, that means we have found all of the digits
        ' that make up the date value.
      ElseIf Cnt = 4 Then
        ' We use VB's Format function to add the first day of the month to
        ' the date and place the slashes that the US date uses as its delimiter
        ' which is stored in the second dimension of the Data array.
        Data(R, 2) = Format(Data(R, 2), "@@/01/@@")
        ' Incase there are no more digits in the text, all the remaining text
        ' is placed in the first dimension of the Data array.
        Data(R, 1) = Left(Data(R, 1), X)
        ' Since Cnt will no longer be increased, we set it to a number so that
        ' the If and ElseIf statements above will not be triggered, that way
        ' the ElseIf statement below will handle the rest of the characters
        ' being examined from the cell's text.
        Cnt = 5
      ' As soon as we come across a digit, we are done searching
      ElseIf IsNumeric(Mid(Data(R, 1), X, 1)) Then
        ' All the text from the beginning of the text to the digit that was
        ' just found are assigned to the first dimension of the Data array.
        Data(R, 1) = Left(Data(R, 1), X)
        ' Since we are done searching the cell's text, we exit this inner
        ' loop so that we can move on to the next cell's text.
        Exit For
      End If
    Next
  Next
  ' Now that all of the text in all of the cells of Column B have be processed,
  ' then first we format the cells in Column D as a Date
  Range("[B][COLOR="#FF0000"]D3[/COLOR][/B]").Resize(UBound(Data)).NumberFormat = "mm/dd/yyyy"
  ' then we place all of the data in Columns C and D (Column C gets the code,
  ' and Column D gets the date).
  Range("[B][COLOR="#FF0000"]C3[/COLOR][/B]").Resize(UBound(Data), 2) = Data
End Sub
 
Last edited:
Upvote 0
Rick Sir....One more mistake I have done..Thank I observed now only..

For this form number type.."LIA-7139 Ed. 01-09"......output should come "LIA-7139 Ed."......it coming now as ..."LIA-7139"..

can we please fix this..

Hi Rick Sir...Thank so much for the explanation. These helps me, but having one problem..
actually my form numbers are start from B3 range..so i have modified this way...but output comes on C4...wherein, C4 = heading name comes and C5 onwards, output comes..

Can you please fix me this..B3 I am copying the data...and output will on C3..form number's and D3 Dates..


There are large parts of Excel that I still am not familiar with, so I am not sure calling me and "expert" is entirely correct; at least not in the same way I consider my fellow volunteers here to be experts, but I do thank you for the kind words. As for what I do know, which is programming, that is a skill I have picked up over time... I have been programming since 1981, so I have had a lot of years to acquire the programming knowledge that I now possess.




Below is my code with extensive commenting added to help you understand what the code is doing. What I did to produce the original code I posted was to try and understand what your needs were in general. It appeared to me that the last four digits in a cell's text were the month and year for the date you wanted and that the lead-in text that you wanted always appeared to end with the last digit in the cell's text that occurred before the four digits that made up the date. That is the overall structure behind what my code is doing. Here is the commented code...
 
Upvote 0
Rick Sir....One more mistake I have done..Thank I observed now only..

For this form number type.."LIA-7139 Ed. 01-09"......output should come "LIA-7139 Ed."......it coming now as ..."LIA-7139"..

can we please fix this..

Hi Rick Sir...Thank so much for the explanation. These helps me, but having one problem..
actually my form numbers are start from B3 range..so i have modified this way...but output comes on C4...wherein, C4 = heading name comes and C5 onwards, output comes..

Can you please fix me this..B3 I am copying the data...and output will on C3..form number's and D3 Dates..
 
Upvote 0
Rick Sir....One more mistake I have done..Thank I observed now only..

For this form number type.."LIA-7139 Ed. 01-09"......output should come "LIA-7139 Ed."......it coming now as ..."LIA-7139"..

can we please fix this..
In your original post, you indicated that the dash was what identified the split point in the data... and that identifier was not kept after the split. Then in Message #6 you said for the example in the above quoted section that "Ed." was the identifier... since you did not keep the dash when it was the identifier, I assumed "Ed." should not be kept because it was the identifier. That left the split point being the first number to the left of the date which is how I coded my macro. Now you appear to be saying you want all the text to the left of the date's digits EXCEPT if that text ends with a space followed by a dash followed by another space. Before I change my code to do this, are there any other characters besides the dash (like, perhaps, a colon, semi-colon, slash, etc.) which should not be kept after the split?
 
Upvote 0
Agree. Sir..That was totally my mistake about "* Ed. *"...
TO make complete the task asap, I forgot that Ed. is missing in Output.
As you said, only now I require "Ed." to be remain in Form number...Rest all perfect. Till now I received 3000 form codes...wherein all is perfectly fine..In future if come something...I am sure confident that your code will work best in that too...

And, Sincerely Thankfull, for keeping me in loop in every post...I will get chance to learn this..

In your original post, you indicated that the dash was what identified the split point in the data... and that identifier was not kept after the split. Then in Message #6 you said for the example in the above quoted section that "Ed." was the identifier... since you did not keep the dash when it was the identifier, I assumed "Ed." should not be kept because it was the identifier. That left the split point being the first number to the left of the date which is how I coded my macro. Now you appear to be saying you want all the text to the left of the date's digits EXCEPT if that text ends with a space followed by a dash followed by another space. Before I change my code to do this, are there any other characters besides the dash (like, perhaps, a colon, semi-colon, slash, etc.) which should not be kept after the split?
 
Upvote 0
Sir, can we do some for that " Ed. "....

In your original post, you indicated that the dash was what identified the split point in the data... and that identifier was not kept after the split. Then in Message #6 you said for the example in the above quoted section that "Ed." was the identifier... since you did not keep the dash when it was the identifier, I assumed "Ed." should not be kept because it was the identifier. That left the split point being the first number to the left of the date which is how I coded my macro. Now you appear to be saying you want all the text to the left of the date's digits EXCEPT if that text ends with a space followed by a dash followed by another space. Before I change my code to do this, are there any other characters besides the dash (like, perhaps, a colon, semi-colon, slash, etc.) which should not be kept after the split?
 
Upvote 0
Sir, can we do some for that " Ed. "....

You did not answer the question I asked at the end of my message which you quoted. To repeat it...

"Now you appear to be saying you want all the text to the left of the date's digits EXCEPT if that text ends with a space followed by a dash followed by another space. Before I change my code to do this, are there any other characters besides the dash (like, perhaps, a colon, semi-colon, slash, etc.) which should not be kept after the split?"
 
Upvote 0
If I understand, your question correctly, then answer is "No". Just for your reference also, im posting some examples..

Out of 3000 form numbers these are the unique, I find. And I dont really believe, apart to this new format will come...
CGU 002 - 05/89
ECG 20 5061 - 04/02
CG 00 01 - 04/13
ILU 003 - 05/89
ECG 25 511 - 12/05
LIA-7139 Ed. 01-09
ACF-7006 Ed. 05-11
CRI-19076 Ed. 01-16
ACF-7007 08/11
ILT8011093
MM99561013
IL T8 01 10 93
IL T0 02 11 89

and if so come, then I surely ping you...please..

You did not answer the question I asked at the end of my message which you quoted. To repeat it...

"Now you appear to be saying you want all the text to the left of the date's digits EXCEPT if that text ends with a space followed by a dash followed by another space. Before I change my code to do this, are there any other characters besides the dash (like, perhaps, a colon, semi-colon, slash, etc.) which should not be kept after the split?"
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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