Solved

Find specific word using VBA

Posted on 2012-04-02
30
415 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@karinos57

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

Author Comment

by:karinos57
Comment Utility
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
Comment Utility
yes my file is actually in excel format
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
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
Comment Utility
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
Comment Utility
please post an example of what you need to see in Sheet2, based on your posted workbook data
0
 

Author Comment

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

Expert Comment

by:aikimark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
@karinos57

Is there a Sheet2 worksheet?
0
 

Author Comment

by:karinos57
Comment Utility
yes, it is there.  is it working for you?
0
 

Author Comment

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

Expert Comment

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

Expert Comment

by:aikimark
Comment Utility
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
Comment Utility
i don't see the workbook that you attached yet.
0
 

Author Comment

by:karinos57
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
many thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

763 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

12 Experts available now in Live!

Get 1:1 Help Now