Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Excel VB To Copy Text In String and Paste In 2nd Worksheet

Posted on 2013-01-09
Last Modified: 2013-01-09

Can someone assist in developing a VB macro that will do the following:

1.  In Sheet1, column AG, there is a string of text and numbers.  The code looks at the string, and copys the text that appears between:  'Context ' & '@' symbol.  Please note there is a space that appears after 'Context' that should not be included in the copy.  The data being copied on average is 7 characters long.

2.  Paste that data in Sheet2, column A

Thank You!!!
Question by:Escanaba
  • 3
  • 3

Author Comment

ID: 38760428
One last note, the code should start in row 2 of column AG on Sheet1 and loop until there is no more data found in that column.  Thanks!
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38760520
Question: does the string 'Context ' always appears, and is it always followed some number of characters later by an '@' symbol?

Author Comment

ID: 38760558
Yes.  The word Context_ will appear in every line that has text in it starting in row 2, and will always have characters following it with an @ symbol appearing after the characters.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 92

Accepted Solution

Patrick Matthews earned 500 total points
ID: 38760610
The following seems to work for me:

Sub DoIt()
    Dim LastR As Long
    With Worksheets("Sheet1")
        LastR = .Cells(.Rows.Count, "ag").End(xlUp).Row
    End With
    With Worksheets("Sheet2").Range("a2:a" & LastR)
        .Formula = "=IF(Sheet1!AG2<>"""",MID(Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2)+8,SEARCH(""@"",Sheet1!AG2," & _
            "SEARCH(""Context "",Sheet1!AG2))-SEARCH(""Context "",Sheet1!AG2)-8),"""")"
        .Value = .Value
    End With
    MsgBox "Done"
End Sub

Open in new window


Author Closing Comment

ID: 38760691
Looks like that did the trick.  Thank you for putting this together!
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38760712
You're welcome :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

791 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