• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

formatting cells|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / formatting cells|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & Macrosformatting cells
sp_PrintTopic sp_TopicIcon
formatting cells
Avatar
Amy 73
Member
Members
Level 0
Forum Posts: 39
Member Since:
February 11, 2015
sp_UserOfflineSmall Offline
1
February 19, 2020 - 9:20 am
sp_Permalink sp_Print

Hello. I need help please. I have an excel spreadsheet we get from our customer.  Column A has 5 digit number, example 12345.  They have formatted the cell under special, adding in a leading zero and adding in a dash between, so finished number looks like, 012-345.

I have a macro that removed the formatting, adds in the leading zero.  What I need to complete is to add in the dash.  Also it needs to be save as text, not a number.  Any help would be greatly appreciated.

Sub LeadingZeros1()

Dim cel As Range, rg As Range

Application.ScreenUpdating = False

Set rg = Range(Range("A3"), Cells(Rows.Count, 1).End(xlUp))

rg.NumberFormat = "@"

For Each cel In rg.Cells
cel.Value = "0" & cel.Value

Next

End Sub

 

I have tried “0##-###”, but I don’t know what code to use….

Thanks so much

Amy

sp_AnswersTopicSeeAnswer See Answer
Avatar
SunnyKow
Puchong, Malaysia

VIP
Members


Trusted Members
Level 8
Forum Posts: 1432
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
2
February 19, 2020 - 1:17 pm
sp_Permalink sp_Print

Hi Amy

Try changing this line from

cel.Value = "0" & cel.Value

to

cel.Value = Format(cel.Value, "000-000")

Hope this helps

Sunny

sp_AnswersTopicAnswer
Answers Post
Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 414
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
3
February 19, 2020 - 2:25 pm
sp_Permalink sp_Print sp_EditHistory

You could just do this in a formula if it helps - i have used this so many times in my work.

=REPT("0",6-LEN(A1))&A1

Purfleet

Avatar
SunnyKow
Puchong, Malaysia

VIP
Members


Trusted Members
Level 8
Forum Posts: 1432
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
4
February 19, 2020 - 3:32 pm
sp_Permalink sp_Print

To include a dash, you can try TEXT(A3,"000-000")

The following users say thank you to SunnyKow for this useful post:

Purfleet
Avatar
Amy 73
Member
Members
Level 0
Forum Posts: 39
Member Since:
February 11, 2015
sp_UserOfflineSmall Offline
5
February 20, 2020 - 2:24 am
sp_Permalink sp_Print

SunnyKow

Thank you so much.  This works great!  

Amy

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Sherry Fox, Cassie Bernier
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 215
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Abisola Ogundele
MARTYN STERRY
Rahim Lakhani
Ngoc Qui Nguyen
Clement Mansfield
Rose .
Bindu Menon
Baruch Zemer
Purple RainbowBenefactor
MOTH Junkie
Forum Stats:
Groups: 3
Forums: 24
Topics: 6349
Posts: 27772

 

Member Stats:
Guest Posters: 49
Members: 32317
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.