Macro to choose data based on cell values in a column

akeemali

New Member
Joined
Nov 29, 2005
Messages
9
I hope someone out there can help. I have a worksheet with values in column A that are based on a personal id for an individual and then the next columns have information for that individual. So for instance I have info for the same individual in rows 1 to 16 then the next individual in rows 17 to 99 and then for the next in rows 100 to 121, etc...so the info for individuals is not the same number of rows always.

What I would like to do is to create a macro that allows me to copy cells in column F of the worksheet based on the value of column A. The value in column A is numeric id number. I would then like to paste these copied cells into another worksheet (and transposed the info into one row instead of several rows) i.e. for instance copy f1:f16 and paste into A1 in another worksheet and so on. This is because I have another worksheet with all the info for an individual laid out on one row instead of several rows like I have in this worksheet.

I am able to record a macro to select data for a particular personal id and paste special (values only and transposed) into the other worksheet but my problem is that I still have to manually select for each personal id and this is becoming quite a chore...as I need to do this for about 98,000 records! Can someone please help by just telling me or giving me the vba code to select a range of cells based on the value in a particular column.

Thanks to any helper(s) out there...
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hello akeemali, welcome to the board.
Here's a little variation on something I saw Erik do recently. If I understand what you're wanting it should work. It assumes you have a header row in sheet1 (where the original data is) and that if there are any blanks in column A, they are only between the ID number changes.
Code:
Sub Copy_And_Transpose()
Dim c As Range, LstRow As Range
Dim r As Long
Set LstRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
Set c = Cells(2, 1)
Application.ScreenUpdating = False

Do While Not c.Row > LstRow.Row
  r = c.Row
  Set c = Range(c, LstRow.Offset(1, 0)).ColumnDifferences(c)(1)
  Range(Cells(r, 1), Cells(c.Row - 1, 1)).Offset(, 5).Copy
    With Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp)(2, 1)
      .PasteSpecial Paste:=xlValues, Transpose:=True
    End With
Loop
 
With Application
  .CutCopyMode = False
  .ScreenUpdating = True
End With

End Sub
Hope it helps.
 
Upvote 0
HalfAce,

Thanks so much for your help...and as luck would have it I actually dropped my tablet pc after posting my request the other day...luckily it was only the hdd that was damaged I am now back online.

I tried the code you sent me just a few minutes ago and it has a problem at various lines...

Do you mind if I send out a sample of the file to see what I am trying o do?

:confused:
Thanks again.
akeemali
 
Upvote 0
No, I don't really mind you sending a copy of the workbook, but for the benefit of others searching/reading this thread I'd rather see if we can figure it out here on the board first.

Can you describe the errors you're getting and what line(s) are highlighted when they occur?
 
Upvote 0
actually, the error materialises at different points in the macro...I should think that sending the xls file out is the easiest way to see what I am trying todo and where the error(s) are...thanks
 
Upvote 0
sheet 1
Gordon Merger.xls
ABCDEFGHIJKLMNOP
1PatientHbA1cTotalcholesterollevelTrilevelBMImed1med2med3med4med5med6med7med8med9med10
276.17.38.423.9
3398.94.2739.5
48411.55.2123.4
Datasheet1
 
Upvote 0
datasheet2
Gordon Merger.xls
ABCD
174.1.4TemazepamTablets10mg18/02/2004TemazepamTablets10mg
276.3.4PrednisoloneE/CTablets5mg38696PrednisoloneE/CTablets5mg
373.1.2IpratropiumSteri-NebNebuliserSolution250micrograms/1ml27/10/2005IpratropiumSteri-NebNebuliserSolution250micrograms/1ml
472.9AspirinDispersibleTablets75mg38543AspirinDispersibleTablets75mg
573.1.1.1SalmeterolXinafoateAccuhaler50micrograms/dose38543SalmeterolXinafoateAccuhaler50micrograms/dose
674.3.3SertralineHydrochlorideTablets100mg38543SertralineHydrochlorideTablets100mg
773.1.1.1SalbutamolCfc-FreeInhaler100micrograms/puff38543SalbutamolCfc-FreeInhaler100micrograms/puff
872.9ClopidogrelHydrogenSulphateTablets75mg38543ClopidogrelHydrogenSulphateTablets75mg
972.6.1GlycerylTrinitrateCfc-FreePumpSpray400micrograms/dose38543GlycerylTrinitrateCfc-FreePumpSpray400micrograms/dose
10721.8OxygenCylinder1360litres38421OxygenCylinder1360litres
1174.7.1Co-Codamol30/500EffervescentTablets38543Co-Codamol30/500EffervescentTablets
1272.6NicorandilTablets20mg38543NicorandilTablets20mg
1372.6NicorandilTablets10mg38543NicorandilTablets10mg
1472.2.2FurosemideTablets40mg38543FurosemideTablets40mg
1572.6.2DiltiazemHydrochlorideM/RCapsules180mg38543DiltiazemHydrochlorideM/RCapsules180mg
1672.6.1IsosorbideMononitrateM/RCapsules60mg38543IsosorbideMononitrateM/RCapsules60mg
173914.4EnziraSuspensionForInjection0.5mlpre-filledsyringe13/10/2005EnziraSuspensionForInjection0.5mlpre-filledsyringe
18396.1.2.2MetforminHydrochlorideTablets850mg30/09/2005MetforminHydrochlorideTablets850mg
19396.1AdvantageIiTestStrips30/09/2005AdvantageIiTestStrips
20392.2.2FurosemideTablets40mg38512FurosemideTablets40mg
21396.1NovofineNeedlesForInsulinPens30g8mm30/09/2005NovofineNeedlesForInsulinPens30g8mm
22393.4.1CetirizineHydrochlorideTablets10mg30/09/2005CetirizineHydrochlorideTablets10mg
23392.4BisoprololFumarateTablets1.25mg30/09/2005BisoprololFumarateTablets1.25mg
243910.1.1DiclofenacSodiumE/CTablets50mg38693DiclofenacSodiumE/CTablets50mg
25844.3.1LofepramineTablets70mg19/10/2005LofepramineTablets70mg
26842.9AspirinE/CTablets75mg19/10/2005AspirinE/CTablets75mg
27846.1AdvantageIiTestStrips19/10/2005AdvantageIiTestStrips
28842.12AtorvastatinTablets20mg19/10/2005AtorvastatinTablets20mg
29846.1Bd-Microfine+NeedlesForInsulinPens8mm/31gauge19/10/2005Bd-Microfine+NeedlesForInsulinPens8mm/31gauge
30842.5.5.1RamiprilCapsules1.25mg19/10/2005RamiprilCapsules1.25mg
Datasheet2
 
Upvote 0
my problem described...

so what would like to do is copy column B in datasheet 2 and paste into columns G to P in datasheet1. For instance data in B1:B9 of datasheet 2 go into G1:P1 of datasheet1, data in B17:B24 of datasheet2 go into H1:P1, etc...

So, the macro I need has to select data from column B in datasheet 2 based on the value of column A in the same sheet, then copy such data, activate datasheet 1 and transpose and paste the copied data into coulmns G to P based on the value of column A in datasheet 1 (which correspnds to value of column A in datasheet 2 as it is the same personal id)...

I hope this is posible! Thanks everyone for your support...otherwise I will have to do this for more than 30000 personal ids!!!
 
Upvote 0
It seems that I am really stumpedhere and there is no hope that this will work or is there any help out there?
 
Upvote 0

Forum statistics

Threads
1,203,658
Messages
6,056,583
Members
444,877
Latest member
kat517

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