Change the lenght of the character

Venus_excel

Board Regular
Joined
Dec 18, 2013
Messages
74
Hi all,

Happy to see u again all.
Need your help here. Thanks in Advance.

here i need to change the character length in macro.
example

Column COutput(same in column c)
AB_1234AB_01234
AB1234AB_01234
AB01234AB_01234
AB_01234aAB_1234a
AB01234aAB_1234a
AB_012345AB_12345
AB 01234AB_01234

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

Hint :
1) _(underscore) should come at third character
2) Total character of output is 8 character
3) After _(underscore) if it is four character then 0 will come after underscore(_) and if it is five character then 0 wont come.


Please help me to sort out this.
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi Rick,

Compile error : Sub or Function Not defined. - on Substitute word
Sorry, I forgot you wanted a VBA solution instead of a formula one (I gave you a formula). It was not clear to me if this was a separate problem from your original request or not. If it is a separate request (the data you showed originally will not be intermingled with the data you showed in Message #9), then you can use this macro...

Code:
Sub FixUnderscore2()
  Dim X As Long, vArr As Variant
  vArr = Range("C1", Cells(Rows.Count, "C").End(xlUp))
  For X = 1 To UBound(vArr)
    If InStr(vArr(X, 1), ".") Then
      vArr(X, 1) = Left(vArr(X, 1), 2) & "_" & Left("00000", 8 - InStr(Replace(vArr(X, _
                   1), "_", "") & ".", ".")) & Mid(Replace(vArr(X, 1), "_", ""), 3)
    End If
  Next
  Range("C1", Cells(Rows.Count, "C").End(xlUp)) = vArr
End Sub

If this is an extension of your first request (the original data will be intermingled with the data you show in Message #9), then you can use this macro for it...

Code:
Sub FixUnderscore()
  Dim X As Long, vArr As Variant
  vArr = Range("C1", Cells(Rows.Count, "C").End(xlUp))
  For X = 1 To UBound(vArr)
    If InStr(vArr(X, 1), ".") Then
      vArr(X, 1) = Left(vArr(X, 1), 2) & "_" & Left("00000", 8 - InStr(Replace(vArr(X, _
                   1), "_", "") & ".", ".")) & Mid(Replace(vArr(X, 1), "_", ""), 3)
    ElseIf Len(vArr(X, 1)) > 2 Then
      vArr(X, 1) = Left(vArr(X, 1), InStr(vArr(X, 1) & ".", ".") - 1)
      vArr(X, 1) = Left(vArr(X, 1), InStr(4, vArr(X, 1) & "  ", " ") - 1)
      vArr(X, 1) = Replace(vArr(X, 1), " ", "")
      vArr(X, 1) = Left(vArr(X, 1), 2) & "_" & Right("00000" & Mid(Replace(vArr(X, 1), "_", ""), 3, 9), 5)
    End If
  Next
  Range("C1", Cells(Rows.Count, "C").End(xlUp)) = vArr
End Sub

Just so you know why I was confused, your original request eliminated text after the space whereas your new request keeps text after the space.
 
Upvote 0
Hi Rick,

Again getting the below error:

Run-time error '5':
Invalid procedure call or arguments

on the below line

vArr(X, 1) = Left(vArr(X, 1), 2) & "_" & Left("00000", 8 - InStr(Replace(vArr(X, _
1), "_", "") & ".", ".")) & Mid(Replace(vArr(X, 1), "_", ""), 3)
 
Upvote 0
Hi Rick,

Again getting the below error:

Run-time error '5':
Invalid procedure call or arguments

on the below line

vArr(X, 1) = Left(vArr(X, 1), 2) & "_" & Left("00000", 8 - InStr(Replace(vArr(X, _
1), "_", "") & ".", ".")) & Mid(Replace(vArr(X, 1), "_", ""), 3)
Does the MessageBox with that error give you the opportunity to go into Debug mode? If yes, what value is in vArr(x, 1) when the error occurs?
 
Upvote 0
The Value is 7
In and of itself, that value should not be causing a problem. Can you send me a copy of your workbook so that I can watch the function behave on your actual data rather than my assumption of what is in your cells? Please mention the thread title when you do so I can relate your message back to this thread. My email address is...

rick DOT news AT verizon DOT net
 
Upvote 0
Hi Rick,

This are the possiblities.

AB_1234.1
AB 1234.1
AB_01234b.1
AB_01234
AB_1234_111
AB1234
AB1234.1
AB__1234.2
AB_012345.1
AB_1234 XXX 1 (UK70, UK72, UK71)
AB_12345.1 XXX 2 (UK76)
AB_12345_XXX4
AB_01234
AB1234.0
AB12345

<tbody>
</tbody>


I think it is litle bit complex. But help me in this hard situation
 
Last edited:
Upvote 0
Hi Rick,

This are the possiblities.

AB_1234.1
AB 1234.1
AB_01234b.1
AB_01234
AB_1234_111
AB1234
AB1234.1
AB__1234.2
AB_012345.1
AB_1234 XXX 1 (UK70, UK72, UK71)
AB_12345.1 XXX 2 (UK76)
AB_12345_XXX4
AB_01234
AB1234.0
AB12345

<tbody>
</tbody>


I think it is litle bit complex. But help me in this hard situation
None of those values would ever make V(x,1) in my code be a 7 like you said you got when the error popped up. Are you sure you cannot send the workbook to me so I can watch what is happening as it happens? It would cut a lot of back-and-forth between us as I tried get you to tell me all the things I would be looking for when debugging the code. If you have sensitive information you are worried about, just delete it as my code only processes Column C and doesn't look at anything else.

Also, could you list what you want my code to return to each of those values (it looks like you have mixed data from your two requests)?
 
Upvote 0
maybe ?

Code:
Function F_snb(c00)
   F_snb = Replace(Left(c00, 2) & "_" & Replace(Replace(Mid(c00, 3), ".", ""), " ", ""), "__", "_")
   If Len(F_snb) > 8 Then F_snb = Left(Replace(F_snb, "_0", "_"), 8)
End Function
 
Upvote 0
Hi Rick,

Sorry to say this. i cant share sheet as it is very confidential.

The valus of V(x,1) is AB_01234b.1

let me know if u need more details.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,618
Members
449,238
Latest member
wcbyers

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