+ Reply to Thread
Results 1 to 27 of 27

Variable Find reference to a column?

  1. #1
    Registered User
    Join Date
    08-18-2006
    Posts
    68

    Variable Find reference to a column?

    Simple question for someone,
    I want to have a find command run and use a varible defined from a column on the same sheet I am performing the find on.
    I can do everything with the exception of defining the column that the variable will work through when it is defining the find name.
    Anyone care to help?
    TIA
    Patrick

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    it always helps for us to assist you if you post your code so that we can see what you are trying to acheive and be able to give you better advice

  3. #3
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Mudraker is right ... your question needs clarification ...
    Are you looking for code along these lines ...
    Please Login or Register  to view this content.
    HTH
    Carim

  4. #4
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    What the macro will be doing is going through a list of names, each name will become a variable in a find command, the find command will run on another sheet and when the name on the other sheet has been found it will copy a set of stats and paste them into the original sheet.
    The part I have no idea how to do is setting up the list and having the code run through all the names on the list and each time making that name a variable.
    Any clearer? I have not done any code for this yet so I cannot post any
    TIA
    Patrick

  5. #5
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Given the fact that all your names are in a range, you can loop in this range ...
    with i as row and j as column
    For i = 1 to whatever
    For j = 1 to whatever
    Cells(i,j).Select
    next j
    next i

    HTH
    Carim

  6. #6
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    And so if I had a statement like
    Dim name As string
    name = activecell

    Making the variable name equal to the current cell selected in the list?
    Correct?
    Patrick

  7. #7
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Yes...that's right

  8. #8
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    One last question, how do I set i as a row variable and j as a column variable??
    Thanks for the excellent help
    Patrick

  9. #9
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    You can do it with

    i = ActiveCell.Row
    j = ActiveCell.Column

    HTH
    Carim

  10. #10
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    And so using this method I could use
    range("Z1").select
    For i = 1 to 20
    For j = 26
    Cells(i,j).Select
    next j
    next i

    Would this work?

  11. #11
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Please Login or Register  to view this content.
    HTH
    Carim

  12. #12
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    So this is what I have BUT I get a type mismatch??
    Any ideas?

    Dim name As String
    name = ActiveCell
    Dim i As Range
    Dim j As Range
    i = ActiveCell.Row
    j = ActiveCell.Column

    For i = 1 To 20
    Cells(i, 26).Select


    Windows("IMPORT BOOK.xls").Activate
    Sheets("Current.xls").Select
    Cells.Find(What:="name", After:=ActiveCell, LookIn:=xlValues, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate
    StartCell = ActiveCell.Offset(0, 0).Address
    EndCell = ActiveCell.Offset(0, 19).Address
    Range(StartCell, EndCell).Copy
    Windows("Core per Hour.xls").Activate
    Sheets("Current").Select
    Range("C1").Select
    Do While Not IsEmpty(ActiveCell)
    ActiveCell.Offset(1, 0).Select
    Loop
    Selection.PasteSpecial Paste:=xlPasteValues
    Next i

  13. #13
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    small correction
    Please Login or Register  to view this content.
    But I would recommend another variable word ... e.g Nam1
    Please Login or Register  to view this content.
    and adjust the find portion too ...

    HTH
    Carim

  14. #14
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Ok well I edited as you suggested but is still gives me a type mismatch for i
    ?
    Should this variable be something different?
    TIA

  15. #15
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Make sure your name instruction appears once you have selected your cell

    Please Login or Register  to view this content.
    HTH
    Carim

  16. #16
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Ok so I have updated and redone some of the code but I still get type mismatch for i variable. Below is the code. Something that also seems to be causing a problem is the line which finds the next blank cell to paste the copied data to.

    Do While Not IsEmpty(ActiveCell)
    ActiveCell.Offset(1, 0).Select
    Loop
    TIA

    Dim Nam1 As String
    Dim i As Range
    Dim j As Range
    i = ActiveCell.Row
    j = ActiveCell.Column

    For i = 1 To 20
    Cells(i, 26).Select
    Nam1 = ActiveCell.Value


    Windows("Core per Hour.xls").Activate
    Sheets("High Value Queue").Select


    Cells.Find(What:="Nam1", After:=ActiveCell, LookIn:=xlValues, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate
    StartCell = ActiveCell.Offset(0, 0).Address
    EndCell = ActiveCell.Offset(0, 19).Address
    Range(StartCell, EndCell).Copy
    Windows("Core per Hour.xls").Activate
    Sheets("High Value Queue").Select
    Range("C1")..PasteSpecial Paste:=xlPasteValues
    Next i

  17. #17
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Dim i,j As Integer

    HTH
    Carim

  18. #18
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Ok so I fixed the problem by changing the i variable to integer instead of range, now I have another problem... my list is in one workbook and my sheet I am searching and copying from is in another workbook, when I select the second workbook it also changes the find variable, is there any way around this??
    TIA
    Patrick

  19. #19
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    LOL... great minds..
    Any ideas on the above problem?

  20. #20
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Hi,

    Try making your find variable Public.
    At the top of your sub try
    Please Login or Register  to view this content.
    This should allow a find on the other workbook.
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  21. #21
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Where would I enter Public?
    I have tried just pasting after the macro name but it gives an error?
    TIA
    Patrick

  22. #22
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    If you have "Option Explicit at the top of your code module the "Public" will go below that.
    Something like this.

    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    And now when I put the code in as instructed I get an error variable not defined for the StartCell = , before I enter the public section everything worked.
    Any ideas?
    Patrick

  24. #24
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Patrick,

    Sorry I assumed you were going from one code to another which the "Public" statement would have worked.
    The following code does a find and paste, but it's in the same workbook.
    Not sure which workbook has the list of name to be found on the other workbook.
    Lets say workbook1 has the list of names and workbook2 has the date to be copied then when you run a code you will have to be active on workbook1 or have it activated in the code. After you get the name you then have the code activate workbook2 for the find and copy. You then reactivate workbook1 for the paste function and the process starts over.


    [code]
    Sub test2()
    Dim Nam1 As String
    Dim i As Integer
    Dim StartCell As String
    Dim EndCell As String

    For i = 1 To 20

    Windows("Core per Hour.xls").Activate''This has the List of names?
    Sheets("High Value Queue").Select

    Cells(i, 26).Select
    Nam1 = ActiveCell.Text

    '''' Activate wokbook2 name '''
    Windows("IMPORT BOOK.xls").Activate
    Sheets("Current.xls").Select

    Cells.Find(What:=Nam1, After:=ActiveCell, LookIn:=xlValues, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False).Activate

    StartCell = ActiveCell.Offset(0, 0).Address
    EndCell = ActiveCell.Offset(0, 19).Address
    Range(StartCell, EndCell).Copy
    '' This switches back to first workbook '''
    Windows("Core per Hour.xls").Activate
    Sheets("High Value Queue").Select

    Range("C1").Select
    Do While Not IsEmpty(ActiveCell)
    ActiveCell.Offset(1, 0).Select
    Loop
    Selection.PasteSpecial Paste:=xlPasteValues
    Next i
    End Sub

  25. #25
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    What I did for a workaround was copied the first find on the first book, pasted it into an empty cell at the top of the second workbook and then it still used it as the variable for the find. This worked for what I needed

    Something else I have been wondering how to do with a sheet is finding a particular value and then a second value further down a sheet and then selecting a range between the two. Any idea how this could be done??
    TIA
    Patrick

  26. #26
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Hi,

    Something like this may help.

    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Works perfect,,, so simple... I love vba

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1