Count characters of sentence

satya12

Board Regular
Joined
Oct 19, 2021
Messages
70
Office Version
  1. 2016
Platform
  1. Windows
Hi All,
Help me , how to write VBA Code for this
Here i am having sentences but those sentence will not go above 40 for one column , above 80 for another column.
If you see "CTO 2TB 7200 SATA Enterprise 3.5 in " this is the sentence for example .
if this sentence count greater than 40 , we have to delete the characters right to left until this sentence count will 40.
same as another columns if sentence character count greater than 80 remove the characters from right to left until the sentence meet the count80.
40 characters Columns : B for example
80 Characters Columns : C for example
Please help me for this
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Can't you just use the LEFT function to take the left-most 40 or 80 characters?

The LEFT function exists in BOTH Excel and VBA.
 
Upvote 0
Thanks for your help!
But, how to write vba code for if count is greater than 40 remove characters which are above
 
Upvote 0
Actually, if you want to fix the whole column at once, it would be quicker to use "Text to Columns", and select Fixed Width, and drop everything after spaces 40 and 80, respectively.
If you were applying this to columns B and C, the code would look something like this:
VBA Code:
Sub MyMacro()

'   Cap column B at 40 spaces
    Columns("B:B").TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 2), Array(40, 9)), TrailingMinusNumbers:=True
        
'   Cap column C at 80 spaces
    Columns("C:C").TextToColumns Destination:=Range("C1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 2), Array(80, 9)), TrailingMinusNumbers:=True
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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