HLookup VBA Question

Sjoewie

New Member
Joined
Sep 23, 2014
Messages
2
Hi,

I am struggling with getting code for the following. In short I would like to use HLOOKUP to find a value in Sheet 2 and then copy and paste all the data below into Sheet 1.

So simple example

Sheet 2 (Upper left cell is A1):
Jan
Feb
Mar
Apr
44
6
7
10
21
17
35
1
3
19
31
17
45
1
12
15
11
46


<tbody>
</tbody>

Sheet 1 (assume upper left is A1 and B1 is the input variable.
Month
Feb

<tbody>
</tbody>

The issue I am having is that I struggle how to cope with the different amount of rows that I need to copy.

Any help would be greatly appreciated.

Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to the board.

This solution doesn't use HLOOKUP, but it should work for you. It pastes the info below B1 in sheet 1.

Code:
Sub CpyMnth()

    Dim x As Long
    Application.ScreenUpdating = False
    
    Range("B2:B1000").ClearContents
    x = WorksheetFunction.Match(Sheets(1).Range("B1"), Sheets(2).Range("A1:L1"), 0)
    
    Sheets(2).Select
    Range(Cells(2, x), Cells(Sheets(2).UsedRange.Rows.Count, x)).Copy Sheets(1).Range("B2")
    Sheets(1).Select
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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