+ Reply to Thread
Results 1 to 18 of 18

Select range of data based on values in colum A

  1. #1
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Select range of data based on values in colum A

    I have several large sets of data in one spreadsheet and would like to split them into different sheets. For each set of data, the values within column A are identical and amount to approximately 40,000 rows for each data set.

    I have found a macro which compares the active cell to the next cell and moves down the rows until the numbers change. At this point it should stop, but it doesn't. The macro is:

    Please Login or Register  to view this content.
    Even if this worked, I am sure it would take an age to go through all 40,000 rows for the data set. And a lifetime for all 2,000 data sets that it needs to go through.

    Can anyone recommend a quick way of exporting each data set into a different sheet? If not please fix the macro above.

    Many thanks
    Attached Files Attached Files
    Last edited by eddiej90; 01-06-2016 at 09:36 AM. Reason: Attachment added

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Select range of data based on values in colum A

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    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 here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    I modified this code per the attached spreadsheet and will upload a file that contains the macro w/ a button

    Please let me know if this worked for you and if not why. If it did not work, please upload a sample sheet of data (just ew of your thousands of rows).

    If anyone else came up with a solution that is better, please feel free to offer any criticism of my code that will help me help others more effectively. I know that it's best to avoid Copy and Paste methods whenever possible; however, this isn't a macro that requires a great deal of speed since it will be performed once.

    '/Assumptions:
    '/Each data point is contained w/i one row (true based on spreadsheet)
    '/Data begins at row 2 with row 1 being the title or row (see StartRow, which may be changed as-needed) -- true based on spreadsheet
    '/Requires the macro be created on the worksheet that contains the data (may be modified from this req. easily)
    Please Login or Register  to view this content.
    Last Updated Code: 10:55 AM (Eastern Std. Time on 1/6/2016) but the 10:46AM update should work -- all I did was change some formatting, remove an unnecessary variable, change the Double datatype to Long datatype (which shouldn't really matter either way b/w Double & Long), and clean up some formatting.
    Last edited by joe31623; 01-06-2016 at 11:55 AM.

  4. #4
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    Hi Joe,

    My aim is in fact to actually copy and paste each data set into a separate worksheet, which will run a model and export results. Then move onto the next data set. I just said split into different sheets for ease of explanation. I can edit it to export to the correct worksheet but please find attached a sample of the data to my original post.

    Many thanks

  5. #5
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Eddie,

    Do you mean separate workbook or worksheet? Either solution can easily be accomplished using my code above. I'll take a look at the sample and get back to you with any questions.

    Joe

  6. #6
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    Sorry, meant workbook

  7. #7
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Eddie,

    I modified the code above to work. Not sure why but I'm having issues uploading the spreadsheet so let me know if you can copy and paste the above code and get it to work.

    Should take me 1/2 hr or less to create another macro to create workbooks for each sheet. I'm on it.

    Joe

  8. #8
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    Hi Joe,

    The macro you've created is great, however it does stop with a debug error after splitting two data sets at this point: DataSetRowEnd = r - 1

    I'll just explain my task to you quickly.

    I have a huge set of data that needs running through an excel model, once for each meter and then the results are exported to a separate workbook.

    So, I have three workbooks named "Import", "Model" and "Export".

    My import file has approximately 1million lines and I have about 34 of these files. Each data set has a varying number of lines around the 40,000 region.

    Therefore, I would like to run the macro to do the following:
    Import one set of data to the model;
    Run the model;
    Export the results to the "Export" workbook;
    Import the next set of data to the model and so on.

    I have developed macros to run the model and export the data but I was struggling with importing each set of data one after the other. That's where you've come in but my coding skills are nowhere near as good as yours and I'm finding it difficult to incorporate your code to work within my macro.

    Many thanks for your help

  9. #9
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    Hi Joe,

    The macro you've created is great, however it does stop with a debug error after splitting two data sets at this point: DataSetRowEnd = r - 1

    I'll just explain my task to you quickly.

    I have a huge set of data that needs running through an excel model, once for each meter and then the results are exported to a separate workbook.

    So, I have three workbooks named "Import", "Model" and "Export".

    My import file has approximately 1million lines and I have about 34 of these files. Each data set has a varying number of lines around the 40,000 region.

    Therefore, I would like to run the macro to do the following:
    Import one set of data to the model;
    Run the model;
    Export the results to the "Export" workbook;
    Import the next set of data to the model and so on.

    I have developed macros to run the model and export the data but I was struggling with importing each set of data one after the other. That's where you've come in but my coding skills are nowhere near as good as yours and I'm finding it difficult to incorporate your code to work within my macro.

    Many thanks for your help

  10. #10
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Eddie,

    I believe the problem is due to the fact that the datatypes I initially defined for some of the variables are Integers instead of a double or long. The reason that causes issues is that Integers can only hold values so big and your number of rows exceeds that. I think you need a long datatype but I'll make it a double just in case (I'm a bit new to VBA). I'll change them and post a follow-up when updated.

    Ok, I'll abandon the sub that creates individual workbooks per each sheet from the workbook that has all the data (for now).

    -Multiple datasets are on each of the 34 workbooks, correct?

    If you have 34 files, it would be easy to loop through all Workbooks in a given directory with a macro to do this all at once. As you can see, I can easily split the dataset into worksheets per Meter ID; however, I am wondering if it would be beneficial to do the following:
    For I = 1 To 34 (...so to say)
    1: Open Ith workbook (All workbooks stored in the same directory)
    2: Split each dataset in Ith workbook N worksheets (where, of course, there will be N datasets in the Ith workbook) ... then there will be N + 1 total worksheets in the Ith workbook)
    ----->Is it possible the Meter ID is not unique within one workbook?
    ----->Is it possible the Meter ID is not unique across all workbooks?
    3: Create a Subfolder in the directory 34 worksheets are stored
    4: In that Subfolder, create N workbooks that contain one set of data per workbook
    5: Loop over all 34 workbooks

    ...the result would be N(I = 1) + N(I = 2) + ... + N(I = 34) workbooks

    ...then it would be easy to run the model on the subdirectory that contains workbooks which have individual datasets


    Again, I'll post again when I've updated what I expect will solve the error mentioned above; however, to avoid future errors, I'd like to know if the above solution would be beneficial and the following.
    ----->Is it possible the Meter ID is not unique within one workbook?
    ----->Is it possible the Meter ID is not unique across all workbooks?

    Joe
    Last edited by joe31623; 01-06-2016 at 11:43 AM.

  11. #11
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Eddie,

    Ok, try the updated code above and comment on the potential solution I listed above.

    Please let me know how the code I wrote works. If there are any errors, please cite the error number in addition to the line that caused the error.

    Joe

  12. #12
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    Joe,

    All the data was originally in one CSV file with about 34million lines. I have split this using some simple software into 34 files of 1million lines. Therefore the meters at the end of each file follow on through to the beginning of the next file. So in short, there are about 33 meter IDs that cross over across files (I was just going move these manually into another file).

    If I understand where you are coming from, then it would be great if I could run the whole data set with all 34 files, but splitting these files would result in about 2,000 files.

    All meter IDs should be grouped together and unique.

    Ed

  13. #13
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    That code works great, I'll try to incorporate it into the macro I currently have.

  14. #14
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    If there are about 33 unique MeterIDs per file and each MeterID is unique, what we can work towards (with minimal effort) once this works is (modifying what I had above):

    If you have 34 files, it would be easy to loop through all Workbooks in a given directory with a macro to do this all at once. I am wondering if it would be beneficial to do the following:

    Loop over all workbooks in a directory w/ the following structure to
    ->For I = 1 To 34 (...so to say), where I denotes the Ith workbook
    ->->
    ->Next I
    1: Open Ith workbook (All workbooks stored in the same directory)
    2: Split each dataset in Ith workbook into N worksheets (where, of course, there will be N datasets in the Ith workbook) ... then there will be N + 1 total worksheets in the Ith workbook, including the Worksheet that contains all of the data)
    ----->Would you need to loop over the workbooks in a specific order so the data is sequentially fed into your Model workbook?
    3: Loop over All Worksheets in Ith Workbook that does not contain all data w/ a structure:
    ->For j = 1 To N Worksheets Created with the Macro we have developed
    ->->Copy And Paste data into Model (perhaps test for j = 1 and j = N to see if we need to concatenate last/next WB's data to run the model)
    ->Next j
    4: Run Model, Export Data (already developed)
    Last edited by joe31623; 01-06-2016 at 12:31 PM.

  15. #15
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Ed,

    I'm glad the code works. Send me a PM or respond to this thread if you're interested in developing this further per what I wrote above or anything else. Also, if you can share the application details, when you have time, please PM me.

    I'll check back on this later tonight or tomorrow unless I notice that you have an issue here within the next few minutes.

    Take Care,

    Joe

  16. #16
    Registered User
    Join Date
    01-06-2016
    Location
    Derbyshire, England
    MS-Off Ver
    2013
    Posts
    18

    Re: Select range of data based on values in colum A

    The macro is working great but I'm at the point now where its a little time consuming to do all these 34 partition files.

    Currently I am opening the Nth partition file, copying and pasting data into the "Import" file, running the macro which dumps results into an "Export" file. Then saving this Export file as "ExportN".

    What do I need to add to the beginning and end of the macro in order to loop through all 34 files? I have processed up to the first 8 so far.

    The partition files are located in C:\Users\User\Desktop\Pieces and are named Piece_N.csv where N is the Nth file (N=9 to 34 for the files left to analyse)
    The export files need to be named ExportN.xlsx and are saved into C:\Users\User\Desktop\Outputs
    The original Export file needs re-opening after I do the save as.

    Many thanks

  17. #17
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    I can take a look at this problem later tonight or tomorrow morning, but I'll need some more information. I think all I need to know is:
    -1: where to paste
    --1a: what cell is the beginning of where you want to paste the data when you run your model in the "Import" file?
    --1b: what is the sheet name that contains the cell referenced in (1a)
    --1c: what is the exact workbook name (with extension -- .xlsm, etc.)?
    -2: When the model runs, where does the macro you have output the results?
    --2a: I need to know the range of cells the results of your model output (ex: cells from data A1:A4023 output to cells B1:B4023 or B1:B10, or B1:Variable Cell but specify that there are no spaces b/w B1 and the last part of the data)
    --2b: remember to specify the sheet name/sheet name format, if different, or if it's the same sheet name referenced in (1b)
    --2c: workbook name answered above so ignore 2c, assuming the output of the macro is mapped to the same workbook

  18. #18
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Select range of data based on values in colum A

    Oh... I'll also need to know the name of your macro that runs the model b/c I'll have to run the macro from another workbook. In order to do that, I'll need to use some code similar to:
    Please Login or Register  to view this content.

+ 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. [SOLVED] How to Select Worksheet and Cell/range based on ComboBox Values
    By ckarlest in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2014, 10:54 AM
  2. Macro to sum values in one column based on data in another colum while ignoring blanks
    By The Machinist in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-15-2012, 03:25 PM
  3. [SOLVED] Colum count based ón values In colum a and b
    By 2001jesper in forum Excel General
    Replies: 16
    Last Post: 11-05-2012, 03:28 PM
  4. Select unknown range and sorte by colum
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-13-2009, 09:10 AM
  5. How to shift range of values from a colum to another colum at a programmed cell.
    By lolypop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2007, 06:24 PM
  6. [SOLVED] How to select a range based on values in 1 column
    By ForestRamsey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2005, 12:05 PM

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