VBA, search in range of data and then copy and paste

dinokovac93

New Member
Joined
Oct 10, 2017
Messages
11
Hi Guys,

I'm working on a extremely tedious project and my hands are about to fall off :rolleyes:.

So to give you an idea of what I'm doing, check out this GIF:


eJn1DTj1VN.gif



(Note: In the GIF: there are more columns to fill out)

So I'm not sure if there is a VBA possible for filling out all of the information.

What the Macro would need to do is (or at least what I think it needs):

1. Search (loop) through the ranges of data in sheet 5.

2. It will copy and paste all the dates from sheet 5 (which are always in column A) into sheet 6 dates column.

3. Now sheet 6 has the dates filled out, along with the column names. This is the key information to taking data from sheet 5 and putting it in sheet 6.

4. There are more columns in sheet 5 but we only need the columns that are in sheet 6.

5. The columns's names in sheet 6 match the same columns in sheet 5.

6. To wrap it up: the macro will use the dates and column names from sheet 6 to obtain the data from sheet 5.


If anyone get help or have any suggestions please let me know, its really appreciated.


Subject IDnamevisit_dateintraocular pressure - rightintraocular pressure - leftVA - Right Distance SCVA - Right Distance SC +-VA - Right Distance CCVA - Right Distance CC +-VA - Right Distance PH SCVA - Right Distance PH CCVA - Left Distance SCVA - Left Distance SC +-VA - Left Distance CCVA - Left Distance CC +-VA - Left Distance PH SCVA - Left Distance PH CCVA TypeWearing Rx - Right SphereWearing Rx - Right CylinderWearing Rx - Right AxisWearing Rx - Right AddWearing Rx - Left SphereWearing Rx - Left CylinderWearing Rx - Left AxisWearing Rx - Left AddWearing Rx - Current AgeManifest Refraction - Right SphereManifest Refraction - Right CylinderManifest Refraction - Right AxisManifest Refraction - Right AddManifest Refraction - Right Dist VAManifest Refraction - Right Near VAManifest Refraction - Left SphereManifest Refraction - Left CylinderManifest Refraction - Left AxisManifest Refraction - Left AddManifest Refraction - Left Dist VARefraction TypePachymetry - Right EyePachymetry - Left Eyeexternal exam - right eyeexternal exam - left eyeSlit Lamp Exam - Right LidsSlit Lamp Exam - Right ConjunctivaSlit Lamp Exam - Right CorneaSlit Lamp Exam - Right ACSlit Lamp Exam - Right LensSlit Lamp Exam - Right IrisSlit Lamp Exam - Right VitreousSlit Lamp Exam - Left LidsSlit Lamp Exam - Left ConjunctivaSlit Lamp Exam - Left CorneaSlit Lamp Exam - Left ACSlit Lamp Exam - Left LensSlit Lamp Exam - Left IrisSlit Lamp Exam - Left VitreousFundus Exam - Right DiscFundus Exam - Right MaculaFundus Exam - Right PeripheryFundus Exam - Right VesselsFundus Exam - Left DiscFundus Exam - Left MaculaFundus Exam - Left PeripheryFundus Exam - Left VesselsFundus Exam - Right Cup/Disc RatioFundus Exam - Left Cup/Disc Ratio

<tbody>
</tbody>
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Here's a start...

Code:
Option Explicit



Sub dinokovac93()
Dim lrow&, col&
Dim cel As Range
Dim rng As Range


With Sheet5
    lrow = .Cells(.Rows.Count, 1).End(xlUp).Row
    .Range(.[A2], .Cells(lrow, 1)).Copy Sheet6.[C2]   'copy dates
End With


With Sheet6
    'set rng with column headers from sheet 6
    Set rng = .Range(.[D1], .Cells(1, .Columns.Count).End(xlToLeft))
    For Each cel In rng.Cells
        If WorksheetFunction.CountIf(Sheet5.Rows(1), cel) > 0 Then
            col = WorksheetFunction.Match(cel.Value, Sheet5.Rows(1), 0)
            Sheet5.Range(Sheet5.Cells(2, col), Sheet5.Cells(lrow, col)).Copy cel.Offset(1)
        End If
    Next cel
End With


End Sub

This sub uses a FOR loop to find matches in the first row of the two sheets. When a match is found, the range is copied to the destination sheet.
 
Upvote 0

Forum statistics

Threads
1,215,183
Messages
6,123,529
Members
449,105
Latest member
syed902

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