Macro to transfer data from sheet1 to sheet2

Karp40

New Member
Joined
Jan 31, 2014
Messages
3
Hi, I've done a lot of searching and I can't seem to find a macro to do what I need. I'm using Excel 2016 on a Windows7 system.

I want to transfer the data from sheet1 to sheet2 but I need transfer the peaks into the correct peak# slots in Sheet2.

My example Sheet1 includes a small portion of the data. There are actually 126 samples x 4 treatments. Two treatments have 78 peaks and two treatments have 97 peaks. Each treatment is in a separate excel file. Each sample within the treatments is missing some peaks.

My example Sheet2 shows how I need the data to look when its moved and sorted.

Thanks for any help that you can provide,

Charles

ABCDEF
110a_aa_post10b_aa_post10c_aa_post
2Peak NumberAreaPeak NumberAreaPeak NumberArea
3117119981185365011847710
4280293402561129026016448
5349158813877565633215693
6438680016471855765435540418
7551902855871825614839
8611922476150069161402716
9712346807142855971560591
1085548756812625992812906065
119605775912534839817432
1211554808105362611779580
13128228298115220731210264538
14132197105412132362651330499693
1514147652391313706866149260197
16151454210414157048121514630105
17161783588031520114122161.89E+08
1817185498161.88E+0817196856
19182819961725674518366896
201945621119209729192096169
212112693321201.25E+08216659092
222339389980211280377622108981
232419395726221868472335203858
24251256822723539238812430211645
2526236258824110557362513345519
262774294532516060882261377612
2729511484261978827277859924
283019849468027768951929879285
293148888629172378302.17E+08
30321636628302.15E+0831191226
31331135101931155564321790381
32345896764643217871993318733803
333561658145335966999345.83E+08
3436135309918345.19E+08351.03E+08
353711490783582092558361.32E+08
3639483099361.53E+08371672384
37412134645913745338539380146
3842213801373995584411.57E+08
39432595170412.32E+084250685538
4044226421484228205400433036323
4145257179034323562214442633226
42463502912444202258054568955608
4348880218545176378854665401995
444963415064626788062475308973
455012705948103758924811641826
465124309904936709944911754336
4752781273950296236501142407
485497095512197993514276465
4955148402635282855745210141213
5056517616655469676954372645
51571588913475658897415526558749
525828025724572.3E+085612618311
53591444081145827776530571.82E+08
54607149809593.83E+08581.18E+08
55611031245609885661592.12E+08
5662950815661275596605176742
576325611499622295511061478733
5864953254163122908936214754304
596538542846473818276345035197
6066163786299653115115649162606
616724907271661.73E+08654606021
626824265686726776087661.92E+08
636935699246828913006730971809
64703505023696338285683145373
65712584635704383205695170261
6672979883713652839704761501
677333528472807410716291664
6874195917160973164844721702351
697528344644742.07E+0973373766
70762440848787520728256741.76E+09
717721866630762.52E+087543787099
7278547175637736394594762.88E+08
73781.26E+087738370513
74781.15E+08

<tbody>
</tbody>
Sheet1




ABCD
1PeakNumber10a_aa_post10b_aa_post10c_aa_post
21171199818536501847710
32802934056112906016448
43491588187756563215693
54386800167185576535540418
65519028587182614839
76119224715006911402716
87123468014285591560591
9855487561262599212906065
1096057751253483817432
111053626
1211554808522073779580
131282282981323626510264538
1413219710541370686630499693
151414765239157048129260197
1615145421042011412214630105
1716178358803188259221189161699
1817185498256745196856
1918281996366896
20194562112097292096169
2120124819302
222112693321128037766659092
2322186847108981
2423393899805392388135203858
2524193957261105573630211645
2625125682271606088213345519
2726236258819788271377612
2827742945376895197859924
2928
3029511484172378879285
3130198494680215474018217062680
3231488886155564191226
3332163662817871991790381
343311351019596699918733803
3534589676464519358121583149402
36356165814582092558103154593
3736135309918153243824132203164
383711490784533851672384
3938
403948309995584380146
4140
4241213464591232201644156812752
4342213801372820540050685538
4443259517023562213036323
4544226421482022580542633226
4645257179031763788568955608
4746350291242678806265401995
48475308973
494888021851037589211641826
50496341506367099411754336
51501270592962361142407
5251243099021979934276465
53527812739828557410141213
5453
555497095372645
565514840263469676926558749
57565176166588974112618311
5857158891347230074510182362907
59582802572427776530117636571
6059144408114383111620212149788
6160714980998856615176742
62611031245275596478733
636295081562295511014754304
6463256114991229089345035197
6564953254173818279162606
6665385428431151154606021
6766163786299173263366191733905
6867249072712677608730971809
6968242656828913003145373
7069356992463382855170261
7170350502343832054761501
7271258463536528396291664
73729798838074101702351
7473335284164844373766
7574195917160920666009231758841931
7675283446442072825643787099
7776244084878252196455287838008
7877218666303639459438370513
797854717563126288053114539600

<tbody>
</tbody>
Sheet2
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the MrExcel board!

Best to try and keep your sample data and expected results a bit smaller than that if you can.

Do the PeakNumbers already appear on Sheet2 in column A or does the macro need to determine them too?
 
Upvote 0
Hi,
Sorry for the length of the sample data. I wasn't sure how much I needed to post for everyone to see the patterns in the data.

The peak numbers would already be in column A of Sheet2 (it only needs to be on the sheet once). The macro needs to look at each sample in Sheet1, take the sample name (ie. 10a_aa-post) and paste into B1 on sheet2. Take peak number 1 from that sample and paste it into B2, peak number2 into B3, etc. Then move on the next sample and put in into C1, etc., until it runs out of samples. The biggest thing is to put each peak's area data into its corresponding row on Sheet2 and to leave a blank cell for the peaknumbers with no data.

Thanks for any help that you can give,

Charles
 
Upvote 0
Assuming the layout of the two sheets is as below (ie column heading positions, starting rows of actual data, nothing in Sheet2 except in col A etc) ..

ABCDEF
110a_aa_post10b_aa_post10c_aa_post
2Peak NumberAreaPeak NumberAreaPeak NumberArea
3117119981185365011847710
4280293402561129026016448
5349158813877565633215693

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1




ABCD
1PeakNumber
21
32
43
54

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet2




.. then give this a try in a copy of your workbook.

Code:
Sub PeakNumbers()  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, uba2 As Long
  
  a = Sheets("Sheet1").Range("A1").CurrentRegion.Value
  uba2 = UBound(a, 2)
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet2")
    With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
      For i = 1 To .Rows.Count
        d(.Cells(i, 1).Value) = i
      Next i
      ReDim b(1 To .Rows.Count, 1 To uba2 / 2)
      For j = 1 To uba2 Step 2
        b(1, (j + 1) / 2) = a(1, j)
      Next j
      For i = 3 To UBound(a)
        For j = 1 To uba2 Step 2
          If d.exists(a(i, j)) Then b(d(a(i, j)), (j + 1) / 2) = a(i, j + 1)
        Next j
      Next i
      With .Offset(, 1).Resize(, UBound(b, 2))
        .NumberFormat = "General"
        .Value = b
      End With
      .CurrentRegion.Columns.AutoFit
    End With
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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