hlookup in VBA

shaunkaz

Board Regular
Joined
Jan 30, 2008
Messages
204
Hi I have a drop down box in "A2" which is changed by month eg. (Apr- 16), in cells "A3:A8" this pulls through all sales data for that month.

I copy that data from cells "A3:A8" and paste values ONLY in the corresponding month in a table "C2: F8".

I need a macro that copies the figures in column "A" and pastes values only in table (C2:F8) for the corresponding month which is chosen in cell "A2"

I will then assign a macro button for this.

Apr-16 Jan-16Feb-16Mar-16Apr-16
2567101615442567
10018875100
2665503266
66648908666
948348899948
3485671348

<tbody>
</tbody>
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi I have a drop down box in "A2" which is changed by month eg. (Apr- 16), in cells "A3:A8" this pulls through all sales data for that month.

I copy that data from cells "A3:A8" and paste values ONLY in the corresponding month in a table "C2: F8".

I need a macro that copies the figures in column "A" and pastes values only in table (C2:F8) for the corresponding month which is chosen in cell "A2"

I will then assign a macro button for this.

Apr-16 Jan-16Feb-16Mar-16Apr-16
2567101615442567
10018875100
2665503266
66648908666
948348899948
3485671348

<tbody>
</tbody>

since you are adding this to a button, this should work

Code:
Sub Shaunkaz()
Dim lngrow As Long, lngcol As Long
Dim rng As Range, rngHEAD As Range, cell As Range
Dim strCELL As String
Dim intCELL As Integer
Dim ws As Worksheet

Set ws = ActiveSheet
Set cell = ws.Range("A2")
With ws
    lngrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    lngcol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
    Set rngHEAD = ws.Range(ws.Cells(2, 1), ws.Cells(2, lngcol))
    Set rng = ws.Range(ws.Cells(3, 1), ws.Cells(lngrow, 1))
    intCELL = rngHEAD.Find(cell.Value).Column
    rng.Copy
    ws.Cells(3, intCELL).PasteSpecial xlPasteValues
End With

End Sub
 
Upvote 0
Hi Sorry yes it does work, but not on my original sheet..

However is it possible to define a range to copy ie.. A3:A8 as the code above copys all data in column "A". Its just that I have other data below and this is also being copied

Also Where as on the table above, the datatable as shown where it is to be pasted is column "C" (2 columns away), where as on my original sheet it is coulmn D (3 columns away).

Can you adjust the code to compensate

Thanks
Shaun
 
Upvote 0
Hi Sorry yes it does work, but not on my original sheet..

However is it possible to define a range to copy ie.. A3:A8 as the code above copys all data in column "A". Its just that I have other data below and this is also being copied

Also Where as on the table above, the datatable as shown where it is to be pasted is column "C" (2 columns away), where as on my original sheet it is coulmn D (3 columns away).

Can you adjust the code to compensate

Thanks
Shaun

Sorry was out of the office for the holiday.

Try this

Code:
Sub Shaunkaz_v2()
Dim lngrow As Long, lngcol As Long
Dim rng As Range, rngHEAD As Range, cell As Range
Dim intCELL As Integer
Dim ws As Worksheet

    Set ws = ActiveSheet
    Set cell = ws.Range("A2")
    With ws
        lngrow = ws.Range("A2").End(xlDown).Row
        lngcol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
        Set rngHEAD = ws.Range(ws.Cells(2, 1), ws.Cells(2, lngcol))
        Set rng = ws.Range(ws.Cells(3, 1), ws.Cells(lngrow, 1))
        intCELL = rngHEAD.Find(cell.Value).Column
        rng.Copy
        ws.Cells(4, intCELL).PasteSpecial xlPasteValues
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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