Consulting

Results 1 to 3 of 3

Thread: Current line number & first letter of string is first letter of the line

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Location
    MUMBAI
    Posts
    13
    Location

    Current line number & first letter of string is first letter of the line

    Hello all,

    I am working on a excel macro where a word file is opened and a list of string are searched.

    I want to check if the string being searched is positioned at the start of a line. But i don't want the system to use cursor or selection, a simple find and execute and to check if the first character of the selected string is the first character of the line.

    Any help would be greatly appreciated.

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Location
    The Netherlands
    Posts
    45
    Location
    Perhaps you can show us a example?

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,391
    Location
    Maybe try this code

    Function IsAtStartOfSentence(ByVal SearchString As String, ByVal SearchRange As Range) As Boolean
        ' Declare variables
        Dim StartOfSentence As String
        Dim FoundPosition As Long
        ' Define the characters that can precede the start of a sentence
        StartOfSentence = ".!?;" 
        ' Add any other characters you consider as sentence endings
        ' Find the position of the search string within the specified range
        FoundPosition = InStr(SearchRange.Text, SearchString)
        ' If the search string is not found, return False
        If FoundPosition = 0 Then
            IsAtStartOfSentence = False
            Exit Function
        End If
        ' Check if the character preceding the search string is a sentence ending character
        If FoundPosition > 1 Then
            If InStr(StartOfSentence, Mid(SearchRange.Text, FoundPosition - 1, 1)) > 0 Then
                IsAtStartOfSentence = True
            Else
                IsAtStartOfSentence = False
            End If
        Else
            ' If the search string is at the very beginning of the range, assume it's at the start of a sentence
            IsAtStartOfSentence = True
        End If
      End Function
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •