VBA copy numerous cells to new sheet based on criteria

Akashwani

Well-known Member
Joined
Mar 14, 2009
Messages
2,911
Good day MrExcelers!

I have been given a Workbook to "tidy up", it's been a long time and I'm seriously struggling, so here I am!

I have a Worksheet that I CANNOT alter the layout of and that is making it difficult to find a suitable VBA code to copy the key data from the Active sheet to a "DataHistory" sheet.

The criteria will be in Column AE (Rows 3-31) in the form of a number.

The Cells I want to copy will be in these Columns and in the following order...
AE, A, F, H, J, K, L, M, O, Q, S, U, W, Y, Z, AA, AC, AD

The Cells that meet the required Criteria need to be copied to the next empty row on Sheet "DataHistory"

Example...
If AE3, AE6, AE20, AE31 have a number in, then the relevant cells on those rows need to be copied to the next empty Row on Sheet "DataHistory". So, the Data from Row3 would be copied to the next empty Row, from Row6 to the next empty Row and so on.

I hope that is clear, but I have some doubts!

Thank you for looking at this and I await some positive replies.

Ak
 
Hi,
. I have just finished a working code for you. I will Tidy it up a bit and post it early Tomorrow
Alan
Bavaria
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi <o:p></o:p>
….. Here is mysolution to your Problem. It is a somewhat simple “Beginners” type Solution. Itis based on how I am currently solving the Problem which is my main reason forgetting involved with Excel VBA. This problem is the Sorting and Reorganizing ofa very large data File and bringing new data into it. The new data can be invarious forms, that is to say in incontinently column order. So something similarI guess to what you are doing.<o:p></o:p>
. One ofmy first Threads was asking for improvements to this method (http://www.mrexcel.com/forum/excel-questions/792647-simple-data-sort-merge-code.html ). It got no replies so I have been learning, amongst other things from participating in this forum, and I particularlylearn lots when a Profi comes in later to a thread like yours that I answer and later and gives a better solution. So Let’sboth keep our fingers crossed for that!!.....<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
….Anyways thesolution I have for you:- <o:p></o:p>
<o:p> </o:p>
. You will see that I have slightly reorganizedyour DataHistory Sheet, that is to say you have an extra heading Row with Addressesin it. <o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p>
Book1
ABCDEFGHIJKLMNOPQRS
1$AE$1$A$1$F$1$H$1$J$1$K$1$L$1$M$1$O$1$Q$1$S$1$U$1$W$1$Y$1$Z$1$AA$1$AC$1$AD$1
2Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18
3
DataHistory
</o:p>

<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
..Yes, you mayhave you guessed it - the Addresses in this new heading row are those for the relevantmatching first Row Columns in the newData which you wish to include in Data History Sheet. <o:p></o:p>
<o:p> </o:p>
. So the programbasically checks For a match in the address, and then copies in the relevantColumn<o:p></o:p>
<o:p> </o:p>
. You can changethe order or add or delete column Addresses in the extra heading row. Theprogram will take care of that and put all Data in the new corresponding order(If you see what I mean?)<o:p></o:p>
<o:p> </o:p>
. Here is a partof the DataHistory Sheet before running the Macro<o:p></o:p>
<o:p> </o:p>
<o:p>
Book1
ABCDE
1$AE$1$A$1$F$1$H$1$J$1
2Header1Header2Header3Header4Header5
3
4
5
6
7
8
9
DataHistory
</o:p>

<o:p> </o:p>
<o:p> </o:p>
. Here is thesame again after running the program twice<o:p></o:p>
<o:p> </o:p>
<o:p>
Book1
ABCDE
1$AE$1$A$1$F$1$H$1$J$1
2Header1Header2Header3Header4Header5
319.08.2014U09575860126.42140
419.08.2014U095762561326.53280
519.08.2014U096593403666.99640
619.08.2014U098752496449.28490
719.08.2014U0991873021314.41370
819.08.20149120493444.8346
919.08.2014423932221124.38128
1019.08.20144251356057.1258
1119.08.2014425102834201.21205
1219.08.201442392119767.0368
1319.08.2014U09575860126.42140
1419.08.2014U095762561326.53280
1519.08.2014U096593403666.99640
1619.08.2014U098752496449.28490
1719.08.2014U0991873021314.41370
1819.08.20149120493444.8346
1919.08.2014423932221124.38128
2019.08.20144251356057.1258
2119.08.2014425102834201.21205
2219.08.201442392119767.0368
23
24
DataHistory
</o:p>

<o:p> </o:p>
. So you seethat new data is added after any existing data<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
. Here is a fullCode:<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> <font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN> <SPAN style="color:#007F00">'Not necerssary to do this but helps limit Computer Memory usage and helps show up typing errors when writing code</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> SimpleSortOfASort()<br><SPAN style="color:#00007F">On</SPAN> Err <SPAN style="color:#00007F">GoTo</SPAN> TheEnd <SPAN style="color:#007F00">'  Go to the End rather than crashing if an error occurs</SPAN><br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#007F00">'Not necerssaryy but speeds things up a bit by not continually updating the screen</SPAN><br>  <SPAN style="color:#00007F">Dim</SPAN> NewDataMaxRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, DataHistoryLastRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'Byte limits the Row size to 255</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> NewDataMaxRow = Worksheets("TypicalData").Cells(Rows.Count, 1).End(xlUp).Row <SPAN style="color:#007F00">'Get maximum Row For new data</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> DataHistoryLastRow = Worksheets("DataHistory").Cells(Rows.Count, 1).End(xlUp).Row <SPAN style="color:#007F00">'Get last Row where  data was inputed in Data History Sheet</SPAN><br>  <SPAN style="color:#00007F">Dim</SPAN> NewDataColumnMax <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, DataHistoryColumnMax <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'Limit Columns to 255</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> NewDataColumnMax = Worksheets("TypicalData").Cells(1, Columns.Count).End(xlToLeft).Column <SPAN style="color:#007F00">'Get Maximum Column in new Data</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> DataHistoryColumnMax = Worksheets("DataHistory").Cells(1, Columns.Count).End(xlToLeft).Column <SPAN style="color:#007F00">''Get Maximum Column in Data History Sheet</SPAN><br>  <SPAN style="color:#00007F">Dim</SPAN> NewDataRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, DataHistoryRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, NewDataColumn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, DataHistoryColumn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> NewDataColumn = 1 <SPAN style="color:#00007F">To</SPAN> NewDataColumnMax <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">' go through each column of new data</SPAN><br>      <SPAN style="color:#00007F">For</SPAN> DataHistoryColumn = 1 <SPAN style="color:#00007F">To</SPAN> DataHistoryColumnMax <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">'Go througth every Data History column</SPAN><br>      <SPAN style="color:#00007F">If</SPAN> Worksheets("TypicalData").Cells(1, NewDataColumn).Address = Worksheets("DataHistory").Cells(1, DataHistoryColumn).Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'look for match in address</SPAN><br>        <SPAN style="color:#00007F">Let</SPAN> DataHistoryRow = DataHistoryLastRow <SPAN style="color:#007F00">'Before Putting the next column in, Reset the data history Row.</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> NewDataRow = 1 <SPAN style="color:#00007F">To</SPAN> NewDataMaxRow <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">'Go through each new data value in the column...</SPAN><br>        <SPAN style="color:#00007F">Let</SPAN> DataHistoryRow = DataHistoryRow + 1 <SPAN style="color:#007F00">'New row for DataHistory'...go to a new row in Data History....</SPAN><br>        Worksheets("TypicalData").Cells(NewDataRow, NewDataColumn).Copy Destination:=Worksheets("DataHistory").Cells(DataHistoryRow, DataHistoryColumn) <SPAN style="color:#007F00">'...And Copy the appropriate vale. Note we choose to use Copy Destination:= method as then your original formatting is preserverd</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> NewDataRow <SPAN style="color:#007F00">'Go to next new data value in column..</SPAN><br>      <SPAN style="color:#00007F">Else</SPAN> <SPAN style="color:#007F00">' If no match.....</SPAN><br>      <SPAN style="color:#007F00">'....Do nothing!</SPAN><br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>      <SPAN style="color:#00007F">Next</SPAN> DataHistoryColumn <SPAN style="color:#007F00">'Once all rows are copied go back and look again for a matching column address</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> NewDataColumn <SPAN style="color:#007F00">'Start ther whole thing again for the next column in in the New Data</SPAN><br>TheEnd:<br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#007F00">'Turn Screen updating back on. Important to put this here, otherwise in the event of an error, your screen stays "Dead"!</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'SimpleSortOfASort()</SPAN></FONT></o:p>
<o:p> </o:p>
<o:p> </o:p>
. And here asimplified one without ‘Green comments graffiti etc.<o:p></o:p>
<o:p> </o:p>
<o:p>
Code:
Sub SimplifiedSimpleSortOfASort()
DataHistoryLastRow = Worksheets("DataHistory").Cells(Rows.Count, 1).End(xlUp).Row
    For NewDataColumn = 1 To Worksheets("TypicalData").Cells(1, Columns.Count).End(xlToLeft).Column
      For DataHistoryColumn = 1 To Worksheets("DataHistory").Cells(1, Columns.Count).End(xlToLeft).Column
      If Worksheets("TypicalData").Cells(1, NewDataColumn).Address = Worksheets("DataHistory").Cells(1, DataHistoryColumn).Value Then
        DataHistoryRow = DataHistoryLastRow
        For NewDataRow = 1 To Worksheets("TypicalData").Cells(Rows.Count, 1).End(xlUp).Row
        DataHistoryRow = DataHistoryRow + 1
        Worksheets("TypicalData").Cells(NewDataRow, NewDataColumn).Copy Destination:=Worksheets("DataHistory").Cells(DataHistoryRow, DataHistoryColumn)
        Next NewDataRow
      End If
      Next DataHistoryColumn
    Next NewDataColumn
End Sub
</o:p>

<o:p> </o:p>
<o:p> </o:p>
. Also here is a file with all macros in them(In module “Get Sort Id”)<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
http://snk.to/f-ctmyuy0i<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
. As I mentioned there are definitely lots ofimprovements that can be done. As I am practicing with this sort of thing Icould have another go sometime if no Profi comes in with something better inthe meantime. <o:p></o:p>
.. And get back to me with any questions /comments, for example if you need help to (remember?) the basic way to get thecodes running etc.<o:p></o:p>
<o:p> </o:p>
Alan.<o:p></o:p>
<o:p> </o:p>
P.s. A few points to bear in mind<o:p></o:p>
.1 I have limited various Columns and rows fornow to 255. But (depending on which version of Excel you have) this can easily bechanged.<o:p></o:p>
.2 That extra heading row does not need to beexactly there in Data History. It could be in a spare Sheet or somehow storedin a Matrix / Array type of thing. The Program can easily be modified to dothat. But I thought for demonstration purposes it was good to have it there soyou can see exactly from which columns your data is coming from.<o:p></o:p>
.3 As it stands your new data must be in the TypicalData sheet. But it is easy to modify the program to bring data from an active sheet. So then you could bring data in from different files. (But againyou must check the order of your columns. Then if there are variations thenchange that extra Heading Row appropriately)<o:p></o:p>
.4 Sheet MiscMacros in the file I send you, youcan trash – in that the sheet and that sheet module I was experimenting. But Ileave it in in case anything there is useful to you<o:p></o:p>
.5 Thesimplified macro will not work unless you comment out (Or delete) The firstline in the Get Sort Id module, Option Explicit<o:p></o:p>
 
Upvote 0
Hi Alan, WOW.
Thank you so very much for all the hard work and time you have put into trying to resolve this for me, it is greatly appreciated.

I have tested and tested the code in your Workbook and it works great, but then I spotted, on the sheet TypicalData, there are no Headers in ROW1, when I add Headers, the code copies those to the DataHistory sheet??!!

Do you have a solution to this?

Once again, a very BIG thank you.

Ak
 
Upvote 0
I have tested and tested the code in your Workbook and it works great, but then I spotted, on the sheet TypicalData, there are no Headers in ROW1, when I add Headers, the code copies those to the DataHistory sheet??!!

Do you have a solution to this?

Sounds dead easy.

-Show me again exactly how your typical data should looks like, including headers (I saw no headers in your sample data)
-Or better still, if you are able, send me over FileSnack or box or whatever a file with sample data in it.

Alan
 
Upvote 0
Hi Alan.

I hope this is ok....

Excel Workbook
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18
2U09575860126.42140132.940815490.31.19%8.6-116.790.163-7.06152.170.5-56.0719/08/2014
3U095762561326.53280343.6708134116.62-1.40%8.5395.390.10963.674135.870.5-145.7119/08/2014
4U096593403666.99640646.3505189.2104.222.64%2.156.670.1886.3476184.212.543.3419/08/2014
5U098752496449.28490504.201020191.690.50%4.8-195.460.19614.1962002.5-263.6019/08/2014
6U0991873021314.413701477.50420295.947.18%11.73-652.660.188107.5187.52.5-1,913.6819/08/2014
79120493444.8346114.7200122.597.4660.82%6.8-7.940.04968.715480.3-475.2019/08/2014
8423932221124.38128316.3300142.297.1760.62%6.8-24.640.058188.33560.5-1,305.2619/08/2014
94251356057.1258108.380019398.4847.15%7.52-6.620.10450.381020.3-385.4819/08/2014
10425102834201.21205403.4900142.298.1550.07%6.8-25.740.072198.5710.5-1,375.5119/08/2014
1142392119767.0368170.8100142.798.5860.76%2.23-2.160.057102.81560-231.1219/08/2014
TypicalData


There wasn't any headers on my first example as I hid them whilst hiding none important rows/columns!

Thanks.

Ak
 
Upvote 0
Hi,
2 questions:

1. Do you always have headings in your Raw Data

2. Do you simply want the code to ignoor them and only copy the data
Alan
 
Upvote 0
Hi Alan,

That will be a yes to both questions.

Ak

Hi Akashwani
. Then it is really easy:

. If you take a look at my (Colorful!) code you will see the line

<font face=Calibri>        <SPAN style="color:#00007F">For</SPAN> NewDataRow = 1 <SPAN style="color:#00007F">To</SPAN> NewDataMaxRow <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">'Go through each new data value in the column...</SPAN></FONT>

. This tells the computer to go through every Row in the new data starting at 1 and stoping at the last entry. So simply change that 1 to a 2. It will then start at the second Row instead and ignor the first (Heading) Row.

I leave it to you as "Homework" to find the corresponding line correction in the Shortened program!!

Come back if I can help futher

Alan
 
Upvote 0
EXCELLENT.
Thank you so much Alan, I really do appreciate your time, patience and work with this problmem.

Code:
For NewDataRow = 2 To Worksheets("TypicalData").Cells(Rows.Count, 1).End(xlUp).Row

That seems to work as well, BONUS! :)

Thanks again Alan.

Ak
 
Upvote 0
EXCELLENT.
Thank you so much Alan, I really do appreciate your time, patience and work with this problmem.

Code:
For NewDataRow = 2 To Worksheets("TypicalData").Cells(Rows.Count, 1).End(xlUp).Row

That seems to work as well, BONUS! :)

Thanks again Alan.

Ak


You're Welcome.

Come back anytime, especially with threads on any "sort of sorting". Maybe I'll catch them!

Alan
 
Upvote 0

Forum statistics

Threads
1,215,180
Messages
6,123,502
Members
449,100
Latest member
sktz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top