Hyphen to cell vba

Snabelhund

New Member
Joined
Nov 11, 2021
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi im Trying to write a simple part in a bigger macro that will convert a set of numbers and add a hyphen between number 8 and 9 see below for examples

The original numbers are fomatted as 000000000000
What i want to achieve i a format that looks like this: 000000-0000

I have a solution that works partly, but since some of the orignal numbers in the data set are formatted/contains both numbers and text it does seem to skip adding hyphen to theese cells.

As an example the original cell formatting can also look like 00000000AA00 instead of 000000000000 as described before . The outcome i´m looking for i that all cells are formatted as 000000-0000 or 000000-AA00 which im having trouble to achieve with my code.

Ill post the part that does the formatting below, (as mentioned it is part of a bigger module/ process)

VBA Code:
Sub Workbook_Open()
    Dim rngh As Range, cellh As Range
    Dim myString As String
    Dim Cell As Range
    Dim wb As Workbook
    Set rngh = Range("B2:B5000")
    For Each cellh In rngh
        If Not IsEmpty(cellh) Then
            myString = cellh.Text
            myString = Right(myString, Len(myString) - 2)
            cellh.Value = myString
        End If
    Next cellh
    On Error GoTo NoFilledCells
  For Each Cell In Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row).SpecialCells(xlCellTypeConstants)
    Cell.Value = Format(Replace(Cell.Value, "-", ""), "000000-0000")
  Next
NoFilledCells:



Cheers and sorry for any spelling errors (from a non english speaking country)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
If you just want to insert a dash ('-') after the sixth character from the left then how about:

replace:
VBA Code:
    Cell.Value = Format(Replace(Cell.Value, "-", ""), "000000-0000")

with:
VBA Code:
    Cell.Value = Left(Cell.Value, 6) & "-" & Mid(Cell.Value, 7, Len(Cell.Value) - 1)
 
Upvote 0
Solution
Hi, I sense some inconsistency in your explanation...

add a hyphen between number 8 and 9 see below for examples
In all your examples the hyphen apears to be at the 7th position (which is between 6 and 7).

The original numbers are fomatted as 000000000000
What i want to achieve i a format that looks like this: 000000-0000
This suggests that the last two characters may be dropped. Is that right?
 
Upvote 0
@GWteB there are a few inconsistencies in the OP, but to answer your question about dropping characters, code in the OP removes the first two characters, If I read it correctly.
 
Upvote 0
You´re right apoligies for the inconsistency..
@ johnnyL Exactly the two first characters should be dropped and the dash should be added between position 6 and . Ill try solution provided and report back.

Again sorry for the somewhat inconsistent question and thanks for the replies
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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