+ Reply to Thread
Results 1 to 10 of 10

Loop through column to cut and paste data

  1. #1
    Registered User
    Join Date
    09-18-2019
    Location
    London, England
    MS-Off Ver
    MS Office Profesional Plus 2010
    Posts
    12

    Loop through column to cut and paste data

    Hello everyone,

    I would like your support on the below please. As you will see I'm not an experienced VBA user, rather I would say I'm beginner.

    So I was wondering if you can help me with a simple issue for the experienced users. I have a spreadsheet with +150k rows from which on column L I want the code to go through each cell and when it finds the value "BPXN" then to cut the line(s) underneath it and paste them to the right in specific columns.

    For example :
    If in cell L350 finds the value "BPXN" and the value in cell L351 is different from "BPXN" then to cut the values from L351 to W351 and paste them to X350 to AI350. After that if the value in cell L352 is also different from "BPXN" to cut the values from L352 to W352 and paste them to AJ350 to AU350 and so on. In other words for each row that it finds under the "BPXN" until it finds again the next "BPXN" to cut them and paste them 12 columns after the previous paste.
    I have attached an example file in which I think I'm explaining what exactly I'm looking for.

    The code that I have below is from what I recorded while doing this and from what I could find in the web. So don't expect something like quality because as I said I'm quite a beginner.
    I solved my problem with 2 different recordings of macro but it was good for 100 rows now that I have 150.000 rows to deal with I'm making mistakes and I have to do everything from the beginning some times.

    Please Login or Register  to view this content.
    I really hope that someone will be able to help me. Thank you in advance for using your time to help me.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    05-09-2021
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    148

    Re: Loop through column to cut and paste data

    Hello Markdk,

    It will be easy for anyone in here to help you achieve what you are asking for. But, it seems like you have thought about this the wrong way, especially when it comes to saving the cell content 12 columns to the right...

    Can you try to describe what you are trying to achieve in the end and I can help to propose a better solution and include the VBA solution for that.

  3. #3
    Registered User
    Join Date
    09-18-2019
    Location
    London, England
    MS-Off Ver
    MS Office Profesional Plus 2010
    Posts
    12

    Re: Loop through column to cut and paste data

    Hello MrChulo,

    first of all thank you for your time. I will try to describe it as better as I can.
    In column L the cells have values from which I use a specific one as my breaking point. That value is "BPXN".
    Now under this value in the next cell there might be 1,2,3 or 4 cells that don't have this value. Those cells and the extend of them up to column W I want to cut them and paste them next to the cell that was the "BPXN" after 12 columns.
    To be more specific :

    Cell L10="BPXN" then
    if Cell L11<>"BPXN" select the range L11:W11 cut this range and paste it 12 columns after cell L10, in the range X10:AI10 then
    if Cell L12<>"BPXN" select the range L12:W12 cut this range and paste it 12 columns after the previous paste, in the range AJ10:AU10 then
    if Cell L13<>"BPXN" select the range L13:W13 cut this range and paste it 12 columns after the previous paste, in the range AV10:BG10 then
    if Cell L14<>"BPXN" select the range L14:W14 cut this range and paste it 12 columns after the previous paste, in the range BH10:BS10 and continue like this.


    Now because I don't know how many cells under the "BPXN" value will be different from that I want to check each time the value in each cell in column L.
    Normally after the "BPXN" I should have at least 2 rows that the values in the cells in column L will be different than "BPXN" so the above cut and paste should happen the first 2 times.
    If the values in the cells after "BPXN" in column L will be different for 3 or 4 rows then the above cut and paste should take place 3 or 4 times respectively.
    Ones this cut and paste will take place the rows that had these values will be deleted. So in the above example rows 11 to 14 should be deleted. After that to start this sequence again from the next cell in column L.
    And I mean Cell L11 because we deleted the rows 11 to 14. So again it should go like this :
    if Cell L11="BPXN" then
    if Cell L12<>"BPXN" select the range L12:W12 cut this range and paste it 12 columns after cell L11, in the range X11:AI11 then
    if Cell L13<>"BPXN" select the range L13:W13 cut this range and paste it 12 columns after the previous paste, in the range AJ12:AU12 then
    if Cell L14<>"BPXN" select the range L14:W14 cut this range and paste it 12 columns after the previous paste, in the range AV13:BG13 then
    if cell L15="BPXN" delete rows 12 to 14 and start over. But then it will start from Cell L12.
    I hope it make more sense now.

    My aim is to reduce the number of the rows that I currently have ( at the moment 150.000 ) to at least 1/3 of those so then I can apply a table to this data which I cannot do at the moment because of the structure of the csv file that I'm downloading.
    Last edited by Markdk; 05-09-2021 at 09:47 AM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Loop through column to cut and paste data


    Hi,

    so it should be better, faster, more efficient whatever, to start from the csv text file rather than after importing it …

  5. #5
    Registered User
    Join Date
    09-18-2019
    Location
    London, England
    MS-Off Ver
    MS Office Profesional Plus 2010
    Posts
    12

    Re: Loop through column to cut and paste data

    Hello Marc L,

    I wish it was that easy. You see the place where I'm downloading the csv has the restriction of 50 entries at a time which then returns in csv with 200 to 250 rows according to the data. Then I have to copy and paste the data from the csv in my file and then do what I'm trying to do here.

  6. #6
    Forum Contributor
    Join Date
    05-09-2021
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    148

    Re: Loop through column to cut and paste data

    Hi Markdk,

    What I meant with my question was more, what is the end goal of this operation? To pasting things into columns, not even knowing how many columns it will be is not a good idea. How are you going to read the data after, what are you going to do with it. It seems structurally not productive the way you want it.

    But trusting that you know best regarding this so providing a solution (might be many) as requested.

    I have tested this with your example and it is working fine. In your example the data started on row 15 (lngRow) and on Col L (intDataCol), these can be changed to fit your need.

    Good luck!


    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-18-2019
    Location
    London, England
    MS-Off Ver
    MS Office Profesional Plus 2010
    Posts
    12

    Re: Loop through column to cut and paste data

    MrChulo,

    Thank you very much for your time and your solution. It worked perfectly.
    Really I would be strangling for days to get it through.

  8. #8
    Forum Contributor
    Join Date
    05-09-2021
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    148

    Re: Loop through column to cut and paste data

    Importing that amount of data when each file just have that few rows does also seem like something to do with Excel, then you could as Marc L says, do this conversion on import.

    Hope you are getting paid for so much manual work!

  9. #9
    Registered User
    Join Date
    09-18-2019
    Location
    London, England
    MS-Off Ver
    MS Office Profesional Plus 2010
    Posts
    12

    Re: Loop through column to cut and paste data

    This is something that I don't understand as well.
    You see the developers of the company that I'm working on doesn't seem to have thought it through.
    What I'm doing now is to try and connect an encrypted ID from 1 data base through the csv that I'm downloading with a public ID from another data base so then I can go to a 3rd data base and get the actual data that I need.
    The 1st data base has the restriction of 50 entries at a time the 2nd data base has a restriction of 200 entries at a time and the 3rd one doesn't have any limit.
    There used to be a way with macros through Internet Explorer but due to confidential data they stopped it. I'm pretty sure though that still this can be done with the correct credentials and security but due to the confidentiality we cannot reach out for help to people like you for example that you might know how to do it.
    And to answer to your question, no unfortunately I'm not getting paid for all this manual work. Again thank you for your time, I really appreciate it.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Loop through column to cut and paste data


    Quote Originally Posted by Markdk View Post
    I wish it was that easy. You see the place where I'm downloading the csv has the restriction of 50 entries at a time which then returns in csv with 200 to 250 rows according to the data. Then I have to copy and paste the data from the csv in my file and then do what I'm trying to do here.
    Just attach a couple of source csv text file and the expected result workbook accordingly …

+ 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] Loop through column, match date, copy & paste row data to another sheet
    By benlw1984 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2020, 06:08 AM
  2. Loop through column to find EID then find date and paste corresponding data
    By CieloSalas in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-01-2017, 06:33 PM
  3. Filtered Data Loop, Copy and Paste into New Workbook, delete first column and save
    By newbiegirl3113 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2016, 03:44 AM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. [SOLVED] Copy/Paste Macro Loop For A column of Data
    By brooksbil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-31-2012, 02:31 PM
  6. Copy Paste and Loop till last column with data
    By eirumba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2011, 07:23 AM
  7. Loop to find cell, paste to new column. Turn 1 column into many.
    By d0tc0m in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2011, 09:51 AM

Tags for this Thread

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