ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Fixed length cells (https://www.excelbanter.com/excel-programming/388929-fixed-length-cells.html)

Prema

Fixed length cells
 
I would like to fix the length of column B to 10 characters. if actual vale
is less than 10 characters, I would like to pad rightmost characters with
spaces. Any help would be appreciated.

Prema

Gary''s Student

Fixed length cells
 
Try this small event macro:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("B:B"), Target) Is Nothing Then
t = Target.Value
n = Len(t)
If n 10 Then
t = Right(t, 10)
Application.EnableEvents = False
Target.Value = t
Application.EnableEvents = True
Exit Sub
End If
If n < 10 Then
t = t & Application.Rept(" ", 10 - n)
Application.EnableEvents = False
Target.Value = t
Application.EnableEvents = True
Exit Sub
End If
End If
End Sub



If the input is less than 10 characters, the padding is applied.
iIf the input is greater than 10 characters, the result is truncated.
--
Gary''s Student - gsnu200719

Prema

Fixed length cells
 
Thank you.
Prema

"Gary''s Student" wrote:

Try this small event macro:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("B:B"), Target) Is Nothing Then
t = Target.Value
n = Len(t)
If n 10 Then
t = Right(t, 10)
Application.EnableEvents = False
Target.Value = t
Application.EnableEvents = True
Exit Sub
End If
If n < 10 Then
t = t & Application.Rept(" ", 10 - n)
Application.EnableEvents = False
Target.Value = t
Application.EnableEvents = True
Exit Sub
End If
End If
End Sub



If the input is less than 10 characters, the padding is applied.
iIf the input is greater than 10 characters, the result is truncated.
--
Gary''s Student - gsnu200719



All times are GMT +1. The time now is 12:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com