Count number of periods in target.value and indent

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
In VBA I would like to have a Worksheet_Change event which when somebody types in a value in column A the macro would do two things.

First, check to make sure the value ends in a period and then indent the value based on the number of periods found?

This is what I have so far...it checks for the ending period and indents, but not sure how to return the number of periods which I would like to use to replace the hard coded IndentLevel. Is this the right way to go or is there a better way?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As String
    If Target.Column <> 1 Then Exit Sub
    i = Right(Target.Value, 1)
    If i = "." Then
        MsgBox Len(Split(Target.Value)(0))
        Range(Target.Address).IndentLevel = 2
    Else
        MsgBox "Please enter a number which ends in a period", vbInformation
    End If
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
whilst i think on this how are you getting the full stop (Period) to stay at the end of a number when it is keyed in?

Ive run your macro with text and it is fine but the period disappears when I key a number in the column
 
Upvote 0
This will count the number of periods

Code:
PeriodCount = Len(Target.Value) - Len(Replace(Target.Value, ".", ""))
 
Upvote 0
I'm not so brilliant at this but have you tried dropping the variable i

and just saying if right (target.value,1) = "." then

followed by

if right (target.value,2) = ".."
 
Upvote 0
Maybe this?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Long
    If Target.Column <> 1 Then Exit Sub
    i = Len(Target.Text) - Len(Replace(Target.Text, ".", ""))
    If i > 0 Then
        Range(Target.Address).IndentLevel = i
    Else
        MsgBox "Please enter a number which ends in a period", vbInformation
    End If
End Sub



Edit: Hmmmm, second thoughts that counts all periods, not just at the end
 
Upvote 0
Thanks AlphaFrog that does it. Thanks to all others also :)
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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