Consulting

Results 1 to 9 of 9

Thread: Searching Cells for String

  1. #1
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location

    Searching Cells for String

    Hi i have a table of data, where there is going down a series of parameters, and going across various readings for these parameters, now i am trying to format the data and move it into another workbook, and i was wanting to use a for loop to search for a string i.e. parameter name and from this activate code to move certain bits from that parameter row into another a workbook

    [vba]Dim MyString As String
    Dim c As Object
    Dim i As Long
    MyString = "Leq"


    For Each c In Sheets("Original").Range("A1:A65536").Cells

    If c = MyString Then i = ActiveCell.Row

    Range("D" & i, "N" & i).Select


    Selection.Copy
    Windows("NA28 Thirds To Table.xls").Activate
    Sheets("NA28 Table Oct Summary").Select
    Range("O" & i).Select
    ActiveSheet.Paste
    Windows("mybook").Activate
    Next

    End Sub[/vba]
    This is what i have at the minute but it doesn't seem to work, i'm not a complete novice programmer but am when it comes to VBA.

    thanks in advance, Dan.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No tested, but try this

    [vba]

    Dim MyString As String
    Dim c As Range
    Dim i As Long
    MyString = "Leq"

    With Sheets("Original")

    For Each c In .Range("A1:A" & .Cells(.Rows.Count, "A").xlUp.Row).Cells

    If c = MyString Then i = c.Row

    .Range("D" & i, "N" & i).Copy _
    Workbooks("NA28 Thirds To Table.xls").Sheets("NA28 Table Oct Summary").Range("O" & i)
    End If
    Next
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location
    No joy so far, am getting a block if error, also due to the nature of the orignal data there are several blank rows where the end of one test's worth of data ends and the next begins so the code to find the last blank row is redundant. its a rather annoying one this routine.

  4. #4
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location
    [VBA]

    Sub test()
    Dim MyString As String
    Dim c
    Dim i As Long
    MyString = "Leq"


    For Each c In Sheet1.UsedRange.Cells

    MsgBox c.Address

    If c = MyString Then
    i = ActiveCell.Row
    Range("D" & i, "N" & i).Select

    Selection.Copy

    Windows("NA28 Thirds To Table.xls").Activate
    Sheets("NA28 Table Oct Summary").Select
    Range("O" & i).Select
    ActiveSheet.Paste

    Windows("mybook").Activate
    Next

    End Sub
    [/VBA]
    Nothing is impossible, just it hasn't been thought of yet.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, my bad

    [vba]

    Dim MyString As String
    Dim c As Range
    Dim i As Long
    MyString = "Leq"

    With Sheets("Original")

    For Each c In .Range("A1:A" & .Cells(.Rows.Count, "A").xlUp.Row).Cells

    If c = MyString Then

    i = c.Row

    .Range("D" & i, "N" & i).Copy _
    Workbooks("NA28 Thirds To Table.xls").Sheets("NA28 Table Oct Summary").Range("O" & i)
    End If
    Next
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location
    Thanks guys between the two codes i modified my original code and got it to work as such

    [VBA]Dim MyString As String
    Dim c
    Dim i As Long
    MyString = "Leq"

    For Each c In Sheets("Original").Range("A1:A65536").Cells

    If c = MyString Then
    i = c.Row
    Range("D" & i, "N" & i).Select

    Selection.Copy
    Workbook("NA28 Thirds To Table.xls").Sheets("NA28 Table Oct Summary").Range ("O" & i)
    ActiveSheet.Paste
    Windows("mybook").Activate

    End If
    Next

    End Sub[/VBA]

    now i've just got to move the rest of the data, but having got this done, i can use it as a template.

  7. #7
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location

    Function

    You should try and use this code in a function if you are doing it over many sheets/different search strings.

    [VBA]
    Function myParseFindDate (mySheetName as string, myRange as string, mySearchStr as string)

    Dim c
    Dim i As Long

    For Each c In Sheets(mySheetName).Range(myRange).Cells

    If c = mySearchStr Then
    i = c.Row
    Range("D" & i, "N" & i).Select

    Selection.Copy
    Workbook("NA28 Thirds To Table.xls").Sheets("NA28 Table Oct Summary").Range ("O" & i)
    ActiveSheet.Paste

    Windows("mybook").Activate

    End If
    Next

    End Function

    'Use this sub to test the code.


    Sub test

    call myParsheFindDate ("Original","A1:A65536","Leq")

    End Sub

    [/VBA]
    Nothing is impossible, just it hasn't been thought of yet.

  8. #8
    VBAX Regular Ebrow's Avatar
    Joined
    May 2007
    Posts
    67
    Location
    [VBA]
    Sub test

    call myParseFindDate ("Original","A1:A65536","Leq")

    End Sub
    [/VBA]
    sorry code at end should read like this.
    Nothing is impossible, just it hasn't been thought of yet.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Rather than looping each cell, it's usually more efficient to Find each value and run the code using the found range
    [VBA]Option Explicit

    Sub DoCopy()

    Dim MyString As String
    Dim c As Range
    Dim i As Long
    Dim ws As Worksheet
    Dim FirstAddress As String

    Set ws = Workbooks("NA28 Thirds To Table.xls").Sheets("NA28 Table Oct Summary")
    MyString = "Leq"

    With Sheets("Original").Columns(1)
    Set c = .Find(MyString)
    If Not c Is Nothing Then
    FirstAddress = c.Address
    Do
    i = c.Row
    Range("D" & i, "N" & i).Copy ws.Range("O" & i)
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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