+ Reply to Thread
Results 1 to 9 of 9

Pulling Range of Cell Values Based on another Cell's Value

  1. #1
    Registered User
    Join Date
    01-14-2012
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2000
    Posts
    16

    Pulling Range of Cell Values Based on another Cell's Value

    I am new to using Macros in excel. I am trying to figure out how to pull a range of cell values into different range of cells. The cell values pulled will depend on the value of another cell. Below is an example of code I made to pull the cells using multiple lines of code.

    Please Login or Register  to view this content.

    Is there a way I can do it in fewer lines of code? For example I tried writing it like shown below and it did not work:

    If Range("Q2").Value = "A" Then
    Range("R2:S5").Value = Range("A2:B5")

    Also, would it be possible to pull values from A2:B5 on Sheet2 into cells R2:S5 on Sheet1?

    Thanks in advance for the help!
    Last edited by Freek; 01-13-2021 at 10:28 AM.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Please post code within code tags.Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html
    ______________________

    This should work, as long as the ranges are of same size (rows/cols).
    Please Login or Register  to view this content.
    If you need from different sheets then:
    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    01-14-2012
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Thank you Millz! That was a simpler fix than I was expecting.

    I am trying to understand why .Value is needed after the larger range you showed me, but not needed for the single cell as I had it.

    While this may be my best option, I am trying to figure out how to expand this for many potential cell value options. In my above example, I had "A" and "B" but I may need to do this for 50 plus letters and numbers. As well as ten different sections where the letters and numbers can be manually selected. Each letter/number would have two columns where their data is stored and the data for each ranges from row 2 to row 36. When the data is copied from the columns on sheet2 it is pasted all into columns B&C and each set of data is copied below the last set of data. I have included an example of what I am talking about, but I only included letters "A" through "I" versus the 50 plus options I would like to include. I also only have three selection options instead of the 10 I would like to include.

    Is it possible to simplify the code instead of having the number of lines of code I will need to do this the current way?


    Please Login or Register  to view this content.

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Pulling Range of Cell Values Based on another Cell's Value

    When you have that many "options", or in terms of code, If-else's, you may consider using Select Case. That would be preferred if there's no pattern to your algorithm.

    Fortunately, I can see there's a pattern for your case, since you're always grabbing from the same columns to the same columns, you may try this code:
    Please Login or Register  to view this content.
    .Cells refer to a single cell; in this code, it refers to row 2, and whichever column var a represents.
    .Resize resizes the preceding "referenced range", which was a single cell, into a range of 35 rows and 2 columns.
    Var a is calculated using the ASCII code, since your conditions are all "A" to "I", using the ASCII code we are able to use a pattern to determine the ending column number we want.

  5. #5
    Registered User
    Join Date
    01-14-2012
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Millz, you are awesome! Thank you!

    I need to play with it more to understand how it works, but it seems to work great so far.

  6. #6
    Registered User
    Join Date
    01-14-2012
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Hello Millz, I set it up to use the whole alphabet and that worked great! I also tried using integers and symbols as the value in cells A30, B30, C30 etc. It appears this code does not like integers and symbols. Is there a way to change the code to take either integers or symbols as an input in those cells? Instead of using integers, I tried using the names for the integers: zero, one, two etc. and instead of using the full name, it used only the first letter for each, "Z" for zero.

    I also noticed that it does not differentiate between upper and lower case letters, is there a way to adjust the code for it to distinguish the difference?

    Thank you again for your help!

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Pulling Range of Cell Values Based on another Cell's Value

    The code is currently changing the letters to Upper Case, with the function UCase(), because if you looked into what ASCII codes are and how it works, you would understand why.

    If you need it to work for lower case letters and special characters as well, it would require quite a substantial change to the code, which I cannot prepare now (I'm on my phone). Meanwhile, show what your code does now and also what else you plan to have the code do.

  8. #8
    Registered User
    Join Date
    01-14-2012
    Location
    Arizona, United States
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Millz, I have attached a two worksheets with detail around what I am doing.
    I am using the drop downs in A1, C1, E1, G1, I1, K1, M1 and O1 and selecting values in each. The code you provided is slightly modified to pull data from sheet 2 for each value selected.
    Upper case and lower case letters and symbols have different data sets associated with them. So far I have the data for the upper case letters on Sheet 2 and will eventually have the other data. I am hoping to be able to select a value(s) from a drop down and click the generate star and have it pull the data from Sheet 2 to the cells below the selected values on Sheet 1. The vba code is modified to work for data sets of varying lengths (number of cells in a column, but always two columns) but so far all the data sets are the same size.

    If it helps the code to interpret the selected value, I could use names for the numbers and symbols instead of the numbers or the symbols themselves. For example: Zero instead of 0. The code would then need to be able to tell the difference between a capitol Z and Zero, since replacing 0 with Zero just causes the data for Z to be pulled from sheet 2.

    If the code were to recognize the difference between Z and Zero, then I might be able to differentiate between upper and lower case letters by adding "Upper Case" or "Lower Case" to the name of each value. For example: "G Upper Case" and "g Lower Case"

    The file "Messin With Macros4" has the natural numbers and symbols and gives an error when one of those is selected and the generate button is pushed.

    The file "Messin With Macros5" has the names for the numbers and symbols and will return the values for the first letter in the name of the value selected.

    Thank you again for your help with this as I struggle through trying to understand bits of code.
    Attached Files Attached Files

  9. #9
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Pulling Range of Cell Values Based on another Cell's Value

    Try this updated code:
    Please Login or Register  to view this content.
    It should work with the data structure in your file Messin With Macros4.xlsm.
    In Sheet2, you need to change the column sequence from 1,2,3, ..., 0 to 0, 1, 2, ..., 9 instead, since that's how the ASCII code runs.
    If you adjust any of the column sequence then it won't work properly anymore. For A-Z, a-z, and 0-9 columns at least, they should not be rearranged unless you know how to edit those numbers that I've hard-coded in.
    It currently works up to the ' / ' symbol column, anything else will just return the current ♥ symbol's column number, regardless of the symbol or how many columns beyond that.

    Quote Originally Posted by Freek View Post
    as I struggle through trying to understand bits of code.
    Again, I should repeat that you need to know what ASCII codes mean and do, and how I am making use of them here. In short, I am using it to determine which column to pick data from.

    This is about as far as I go, any more modifications is up to you.
    Last edited by millz; 01-20-2021 at 01:48 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Return values to specific columns based on range of cell values
    By sbrt10 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-27-2019, 03:15 AM
  2. [SOLVED] Pulling row data to new worksheet based on values on row cell
    By afrazier25 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-26-2015, 02:50 PM
  3. Sum cells in a range based between two equal cell values in another range
    By sshone1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-09-2013, 08:12 AM
  4. Need help making a macro to copy range of values based on cell values.
    By zolton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-23-2013, 08:58 AM
  5. Pulling a Specific Cell when Pulling a Tabel from Web Based Data
    By Zallen89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2013, 05:39 PM
  6. Range Based on Cell Values
    By Clark in forum Excel General
    Replies: 2
    Last Post: 05-06-2009, 02:56 AM
  7. [SOLVED] formula for named cell/range based on cell values
    By alex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2005, 10:05 AM

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