Macro VBA: CellLength Setting

lsteffen

Board Regular
Joined
May 11, 2006
Messages
111
Good afternoon,

I am trying to get a macro to format a cell to be 14 characters in length regardless of the size of the data in the field.

Example:

Before Macro Runs:
B1 = [650]

After Macro Runs:
B1=[ 650]

My current code, both ways, makes the cell 14 characters plus the character and in the above example, B1 has 17 characters. Here are my two versions of code. Can someone please help? Thank you in advance for your help!!! Liz

Version 1

Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count
Columns("B:B").Select
Selection.NumberFormat = "@"
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("G1").Formula = "=RIGHT(RC[-5]& REPT("" "",14),14)"
Worksheets("Data").Range("G1:G" & LastRow).FillDown
Columns("G:G").Select
Selection.Copy
Columns("B:B").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("B:B").AutoFit
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Range("B1").Select

Version 2

'Dim NumSpaces As Long
'Dim LastRow As Integer
'LastRow = ActiveSheet.UsedRange.Rows.Count
'NumSpaces = 14
'Columns("G:G").Select
'Selection.NumberFormat = "General"
'Range("G1").Formula = "=REPT("" ""," & NumSpaces & ")&RC[-5]"
'Worksheets("Data").Range("G1:G" & LastRow).FillDown
'Columns("G:G").Select
'Selection.Copy
'Columns("B:B").Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Columns("B:B").AutoFit
'Columns("G:G").Select
'Selection.Delete Shift:=xlToLeft
'Columns("B:B").Select
'Selection.NumberFormat = "@"
'Range("B1").Select
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
First off, there is rarely any need to select Cells in order to work with them. You can work with ranges directly in VBA.
Code:
Sub test()
Dim s As String * 14, c As Range
For Each c In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    c.NumberFormat = "@"
    s = Right(c, 14)
    c = s
Next
End Sub
 
Upvote 0
Another way:
Code:
Sub x()
    With Range("B1", Cells(Rows.Count, "B").End(xlUp))
        .NumberFormat = "@"
        .Value = Evaluate("if(row(), right(""             "" & " & .Address & ",14))")
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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