Solved

Find specific word using VBA

Posted on 2012-04-02
30
417 Views
Last Modified: 2012-04-06
Hi,
I have something like this through out the attached file:
lr_start_transaction("Step02-Login to Dataupload")
so  anytime u see lr_start_transaction  then i want to copy everything in between "  "
in this case     Step02-Login to Dataupload   so after it is copied then i want to paste it in sheet2 starting column A2.  thanks for your help.
test--1-.xls
0
Comment
Question by:karinos57
  • 12
  • 12
  • 5
  • +1
30 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37799010
Sub getsteps()
    Dim cel As Range
    For Each cel In Sheet1.UsedRange
        If Left(cel, 3) = "lr_" Then
            Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Mid(cel, InStr(cel, """") + 1, InStr(InStr(cel, """") + 1, cel, """") + 1)
        End If
    Next cel
End Sub
0
 
LVL 8

Expert Comment

by:wchh
ID: 37799092
Correct formula shall be:
Sub getsteps()
    Dim cel As Range
    For Each cel In Sheet1.UsedRange
        If Left(cel, 3) = "lr_" Then
            Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Mid(cel, InStr(cel, """") + 1, InStr(InStr(cel, """") + 2, cel, """") - (InStr(cel, """") + 1))
        End If
    Next cel
End Sub

Open in new window

0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37799119
Yes I missed a bit. To be more exact it should be

Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Mid(cel, InStr(cel, """") + 1, InStr(InStr(cel, """") + 1, cel, """") - (InStr(cel, """") + 1))
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37799289
@karinos57

Is your file actually an Excel workbook or some other file format?
0
 

Author Comment

by:karinos57
ID: 37801049
guys thanks but i am getting this error when i run it:
"invalid procedure call or argument"
and the code stops here at this line"
Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Mid(cel, InStr(cel, """") + 1, InStr(InStr(cel, """") + 2, cel, """") - (InStr(cel, """") + 1))
0
 

Author Comment

by:karinos57
ID: 37801208
yes my file is actually in excel format
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37801823
I don't bother parsing the found strings:


Option Explicit
Public Sub CopyStartTrans()
    Dim rngFind As Range
    Dim strStartAddr As String
    Dim rngTgt As Range
    Dim wks As Worksheet
    Set wks = ActiveWorkbook.Worksheets("Sheet1")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngFind = wks.Cells.Find("lr_start_transaction(")
    If rngFind Is Nothing Then
        Exit Sub
    Else
        strStartAddr = rngFind.Address
    End If
    Application.ScreenUpdating = False
    Do
        rngFind.Copy rngTgt
        Set rngTgt = rngTgt.Offset(1)
        Set rngFind = wks.Cells.Find("lr_start_transaction(", rngFind)
    Loop Until rngFind.Address = strStartAddr
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range(rngTgt, rngTgt.End(xlDown))
    rngTgt.Replace "lr_start_transaction(""", vbNullString
    rngTgt.Replace """);", vbNullString
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:karinos57
ID: 37802758
thanks you so much aikimark

This was the perfect and the correct solution that I have seen so far.  I would like to ask you one more thing since you already figured out what I want to do.  I would also like to get some URL (http) address in my previous excel file.  I want to copy the FIRST URL FOR EACH STEP or the word  ‘lr_start_transaction’.  There are so many http addresses in the file but I am only interested the first address of EACH STEP       in other words the first address that starts with http after each   lr_start_transaction.  For instance:

lr_start_transaction("Step02-Open CA");
web_reg_find("Text=Product Links Display. Premier",
web_url("Main.aspx",
      URL=https://corona.com/microstrategy/asp/Main.aspx?pg=subProduct&slice=1&parAppId=&prCrumbId=0,
      URL=https://test..com/microstrategy/asp/Main.aspx?

So like the example above, I only want to the first address which is this: https://corona.com/microstrategy/asp/Main.aspx?  To be copied in sheet3 and do the same thing for each of the steps.  Basically the same thing you helped me in your post.  Pls. let me know if I need to open another request.  Thanks.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37802812
please post an example of what you need to see in Sheet2, based on your posted workbook data
0
 

Author Comment

by:karinos57
ID: 37803018
thanks.  Please see the sheet called Desired Results
test2.xls
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37803045
Please update your [Desired Results] worksheet with the step names.  You have a different number of step names than you nave URL lines.
0
 

Author Comment

by:karinos57
ID: 37803285
Thanks.  I have updated the file.  Pls. remember each step starts with the word 'lr_start_transaction'
and then need to grab only the first http url
many thanks
test2.xls
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37804228
This should come close.

Public Sub CopyStartTrans()
    Dim rngFind As Range
    Dim strStartAddr As String
    Dim rngTgt As Range
    Dim rngPrev As Range
    Dim rngCurrent As Range
    Dim rngItem As Range
    Dim wks As Worksheet
    Dim colRanges As New Collection
    Set wks = ActiveWorkbook.Worksheets("Sheet1")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngFind = wks.Cells.Find("lr_start_transaction(")
    If rngFind Is Nothing Then
        Exit Sub
    Else
        strStartAddr = rngFind.Address
    End If
    Application.ScreenUpdating = False
    Do
        colRanges.Add rngFind
        rngFind.Copy rngTgt
        Set rngTgt = rngTgt.Offset(1)
        Set rngFind = wks.Cells.Find("lr_start_transaction(", rngFind)
    Loop Until rngFind.Address = strStartAddr
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range(rngTgt, rngTgt.End(xlDown))
    rngTgt.Replace "lr_start_transaction(""", vbNullString
    rngTgt.Replace """);", vbNullString
    
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("B2")
    Set rngFind = wks.Cells.SpecialCells(xlCellTypeLastCell)
    colRanges.Add rngFind
    For Each rngItem In colRanges
        
        Do  '<<Group
            If rngPrev Is Nothing Then
                Set rngPrev = rngItem
                Set rngCurrent = rngPrev
                Exit Do
            Else
                Set rngPrev = rngCurrent
            End If
            
            Set rngCurrent = rngItem
            Set rngFind = wks.Range(rngPrev, rngCurrent.Offset(0, 1)).Find("=http")
            
            strStartAddr = rngFind.Value      'reusing string variable
            rngTgt.Value = Replace(Mid$(strStartAddr, InStr(strStartAddr, "=") + 1), ",", vbNullString)
            Set rngTgt = rngTgt.Offset(1)
            
        Loop While False    '>>GROUP
    Next
    Application.ScreenUpdating = True
    
End Sub

Open in new window


Note: this code expects that a Sheet2 exists.

For more information on the Do Group, read my article on the technique:
http:/A_2755-Next-Iteration-functionality-in-VB-classic-and-VBA.html
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37804358
Have we changed the question here?

The original file from the original question is attached here with the macro I provided and is working ok. Check it out.
Copy-of-test--1-.xls
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37804370
It appears that this question has been extended to what your other question was. I have already posted a code there to which you have not responded yet.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:aikimark
ID: 37804374
@ssaqibh
Are you referring to your initial comment?
http:#a37799010

Unfortunately, your code doesn't limit itself to "lr_start_transaction" strings.  It would also process the following:
lr_output_message
lr_end_transaction
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37805565
aikimark, That can easily be taken care of by changing

  If Left(cel, 3) = "lr_" Then

to include the complete string. The response to my initial comment was that it was not working and producing an error which I have demonstrated.otherwise.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37806066
@ssaqibh

karinos57 probably encountered the error when he tested your code against a different set of data than he originally posted.
0
 

Author Comment

by:karinos57
ID: 37806135
thanks aikimark,
I am getting this error when I run the code:
 ‘ Run time error ‘91’ Object variable or With block variable not set’
Can you post with the working sample file?  thanks
0
 

Author Comment

by:karinos57
ID: 37806153
@ssaqibh
i extended the question but i tested your solution but it was producing duplicates.  if you look at the last file i posted test2 will show you the result i am looking for.  thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37806159
@karinos57

Is there a Sheet2 worksheet?
0
 

Author Comment

by:karinos57
ID: 37806309
yes, it is there.  is it working for you?
0
 

Author Comment

by:karinos57
ID: 37806323
the code is failing at this line:
strStartAddr = rngFind.Value      'reusing string variable
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37806335
I've included the workbook including the code and the populated Sheet2 after running the code.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37806377
If it failed on that statement, it means that you have a worksheet data configuration where there is at least one lr_start_transaction section without an =http string
0
 

Author Comment

by:karinos57
ID: 37806398
i don't see the workbook that you attached yet.
0
 

Author Comment

by:karinos57
ID: 37806480
okay, i see the issue now as you said.. at least one lr_start_transaction section without an =http string  is missing and you are right about this.  is there a way we can leave it blank for that missing http and go to the next one? i am just trying to avoid this error because i have 100 files to go thru
thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37806567
Don't know why it didn't attach to the prior comment.
test2--1-.xls
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 37809084
No problem.  I never received a notice for your comment.  Sorry for the delay.

Option Explicit

Public Sub CopyStartTrans()
    Dim rngFind As Range
    Dim strStartAddr As String
    Dim rngTgt As Range
    Dim rngPrev As Range
    Dim rngCurrent As Range
    Dim rngItem As Range
    Dim wks As Worksheet
    Dim colRanges As New Collection
    Set wks = ActiveWorkbook.Worksheets("Sheet1")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngFind = wks.Cells.Find("lr_start_transaction(")
    If rngFind Is Nothing Then
        Exit Sub
    Else
        strStartAddr = rngFind.Address
    End If
    Application.ScreenUpdating = False
    Do
        colRanges.Add rngFind
        rngFind.Copy rngTgt
        Set rngTgt = rngTgt.Offset(1)
        Set rngFind = wks.Cells.Find("lr_start_transaction(", rngFind)
    Loop Until rngFind.Address = strStartAddr
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("A2")
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range(rngTgt, rngTgt.End(xlDown))
    rngTgt.Replace "lr_start_transaction(""", vbNullString
    rngTgt.Replace """);", vbNullString
    
    Set rngTgt = ActiveWorkbook.Worksheets("Sheet2").Range("B2")
    Set rngFind = wks.Cells.SpecialCells(xlCellTypeLastCell)
    colRanges.Add rngFind
    For Each rngItem In colRanges
        
        Do  '<<Group
            If rngPrev Is Nothing Then
                Set rngPrev = rngItem
                Set rngCurrent = rngPrev
                Exit Do
            Else
                Set rngPrev = rngCurrent
            End If
            
            Set rngCurrent = rngItem
            Set rngFind = wks.Range(rngPrev, rngCurrent.Offset(0, 1)).Find("=http")
            If rngFind Is Nothing Then
            Else
                strStartAddr = rngFind.Value      'reusing string variable
                rngTgt.Value = Replace(Mid$(strStartAddr, InStr(strStartAddr, "=") + 1), ",", vbNullString)
            End If
            Set rngTgt = rngTgt.Offset(1)
            
        Loop While False    '>>GROUP
    Next
    Application.ScreenUpdating = True
    
End Sub

Open in new window

0
 

Author Closing Comment

by:karinos57
ID: 37815890
many thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now