Results 1 to 6 of 6

Thread: How to locate a text in an excel file using vb?

  1. #1

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Question How to locate a text in an excel file using vb?

    Hi! I don't know if anyone can help me with this. I would like to find a text (which may vary for example: rev1 or rev 1) in an excel file provided the worksheet is specified using VB6 and show only the 1 (instead of rev1) in a textbox.

    Example:

    excel file = helpme.xls (with 3 worksheets available is help1, help2, help3)
    specified worksheet = help1
    text to find = rev1 (or rev 1 --> there may be times where there is a space between rev and 1.)
    text to show in textbox = 1

    Please help.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How to locate a text in an excel file using vb?

    Have a look at .Find method on a range of cells.
    I think you can use it on a worksheet, but usually its on a range of cells.

    You can use wildcards and I think it would be "rev*1" or just "rev".
    Once you have the cell, you need to us string manipulation, such as mid or left.

    Post up if/when you have code and its not working quite right.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Question Re: How to locate a text in an excel file using vb?

    It still doesn't return the numeric (4) in rev 4 or rev4.

    Here's the code:

    VB Code:
    1. (frm_loadfile)
    2. Option Explicit
    3.  
    4. Dim xl0a As New Excel.Application
    5. Dim xl0b As Excel.Workbook
    6. Dim xl0s As Excel.Worksheet
    7. Public var As Variant  ' variable for project
    8. Public var1 As Variant 'variable for package code
    9. Public var2 As Variant 'variable for parts list version
    10.  
    11. Private Sub loadwrksht()
    12. Dim X As Integer
    13. Dim xtotal As Integer
    14. Dim counter As Integer
    15. Dim project As String
    16. Dim packagecode As String
    17. Dim plversion As String
    18.  
    19.  
    20.     Set xl0a = New Excel.Application
    21.     Set xl0b = xl0a.Workbooks.Open(txt_filename.Text)
    22.    
    23.     xtotal = xl0b.Worksheets.Count
    24.     lst_wrksht.Clear
    25.     For counter = 1 To xtotal
    26.         Set xl0s = xl0b.Worksheets(counter)
    27.         lst_wrksht.AddItem xl0s.Name
    28.     Next counter
    29.  
    30.     var = xl0s.Range("A1").Value
    31.     var1 = xl0s.Range("A2").Value
    32.     var2 = xl0s.Range("C4").Find("rev")
    33.     txt_version.Text = var2
    34.    
    35.    
    36.     project = var
    37.     packagecode = var1
    38.     plversion = var2
    39.    
    40.     xl0b.Close
    41.     xl0a.Quit
    42.     Set xl0s = Nothing
    43.     Set xl0b = Nothing
    44.     Set xl0a = Nothing
    45.    
    46. End Sub
    47.  
    48. Function GetVer(tmpStr As String, tmpSpc As String, Mode As String) As String
    49. Dim tmpRetStr As String
    50. Dim tmpLen As Integer
    51. Dim tmpErr As Integer
    52.   ' *****************************************************
    53.   ' tmpString = The whole string
    54.   ' tmpSpc = Space chr
    55.   ' if mode = "F" then get the string in front of the div
    56.   ' if mode = "B" then get the string in back of the div
    57.   ' if mode = "C" then get the string in the middle
    58.   '
    59.   ' Return values:
    60.   '  Errors
    61.   '    Err1 = wrong mode ("F" & "B" ok)
    62.   '    Err2 =  No Div, did not found a divider
    63.   '    Err3 = No F, did not find any string in front of the div
    64.   '    Err4 = No B, did not find any string in back of the div
    65.   '    Err5 = No String
    66.   '    Err6 = No C, did not find any string in the middle
    67.   '  Normal
    68.   '    String
    69.   ' ******************************************************
    70.   ' Example: GetVer("Red=Green","=","F") will retrun "Red"
    71.   ' *** for string
    72.   tmpErr = 0
    73.   If Len(tmpStr) = 0 Then
    74.     tmpErr = 5
    75.     GoTo GetVerErr
    76.    
    77.  End If
    78.  
    79.   ' *** test for chr in tmpDiv
    80.   If Len(tmpSpc) = 0 Then
    81.     tmpErr = 2
    82.     GoTo GetVerErr
    83. '        MsgBox ("Error: Non-standard format in saving profile name.  No hyphen '-' found.")
    84. '        frm_dir.txt_plprofile.Text = "*.plp"
    85.   End If
    86.  
    87.   ' *** test for div in string
    88.   If InStr(1, tmpStr, tmpSpc) = 0 Then
    89.     tmpErr = 2
    90.     GoTo GetVerErr
    91. '    MsgBox ("Error: Non-standard format in saving profile name.  No hyphen '-' found.")
    92. '    frm_dir.txt_plprofile.Text = "*.plp"
    93.   End If
    94.  
    95.   ' *** process "F"
    96.   If Mode = "F" Then
    97.      tmpRetStr = Left(tmpStr, (InStr(1, tmpStr, tmpSpc) - 1))
    98.      If Len(tmpRetStr) > 0 Then
    99.        GetVer = tmpRetStr
    100.        Exit Function
    101.      Else
    102.        tmpErr = 3
    103.        GoTo GetVerErr
    104.      End If
    105.   End If
    106.  
    107.  
    108.  
    109.  
    110.     tmpErr = 1
    111.     frm_dir.txt_plprofile.Text = "*.plp"
    112.     'GoTo GetVerErr
    113.   Exit Function
    114. GetVerErr:
    115. GetVer = "Err" & tmpErr
    116. End Function

    VB Code:
    1. (frm_summary)
    2. Private Sub Form_Load()
    3. Dim plver As String
    4. Dim prof As String
    5.    
    6.     plver = frm_loadfile.GetVer(frm_field.txt_version, " ", "F")
    7.     lbl_versiondata.Caption = plver
    8.    
    9. End Sub

    Anyone, hope you can help.
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How to locate a text in an excel file using vb?

    Looks like the code works for the finding and returning the string.
    Have you debugged it (stepped through to see what is happening) ?
    Have you tested it?
    I ran it for the following line and it worked fine... (immediates window)
    Code:
    ?GetRev("Rev 1"," ","F")
    From your onload spreadsheet code this line
    Code:
    txt_version.Text = var2
    would return the first matching cell only. You didn't call splitting function you wrote.

    Also note that .Find returns a cell or nothing. You may need to check that you have something.



    In your original post you asked how to find a cell matching rev and then return the numeric part of that... Assuming cells contain Rev1, Rev 1, rev 2 etc..

    Try this?
    Code:
    Public Function ReturnDaNum(ByVal strWhole As String, strSearch As String) As Long
    
        Dim lngPosition As Long
        
        On Error Resume Next
        
        ReturnDaNum = 0
         
        If Len(strWhole) = 0 Or Len(strSearch) = 0 Then Exit Function
        
        lngPosition = InStr(1, LCase(strWhole), LCase(strSearch))
        If lngPosition > 0 Then
            lngPosition = lngPosition + Len(strSearch) - 1
            ReturnDaNum = CLng(Trim(Right(strWhole, Len(strWhole) - lngPosition)))
        End If
        
        If Err.Number <> 0 Then
            MsgBox "Error : " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "Error"
        End If
        
    End Function
    This should (I hope) cover most things, but you may want to use this and tweak it more to your needs.
    Last edited by Ecniv; May 26th, 2005 at 03:31 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: How to locate a text in an excel file using vb?

    Not sure you are using the find method correctly. Should look something like:

    VB Code:
    1. Cells.Find(What:="rev1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    2.         :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    3.         False, SearchFormat:=False).Activate

    Suggest you open Excel, go to the sheet you want to find the text, select cell A1, turn on the macro recorder (menu Tools -> Macro -> Record new macro), click Edit -> Find, type in the the text you want to find, stop the recorder, and look at the resulting code. You can tweek it from there.

    VBAhack

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How to locate a text in an excel file using vb?

    VbaHack:
    Using rng.find("text") will return. I tried with an excel sheet and all those options done and it either crashed or refused to run (cannot remember which). However, just the search text did what I wanted.

    It's only a starting point, and they should read the help file on it anyway.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width