Home

Help with excel

%3CLINGO-SUB%20id%3D%22lingo-sub-871032%22%20slang%3D%22en-US%22%3EHelp%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-871032%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20file%20that%20I%20use%20for%20analysis%20on%20a%20monthly%20basis%2C%20and%20I%20was%20wondering%20if%20is%20there%20a%20way%20to%20format%20a%20column%20to%20always%20be%20G0000xxxx.%20The%20column%20is%20called%20Grant%20and%20many%20people%20instead%20of%20entering%20the%20entire%20grant%20code%2C%20they%20enter%20G%201234%2C%20I%20want%20to%20add%20a%20script%20of%20formatting%20so%20that%20if%20anyone%20enters%20G%201234%20it%20will%20automatically%20change%20to%20G00001234.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAmanda%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-871032%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-871068%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-871068%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412564%22%20target%3D%22_blank%22%3E%40amandaramos%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20may%20try%20something%20like%20this...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0AIf%20Target.CountLarge%20%26gt%3B%201%20Then%20Exit%20Sub%0ADim%20Matches%20As%20Object%0ADim%20str%20%20%20%20%20As%20String%0AIf%20Target.Column%20%3D%201%20And%20Target.Row%20%26gt%3B%201%20Then%0A%20%20%20%20On%20Error%20GoTo%20Skip%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20If%20Target%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20%20%20%20%20str%20%3D%20Replace(Target.Value%2C%20%22%20%22%2C%20%22%22)%0A%20%20%20%20%20%20%20%20With%20CreateObject(%22VBScript.RegExp%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.Global%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20.IgnoreCase%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20.Pattern%20%3D%20%22(G)(%5Cd%2B)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20.Test(str)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Set%20Matches%20%3D%20.Execute(str)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20str%20%3D%20UCase(Matches(0).SubMatches(0))%20%26amp%3B%20Format(Matches(0).SubMatches(1)%2C%20%2200000000%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Target%20%3D%20str%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20End%20If%0AEnd%20If%0ASkip%3A%0AApplication.EnableEvents%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20above%20is%20for%20Sheet%20Change%20Event%20and%20is%20placed%20on%20Sheet1%20Module.%20To%20view%20the%20code%2C%20right%20click%20on%20Sheet1%20Tab%20and%20choose%20View%20Code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20per%20the%20code%2C%20if%20you%20input%20the%20Grant%20Code%20in%20Column%20A%20starting%20from%20Row%232%2C%20the%20code%20will%20be%20converted%20into%20the%20desired%20format.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873234%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873234%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40amandaramos%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20may%20try%20something%20like%20this...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20above%20is%20for%20Sheet%20Change%20Event%20and%20is%20placed%20on%20Sheet1%20Module.%20To%20view%20the%20code%2C%20right%20click%20on%20Sheet1%20Tab%20and%20choose%20View%20Code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873335%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873335%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412564%22%20target%3D%22_blank%22%3E%40amandaramos%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ean%20alternative%20without%20using%20VBA%20is%20a%20combination%20of%20data%20validation%20and%20formatting.%20Use%20Data%20Validation%20to%20allow%20only%20whole%20numbers%20in%20the%20cells.%20Set%20the%20minimum%20to%20the%20smallest%20grant%20number%20and%20the%20maximum%20to%2099999999%20if%20grant%20numbers%20can%20have%20up%20to%20eight%20digits.%20Then%20format%20the%20cells%20with%20custom%20format%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%5CG00000000%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20this%20approach%20you%20don't%20need%20to%20save%20your%20workbook%20as%20a%20macro-enabled%20spreadsheet%20and%20it%20works%20on%20all%20devices%2C%20i.e.%20web%2C%20mobile%20and%20desktop%20Excel.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20796px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F133378i43BAEBF91643D901%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222019-09-25_08-46-11.png%22%20title%3D%222019-09-25_08-46-11.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20do%20want%20to%20use%20code%2C%20here%20is%20a%20simpler%20version%20that%20also%20works%20on%20multiple%20cells%20at%20a%20time%2C%20if%20data%20is%20copied%20and%20pasted%2C%20for%20example.%20It%26nbsp%3Blooks%20for%20the%20numbers%20entered%20into%20column%20A%2C%20so%20you%20need%20to%20change%20that%20to%20the%20column%20you%20need.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20apply%20the%20code%2C%20copy%20it%2C%20then%20right-click%20the%20sheet%20tab%2C%20select%26nbsp%3B%3CSTRONG%3EView%20Code%3C%2FSTRONG%3E%20and%20paste%20the%20code%20into%20the%20big%20white%20code%20window.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%0A%0ADim%20cel%20As%20Range%0ADim%20padding%20As%20String%0A%0AIf%20Not%20Intersect(Target%2C%20Range(%22A%3AA%22))%20Is%20Nothing%20Then%0A%20%20%20%20Application.EnableEvents%20%3D%20False%0A%20%20%20%20For%20Each%20cel%20In%20Target%0A%20%20%20%20%20%20%20%20padding%20%3D%20%2200000000%22%0A%20%20%20%20%20%20%20%20padding%20%3D%20Left(padding%2C%208%20-%20Len(cel.Value))%0A%20%20%20%20%20%20%20%20cel%20%3D%20%22G%22%20%26amp%3B%20padding%20%26amp%3B%20cel.Value%0A%20%20%20%20Next%20cel%0A%20%20%20%20Application.EnableEvents%20%3D%20True%0A%0AEnd%20If%0A%0AEnd%20Sub%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873616%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873616%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412564%22%20target%3D%22_blank%22%3E%40amandaramos%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20should%20place%20that%20code%20on%20Sheet%20Module%20and%20to%20do%20that%20refer%20to%20the%20following%20image%20to%20know%20the%20steps%20required.%3C%2FP%3E%3CP%3E1)%20Right%20click%20on%20Sheet%20Tab.%3C%2FP%3E%3CP%3E2)%20Choose%20View%20Code%20and%20it%20will%20open%20a%20code%20window.%3C%2FP%3E%3CP%3E3)%20Paste%20the%20code%20into%20the%20opened%20code%20window.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20539px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F133399i9F9F698374EF46E3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22SheetModule.jpg%22%20title%3D%22SheetModule.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-874693%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-874693%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20like%20the%20fact%20that%20it%20doesn't%20need%20macros...since%20this%20file%20lives%20in%20SharePoint%20and%20it%20doesn't%20support%20macros.%20I%20followed%20your%20approach%20with%20data%20validation%20but%20it%20didn't%20work.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Esee%20my%20file%20attached.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-875881%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-875881%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412564%22%20target%3D%22_blank%22%3E%40amandaramos%3C%2FA%3E%26nbsp%3B%20The%20file%20you%20attached%20does%20not%20use%20any%20data%20validation.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20part%20of%20my%20approach%20does%20not%20work%3F%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EData%20validation%20works%20only%20when%20data%20is%20entered%20into%20a%20cell.%20If%20you%20already%20have%20text%20in%20a%20cell%2C%20applying%20data%20validation%20for%20whole%20numbers%20will%20not%20change%20the%20text%20in%20the%20cell.%20Also%2C%20custom%20number%20formats%20won't%20work%20in%20the%20cells.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20see%20you%20have%20applied%20the%20number%20format.%20Now%20you%20need%20to%20turn%20the%20data%20into%20numbers.%20For%20most%20of%20the%20data%20you%20can%20use%20this%20approach%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20select%20column%20L%3C%2FP%3E%0A%3CP%3E-%20use%20Find%20and%20Replace%3C%2FP%3E%0A%3CP%3E-%20Find%20%22G%3F%22%20(that%20is%20a%20capital%20G%20and%20a%20space)%3C%2FP%3E%0A%3CP%3E-%20Replace%20with%20--%20leave%20that%20empty%3C%2FP%3E%0A%3CP%3E-%20Click%26nbsp%3B%3CSTRONG%3EReplace%20All%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20a%20few%20cells%20where%20the%20space%20is%20a%20special%20non-breaking%20character%2C%20not%20the%20regular%20space%20character.%20Therefore%2C%20use%20the%20wildcard%20character%20%3F%20in%20the%20find%20box.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20the%20cells%20have%20been%20converted%20to%20numbers%20and%20show%20the%20custom%20format.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20next%20step%2C%20apply%20the%20data%20validation%20to%20the%20whole%20column%2C%20so%20any%20new%20grant%20number%20entered%20can%20only%20use%20whole%20numbers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
amandaramos
New Contributor

Hello, 

 

I have a file that I use for analysis on a monthly basis, and I was wondering if is there a way to format a column to always be G0000xxxx. The column is called Grant and many people instead of entering the entire grant code, they enter G 1234, I want to add a script of formatting so that if anyone enters G 1234 it will automatically change to G00001234. 

 

Thank you, 

 

Amanda 

6 Replies

@amandaramos 

You may try something like this...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim Matches As Object
Dim str     As String
If Target.Column = 1 And Target.Row > 1 Then
    On Error GoTo Skip
    Application.EnableEvents = False
    If Target <> "" Then
        str = Replace(Target.Value, " ", "")
        With CreateObject("VBScript.RegExp")
            .Global = False
            .IgnoreCase = True
            .Pattern = "(G)(\d+)"
            If .Test(str) Then
                Set Matches = .Execute(str)
                str = UCase(Matches(0).SubMatches(0)) & Format(Matches(0).SubMatches(1), "00000000")
                Target = str
            End If
        End With
    End If
End If
Skip:
Application.EnableEvents = True
End Sub

 

The code above is for Sheet Change Event and is placed on Sheet1 Module. To view the code, right click on Sheet1 Tab and choose View Code.

 

As per the code, if you input the Grant Code in Column A starting from Row#2, the code will be converted into the desired format.

 

 

 

Hi @Subodh_Tiwari_sktneer  where do I place this code? 

 

Thanks, 

 

Amanda

Hello @amandaramos ,

 

an alternative without using VBA is a combination of data validation and formatting. Use Data Validation to allow only whole numbers in the cells. Set the minimum to the smallest grant number and the maximum to 99999999 if grant numbers can have up to eight digits. Then format the cells with custom format

 

\G00000000

 

For this approach you don't need to save your workbook as a macro-enabled spreadsheet and it works on all devices, i.e. web, mobile and desktop Excel.

 

2019-09-25_08-46-11.png

 

If you do want to use code, here is a simpler version that also works on multiple cells at a time, if data is copied and pasted, for example. It looks for the numbers entered into column A, so you need to change that to the column you need. 

 

To apply the code, copy it, then right-click the sheet tab, select View Code and paste the code into the big white code window. 

 

 

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cel As Range
Dim padding As String

If Not Intersect(Target, Range("A:A")) Is Nothing Then
    Application.EnableEvents = False
    For Each cel In Target
        padding = "00000000"
        padding = Left(padding, 8 - Len(cel.Value))
        cel = "G" & padding & cel.Value
    Next cel
    Application.EnableEvents = True

End If

End Sub

 

 

@amandaramos 

You should place that code on Sheet Module and to do that refer to the following image to know the steps required.

1) Right click on Sheet Tab.

2) Choose View Code and it will open a code window.

3) Paste the code into the opened code window.

 

SheetModule.jpg

Hello, @Ingeborg Hawighorst 

 

I like the fact that it doesn't need macros...since this file lives in SharePoint and it doesn't support macros. I followed your approach with data validation but it didn't work. 

 

see my file attached. 

@amandaramos  The file you attached does not use any data validation. 

 

What part of my approach does not work? 

 

Data validation works only when data is entered into a cell. If you already have text in a cell, applying data validation for whole numbers will not change the text in the cell. Also, custom number formats won't work in the cells.

 

I see you have applied the number format. Now you need to turn the data into numbers. For most of the data you can use this approach:

 

- select column L

- use Find and Replace

- Find "G?" (that is a capital G and a space)

- Replace with -- leave that empty

- Click Replace All

 

There are a few cells where the space is a special non-breaking character, not the regular space character. Therefore, use the wildcard character ? in the find box.

 

Now the cells have been converted to numbers and show the custom format. 

 

As a next step, apply the data validation to the whole column, so any new grant number entered can only use whole numbers.

 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies