VBA code to transpose data from vertical to horizontal based on certain conditions.

dsubash

New Member
Joined
Nov 22, 2024
Messages
30
Office Version
  1. 2019
  2. Prefer Not To Say
Platform
  1. Windows
Hi,
I am looking for a VBA code to transpose data from vertical to horizontal based on certain conditions.

My Excel spreadsheet has two sheets.

Sheet 1(Data), where in details of tests run for each patient is given in separate rows (one row for one test) – For a single Patient, multiple rows are present based on the number of tests conducted for the particular patient. My data runs to 15000 or more rows. This data have been downloaded from our software.

Sample Test Details.xlsx
ABCDEFGHIQ
1No.Sample No.Sample IDTestResultRegister DateNameAgeGenderTest Date
211062339000106233900 UREA2901/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:13
321062339000106233900 SGOT19.601/01/2025 08:57NithishKumar0YearsMale01/01/2025 09:16
22211062341000106234100 GLUC11601/01/2025 08:57Maharaja0YearsMale01/01/2025 09:19
23221062341000106234100 CAL A10.401/01/2025 08:57Maharaja0YearsMale01/01/2025 09:20
27261062340000106234000 TGL19201/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:19
28271062340000106234000 GLUC40201/01/2025 08:58VetrivelS0YearsMale01/01/2025 09:18
29281062342000106234200 GLUC9701/01/2025 09:59AnandKumarV0YearsMale01/01/2025 10:13
30295005736000500573600 GLUC15001/01/2025 10:19John0YearsMale01/01/2025 10:34
31305005735000500573500 GLUC9401/01/2025 10:19Pooranima0YearsFemale01/01/2025 10:33
32315005735010106234400 GLUC10101/01/2025 10:29logapriyaMale01/01/2025 10:49
33325005735020106234400 UA3.601/01/2025 10:36logapiyaMale01/01/2025 10:50
58571062356000106235600 SGOT3301/01/2025 12:41AbdulAlphaf0YearsMale01/01/2025 12:55
59581062356000106235600 SGPT5801/01/2025 12:41AbdulAlphaf0YearsMale01/01/2025 12:55
60591062356000106235600 ALP7901/01/2025 12:41AbdulAlphaf0YearsMale01/01/2025 12:56
616050057350620250101500573506 PHOS6.2701/01/2025 13:39sivakamasundariMale01/01/2025 13:53
62611062375000106237500 BILL-D3.3801/01/2025 14:22Sara0YearsFemale01/01/2025 14:37
Data


Sheet2 (Report) – I need the same data in a horizontal format, i.e, I need all the tests run for a single patient in a single row. I need only the names of the tests run for each patient and the same to be based on the header. Column A will be serial number, Columns B to G will be names of the tests conducted, eg. If a patient had taking only Urea, GGT, Gluc, etc, then the name of the tests conducted for the particular patient to be mentioned under the header. I have realigned some headers for the ease of my reporting. Sample No. or Sample ID is unique to a particular patient and the same can be used for filtering.

Sample Test Details.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1No.Sample No.Sample IDRegister DateNameGenderTest DateUREASGOTGGTTPCHOLBILL-DALPBILL-TCAL ATGLSGPTCREATINALBUAGLUCPHOSACEIRONAMYLASEMICROCKNACMAGLDHCKMBZINC-CO
211062339000106233900 01/01/2025 08:57NithishKumarMale01/01/2025 09:13UREASGOTGGTTPCHOLBILL-DALPBILL-TCAL ATGLSGPTCREATINALBUAGLUC
321062341000106234100 01/01/2025 08:57MaharajaMale01/01/2025 09:21UREACHOLCAL ATGLCREATINUAGLUCPHOS
41062340000106234000 01/01/2025 08:58VetrivelSMale01/01/2025 09:19CHOLTGLCREATINGLUC
51062342000106234200 01/01/2025 09:59AnandKumarVMale01/01/2025 10:13GLUC
65005735000500573500 01/01/2025 10:19PooranimaFemale01/01/2025 10:33GLUC
71062402000106240200 01/01/2025 16:39SumithraFemale01/01/2025 16:54SGOTTPBILL-DALPBILL-TCAL ASGPTALBACE
81062435000106243500 02/01/2025 09:23BhuvaneshwariBFemale02/01/2025 09:50UREA
95005739000500573900 02/01/2025 11:53SuryarajMale02/01/2025 12:12UREASGOTGGTTPALPBILL-DBILL-TSGPTCREATINALBGLUC
101062741030106278000 03/01/2025 15:39JOHNMale03/01/2025 15:51AMYLASE
111063087000106308700 05/01/2025 09:20SavithaFemale05/01/2025 09:21UREASGOTGGTTPBILL-DALPBILL-TCAL ASGPTCREATINALBUAMICRO
121063554020106357100 07/01/2025 14:23muraliMale07/01/2025 14:36CKNAC
131063609020520103200 07/01/2025 16:05kumuthaMale07/01/2025 16:18MAG
1410636090620250107106360906 07/01/2025 16:14karthikMale07/01/2025 16:27CREATINGLUCIRONLDH
151064347000106434700 10/01/2025 16:35SuntharesanMale10/01/2025 16:48CKMB
161064758080106478200 13/01/2025 16:04THIYAMale13/01/2025 16:15ZINC-CO
Report


I am looking for a VBA Code that would ease my work. I am using Excel 2019 Professional

Thanks in Advance
Subash D
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
if you have Microsoft Access, this result can be made with a simple Crosstab query, no code.
link (or import) the data, run the query.

1737120942177.png
 
Upvote 0

Forum statistics

Threads
1,225,637
Messages
6,186,137
Members
453,339
Latest member
Stu61

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