Visual Basic: how can I limit the number of characters in a range

msmith3

New Member
Joined
Feb 1, 2022
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
I have the following code. Within the range B4-P4 are the days of the week (Monday-Sunday) and I'm trying to limit the characters so that it will say (Mon-Sun). I'm not sure if .Left is the right property to use so if anyone has any other ideas, please offer them!


With Range("B4:P4")
.Font.Size = 10
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Left ........... (Help?!)

End With
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I have the following code. Within the range B4-P4 are the days of the week (Monday-Sunday) and I'm trying to limit the characters so that it will say (Mon-Sun). I'm not sure if .Left is the right property to use so if anyone has any other ideas, please offer them!


With Range("B4:P4")
.Font.Size = 10
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Left ........... (Help?!)

End With
How are the days of the week entered into the cells. Is it via another code, from a date or are they manually entered?
If from a date then you could use the FORMAT to just have the three letters of each day. Assuming they are entered manually you will need to loop through each cell in the range (if you want to use the left function) something like the below should work.

VBA Code:
Set Rng = Range("g2:M2")

For Each cel In Rng
    cel = left(cel, 3)

    With cel
      .Font.Size = 10
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With

Next cel
 
Upvote 0
If you can't find a real solution, try this little trick.
VBA Code:
Dim rng As Range
Set rng = Range("B4:P4")
For Each cell In rng
    With rng.Characters(Start:=4, Length:=Len(cell) - 3).Font
        .ColorIndex = 2
    End With
Next cell
 
Upvote 0
How are the days of the week entered into the cells. Is it via another code, from a date or are they manually entered?
If from a date then you could use the FORMAT to just have the three letters of each day. Assuming they are entered manually you will need to loop through each cell in the range (if you want to use the left function) something like the below should work.

VBA Code:
Set Rng = Range("g2:M2")

For Each cel In Rng
    cel = left(cel, 3)

    With cel
      .Font.Size = 10
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With

Next cel
The Macro CreateCalendar creates this:
Jan.png
the code for the days of the week is written in CreateCalendar as:

createCal.png


The results of that macro is then copied into the macro GetCalendars to look like this:
Calendar.png

This is the code for GetCalendars:

GetCalendar.png

My goal is to edit the days of the week in the macro GetCalendars to say Mon, Tues, Wed, Thurs, Fri, Sat, Sun rather than the current Monday, Tuesday, Wednesday, etc.
 
Upvote 0
Just replace everything below your line

'Edit the written days of the week

with this and it will work

VBA Code:
Set Rng = Range("B4:P4")

For Each cel In Rng
    cel = left(cel, 3)

    With cel
      .Font.Size = 10
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With

Next cel
 
Upvote 0
Maybe this piece of code is what you want.
VBA Code:
Dim rng As Range
Dim cell As Range
Set rng = Union(Range("B5:H5"), Range("J5:P5"))
For Each cell In rng
    With cell
        If .Value = "Tuesday" Then
               .Value = Left(.Value, 4)
        ElseIf .Value = "Thursday" Then
               .Value = Left(.Value, 5)
        Else
               .Value = Left(.Value, 3)
        End If
     End With
Next cell
 
Upvote 0
Solution
Maybe this piece of code is what you want.
VBA Code:
Dim rng As Range
Dim cell As Range
Set rng = Union(Range("B5:H5"), Range("J5:P5"))
For Each cell In rng
    With cell
        If .Value = "Tuesday" Then
               .Value = Left(.Value, 4)
        ElseIf .Value = "Thursday" Then
               .Value = Left(.Value, 5)
        Else
               .Value = Left(.Value, 3)
        End If
     End With
Next cell
That worked!! Thank you so much you saved me from a major headache.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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