[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Find specific word using VBA

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
karinos57
Asked:
karinos57
  • 12
  • 12
  • 5
  • +1
1 Solution
 
Saqib Husain, SyedEngineerCommented:
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
 
wchhCommented:
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
 
Saqib Husain, SyedEngineerCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
aikimarkCommented:
@karinos57

Is your file actually an Excel workbook or some other file format?
0
 
karinos57Author Commented:
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
 
karinos57Author Commented:
yes my file is actually in excel format
0
 
aikimarkCommented:
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
 
karinos57Author Commented:
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
 
aikimarkCommented:
please post an example of what you need to see in Sheet2, based on your posted workbook data
0
 
karinos57Author Commented:
thanks.  Please see the sheet called Desired Results
test2.xls
0
 
aikimarkCommented:
Please update your [Desired Results] worksheet with the step names.  You have a different number of step names than you nave URL lines.
0
 
karinos57Author Commented:
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
 
aikimarkCommented:
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
 
Saqib Husain, SyedEngineerCommented:
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
 
Saqib Husain, SyedEngineerCommented:
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
 
aikimarkCommented:
@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
 
Saqib Husain, SyedEngineerCommented:
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
 
aikimarkCommented:
@ssaqibh

karinos57 probably encountered the error when he tested your code against a different set of data than he originally posted.
0
 
karinos57Author Commented:
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
 
karinos57Author Commented:
@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
 
aikimarkCommented:
@karinos57

Is there a Sheet2 worksheet?
0
 
karinos57Author Commented:
yes, it is there.  is it working for you?
0
 
karinos57Author Commented:
the code is failing at this line:
strStartAddr = rngFind.Value      'reusing string variable
0
 
aikimarkCommented:
I've included the workbook including the code and the populated Sheet2 after running the code.
0
 
aikimarkCommented:
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
 
karinos57Author Commented:
i don't see the workbook that you attached yet.
0
 
karinos57Author Commented:
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
 
aikimarkCommented:
Don't know why it didn't attach to the prior comment.
test2--1-.xls
0
 
aikimarkCommented:
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
 
karinos57Author Commented:
many thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 12
  • 12
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now