VBA format entry

cyrilbrd

Well-known Member
Joined
Feb 2, 2012
Messages
4,113
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi,
Given a UserForm with a TextBox named TextBox1
The textbox will feed data to an excel table using the following code:
VBA Code:
Sheets("Data").Range("Data_Start").Offset(TargetRow, 6).Value = TextBox1

We would like the user to encode 6 numbers example 123456 that would show in the table as "STATUS-12-3456"

So if 230123 was typed in the textbox, the following would appear in the table "STATUS-23-0123"
Any advice?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:
PHP:
Sheets("Data").Range("Data_Start").Offset(TargetRow, 6).Value = "STATUS-" & Format( TextBox1,"00-0000")
 
Upvote 1
Hi
if you want the value in the cell to remain numeric (like a date) then you can apply a Number Format

VBA Code:
With Worksheets("Data").Range("Data_Start").Offset(TargetRow, 6)
        .Value = TextBox1.Value
        .NumberFormat = """STATUS-""00-0000"
    End With

Dave
 
Upvote 1
Thank you for the proposed solutions, I can use the second one for another entry that would require the cell to remain numerical.
 
Upvote 0
Given the above. The users are allowed to recall / edit previously entered information.
The code used for recalling TextBox1 is:
VBA Code:
Data_UF.TextBox1= Sheets("Data").Range("Data_Start").Offset(TargetRow, 6).Value

How can I force a format as the data is modified?
Example above the data should show "STATUS-12-3456"
Further for value with several decimal point or integer what would be the best method to show numerical instead of text?
Example
VBA Code:
Data_UF.Txt_Size = Sheets("Data").Range("Data_Start").Offset(TargetRow, 4).Value
Shows the result as a text instead of let's say a 4 decimal place integer.
 
Upvote 0
Shall I use CDbl(Txt_Size) or is there a better way?
 
Upvote 0
VBA Code:
Sheets("Data").Range("Data_Start").Offset(TargetRow, 4).Value = Format(Txt_Size, "#.####")
Would that be the best approach?
 
Upvote 0
Given the above. The users are allowed to recall / edit previously entered information.
The code used for recalling TextBox1 is:
VBA Code:
Data_UF.TextBox1= Sheets("Data").Range("Data_Start").Offset(TargetRow, 6).Value

How can I force a format as the data is modified?

Use the Range.Text property

Rich (BB code):
Data_UF.Txt_Size = Sheets("Data").Range("Data_Start").Offset(TargetRow, 4).Text

This returns what you see in the cell rather that its underlying value

Dave
 
Upvote 0
Thank you for the proposed solutions, I can use the second one for another entry that would require the cell to remain numerical.

Rich (BB code):
With Worksheets("Data").Range("Data_Start").Offset(TargetRow, 6)
        .Value = Val(TextBox1.Value)
        .NumberFormat = """STATUS-""00-0000"
    End With

I got caught out by 10 min editing rule & omitted inclusion of Type Conversion to coerce text to numeric value.
Suggest add text shown in red.

Glad suggestion helps

Dave
 
Upvote 1
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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