To add variable space in a cell

dhavnil06

New Member
Joined
Nov 21, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi There,

Is it possible to add variable space (but the character's length is fixed at 31) using VBA? I will show an example below.
1700572016595.png

It should add the space (subtracting the initial characters) but shouldn't exceed 31 characters in a cell. The length of initial characters can vary (it's not fixed). Hope this clears the issue I am having.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi try this:
VBA Code:
Sub mmm()
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Do While Len(Cells(i, 1).Value) < 31
            Cells(i, 1).Value = Cells(i, 1).Value & Chr(32)
        Loop
    Next
    MsgBox "task complete"
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
Assuming you will never have cells with more than 31 characters to start with. Another option is
VBA Code:
Sub dhavnil()
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(@="""","""",@&rept("" "",31-len(@)))", "@", .Address))
   End With
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
Assuming you will never have cells with more than 31 characters to start with. Another option is
VBA Code:
Sub dhavnil()
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(@="""","""",@&rept("" "",31-len(@)))", "@", .Address))
   End With
End Sub
Hi Fluff,

Thanks for the reply, but I'll have characters after 31 to 61 length.
 
Upvote 0
Hi try this:
VBA Code:
Sub mmm()
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Do While Len(Cells(i, 1).Value) < 31
            Cells(i, 1).Value = Cells(i, 1).Value & Chr(32)
        Loop
    Next
    MsgBox "task complete"
End Sub
Hi Rustom,

This is working fine but it is taking so much time to run. Is there a way to minimize the run-time?
 
Upvote 0
Try this:
VBA Code:
Option Explicit
Sub ADDspace()
Dim lr&, i&, rng
lr = Cells(Rows.Count, "A").End(xlUp).Row
rng = Range("A1:A" & lr).Value
For i = 1 To UBound(rng)
    If Len(rng(i, 1)) < 31 Then rng(i, 1) = Left(rng(i, 1) & String(31, " "), 31)
Next
Range("A1:A" & lr).Value = rng
End Sub

BEFORE
bfr.JPG



AFTER:
after.JPG
 
Upvote 0
Welcome to the MrExcel board!

Doing them all at once, even if longer than 31 ..

VBA Code:
Sub dhavnil_v2()
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(len(@)>31,@,left(@&rept("" "",31),31))", "@", .Address))
   End With
End Sub

Before:

1700630018820.png


After:

1700630072308.png
 
Upvote 0
Hi Rustom,

This is working fine but it is taking so much time to run. Is there a way to minimize the run-time?
Fixed...
VBA Code:
Sub mmm()
Application.ScreenUpdating = False
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    arr = Range(Cells(1, 1), Cells(lr, 1)).Value
    For i = 1 To UBound(arr)
          Do While Len(arr(i, 1)) < 31
                arr(i, 1) = arr(i, 1) & Chr(32)
          Loop
    Next
    [a1].Resize(UBound(arr), 1).Value = Application.Transpose(arr)
Application.ScreenUpdating = True
MsgBox "task complete"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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