+ Reply to Thread
Results 1 to 13 of 13

VBA needed to extract data from specific cells in different worksheet

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    VBA needed to extract data from specific cells in different worksheet

    Hello. I am new to this, and trying to figure out a VBA code that will work for my set of data. I have attached a much smaller version of the data I am working with.

    I am trying to make a 'tool' sheet in Excel that can search all of the data from the 'raw data' sheet and return specific cells from that 'raw data' sheet. Specifically, in the attached workbook, I want users to be able to enter values into the yellow highlighted cells (Test #s and Class rank) on the 'tool' sheet, press the "Go" button, and from there I need the VBA to:

    1. Go to the 'raw data' sheet and first search for the row in Column A that matches the Test # given in the yellow highlighted cell. Secondly, if the correct test # is found, I need the code to also search in Column B for the class to also match the value that was entered in the yellow highlighted cell on the 'tool' sheet.

    2. Once both columns match, I want the VBA code to go over to column E and to copy the cell directly across from the first row that has both correct values, and also to copy the second cell. For example, if test #1 and freshman were entered in the yellow cells, the code would find that row 2 matches the requirements, so it would go over and copy E2 and E3 on the sheet.

    3. Lastly, I want the code to go back and paste the 2 copied cells in the specified green highlighted cells on the 'tool sheet'.

    4. In the case that the user enters something that does not exist, such as "senior" in the yellow class cell, I also need to code to come back and say something like "invalid entry".

    Thanks so much for any help you can give!!!!!
    Mindy
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy

    This code is in the attached. It appears to do as you require. In this scenario you don't require this as I've limited their choices to valid entries
    4. In the case that the user enters something that does not exist, such as "senior" in the yellow class cell, I also need to code to come back and say something like "invalid entry".
    Let me know of issues.
    Please Login or Register  to view this content.
    PS: I failed to mention that Sheet3 can and probably should be hidden.
    Attached Files Attached Files
    Last edited by jaslake; 08-03-2011 at 06:52 PM. Reason: add PS
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Thanks so much jaslake. This works perfectly! I have another question for you. I have a larger data set that I am essentially doing the same thing with. I ended up combining 2 cells in column A of the data sheet so that I only had to find one value in the code. Below is my code (very basic!). It works correctly, but it always goes to the last row (#2861) even when it finds what it is looking for much earlier in the loop. Also, when it is finished, I am looking at the bottom of the sheet, instead of at the top of the "datatool" sheet. Do you know what I can enter into the code to fix these 2 problems? Also, any easy suggestions to make this code a little better (i.e. naming variables, using range instead of cells, etc.)?

    Sub Button3_Click()

    Sheets("DataTool").Select

    For val1 = 67 To 2861 Step 1

    Cells(val1, 1).Select

    If Cells(val1, 1) = Sheets("tool").Range("G4").Value Then
    ActiveCell.Offset(0, 18).Select
    Range(ActiveCell, ActiveCell.Offset(3, 0)).Copy

    Sheets("Tool").Select
    Range("C16").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

    Else
    End If
    Next val1

    End Sub

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy

    Please wrap your code in code tags.

    Rule #3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Select your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button). The result will appear like this in the post window:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi, I didn't realize I needed to do that. Thanks for letting me know for the future. Below is the same code in code tags.


    Please Login or Register  to view this content.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy

    This is probably a NEW issue and should be started as a NEW thread. I'm hoping the Moderators will give us (you AND I) a pass on that.

    Please post a sample of your file, explain what you wish to do...I'm kinetic learner...do much better with visual clues. Show me.

  7. #7
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Thanks John. I've attached a file...it is the same as the first file, except I have added more data. I need to change the code so that when the macro ends, I am looking at the results on the tool spreadsheet. As it is now, it ends and I am looking at cell 73 (which is the last cell in the search range). Also, I notice that even if the code finds what it is looking for early in the loop (say row 23), it still runs until the end ...to row 73...I want the code to tell it to stop after it finds the search criteria.

    Thanks again for your help!
    Attached Files Attached Files

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy
    Is there a reason you decided to loop through every record to find a match? Is there also a reason for adding a new Column A (Combined) in Raw Data?

    In the attached, I've deleted that "Combined" Column, reinstated Data Validation in Tool worksheet. copied the code into the workbook that I previously provided you, assigned that macro to the Go button and it ran out of the box.

    Let me know of issues...and let me know if you're not satisfied with this approach.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi John,
    I am actually using your approach, but I am trying to learn VBA on my own in the process. I was trying to see if there is a way to fix the issues I mentioned for future use. To scroll to the top, I found that I could use Application.goto, but I'm still working on figuring out how to make that code stop when it finds what it needs.

    Thanks for all of your help
    Mindy

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy

    After I wrote my previous response I wondered if perhaps you were doing just that. So, this one line added to your code "fixes" both of your issues
    Please Login or Register  to view this content.
    I'd much prefer you use the Find method and avoid all the looping.

  11. #11
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi John,

    I have a new issue related to extracting whole ranges of data that I was hoping you could help me with. I still want to find the rows that match the 2 criteria on the "tool" sheet (matching test # and class). This time though, I want to have another button on the "tool" sheet that says "show raw data". When I press it, I want it to:

    1. find all of the rows that match the criteria
    2. select all of the data associated with those rows (columns A-C)
    3. Past the data selection in the worksheet called "specific data". For example, if the criteria selected are Test # 1 and Class: Freshman, I want the VBA to select cells A2:C14 and to paste those.

    **When I press the "Show Raw Data" button several times, I assume there may be overlap on the data from the previous paste. Therefore, is there a way to erase the contents of the sheet before a new range is pasted so that only the new raw data is showing?

    Many thanks!!
    Mindy

  12. #12
    Registered User
    Join Date
    08-01-2011
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA needed to extract data from specific cells in different worksheet

    Forgot the attachment. Here it is.
    Attached Files Attached Files

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA needed to extract data from specific cells in different worksheet

    Hi Mindy

    This code has been added and appears to do as you requested
    Please Login or Register  to view this content.
    Let me know of issues.
    Attached Files Attached Files

+ 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