+ Reply to Thread
Results 1 to 5 of 5

If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

    I'm using the s.autECLPS.GetText command to grab text out of the Terminal program.

    Once I grab data from that, I put it into a variable. I want to check that variable against A1, A2, A3, A4, etc., to see if that value already exists in the sheet I have.

    My problem comes from the fact that the text that I grab can vary in lengths - sometimes it's 5 digits, sometimes it's 20, so I've had to build the s.autECLPS.GetText to grab the max length each time - so on SHORTER items, the end result may include 5 digits, and 15 blank spaces, like this:

    Example 1: ASDF1234_________________
    Example 2: ASDFJSODAFJ2394827349293JS

    Prior to this function running, I have another piece of code that runs "text to columns" on column A and delete multiple blanks, so all of my data in column A has NO additional spaces at the end.

    This was working fine until I tried to do this last part. I cannot compare the data taken into the variable via s.autECLPS.GetText with the data in a cell, because the additional spaces make them always not equal.


    dCheck = s.autECLPS.GetText(1, 8, 3)  'This grabs the first 3 digits of the selection in Terminal and puts it into the variable dCheck

    The cell I am comparing it to is held in:
    Cells(tRow, 1).Offset(ttRow, 0).Value
    How do I say:

    If dCheck = the first the characters in Cells(tRow, 1).Offset(ttRow, 0).Value then

    Thanks!

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

    Not sure I totally follow you. Would this work:

    dCheck = Application.Trim(dCheck)
    If dCheck = Left(Application.Trim(Cells(tRow, 1).Offset(ttRow, 0).Value), Len(dCheck)) Then

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

    Hmmm - this does not work.

    I will try to explain a bit better what i want to do here:

    A2 = D12345

    When I type in dCheck = s.autECLPS.GetText(1, 8, 3) , it loads the Terminal, then goes to Row 1, Cell 8, and grabs the first 3 letters after that. The text that it grabs is "D12"

    I then want to go back into Excel and ask "Is the value of dCheck equal to the first 3 letters in cell A2?"

    Excel should then look and say "Yes, D12 = D12"

    When I used your code, (and added msg boxes to see what was was being captured), I found that:

                dCheck = s.autECLPS.GetText(1, 8, 3)
                dCheck = Application.Trim(debitCheck)
                MsgBox Cells(tRow, 1).Offset(ttRow, 0).Value & " at " & Cells(tRow, 1).Offset(ttRow, 0).Address
                MsgBox Left(Application.Trim(Cells(tRow, 1).Offset(ttRow, 0).Value), Len(dCheck))
                If dCheck = Left(Application.Trim(Cells(tRow, 1).Offset(ttRow, 0).Value), Len(dCheck)) Then
                    MsgBox Left(Application.Trim(Cells(tRow, 1).Offset(ttRow, 0).Value), Len(dCheck))
                End If

                MsgBox Left(Application.Trim(Cells(tRow, 1).Offset(ttRow, 0).Value), Len(dCheck))
    Is just giving me null value, instead of the first 3 characters from the left.

    I'd try to help fix the code, but I'm not familiar with the Application.Trim command at all

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

    The trim function trims all the blank spaces. You could also use the built-in clean function if that would be an issue.

    Would something like this work?

    Sub test()
        Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet        'may need to change
        Dim i As Long, dCheck As String
        dCheck = s.autECLPS.GetText(1, 8, 3)
    
        With ws
            For i = 1 To .Range("A1", .Cells(Rows.Count, 1).End(xlUp)).Rows.Count
                If dCheck = Left(Trim(Application.Clean(.Cells(i, 1).Value)), Len(dCheck)) Then
                    MsgBox Left(Trim(Application.Clean(.Cells(i, 1).Value)), Len(dCheck)) & _
                           " at " & .Cells(i, 1).Address        'change
                    Exit For
                End If
            Next i
        End With
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: If (the first 3 letters of a variable's value) = (the first 3 letters in a given cell)

    I messed around with the code, so I can't go back and paste that in - but I did test out doing:

    dCheck = s.autECLPS.GetText(1, 8, 11)
    Msgbox dCheck & "."
    dCheck = Application.Trim(debitCheck)
    Msgbox dCheck & "."
    and it trimmed all the extra spaces out of the variable dCheck - this is EXACTLY what I needed. thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Reading the last 2 letters in a combobox value and enter those 2 letters into column
    By emilyloz in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-26-2013, 09:26 AM
  2. Replies: 5
    Last Post: 12-30-2012, 10:59 AM
  3. Replies: 2
    Last Post: 12-01-2010, 02:29 PM
  4. Replies: 9
    Last Post: 07-15-2010, 08:54 PM
  5. How to sort Single Letters before Double Letters
    By jabberdoo in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-09-2008, 12:18 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1