Macro for copying value to specific cells

Ivica1987

New Member
Joined
Sep 20, 2016
Messages
14
Hello

Since I don't use much Excel Macro I will need help with a VBA Macro for Excel. What I need is to copy a specific value from the 5th row (the Main Value) depending which month it is (cell K1). As an example: if the month is equal to number 9 then it will copy the value from the cell for September (cell AD5: 125068) to the column D for each of the three employees (the orange fields).

I can maybe solve this with normal formulas and with the VBA function for recording, but it could happen that next month the number of employees is different and so the recorded macro wouldn't work. Also, I will need something that works on multiple sheets since every table in every sheet has the same layout.

Here is an example.

Thanks in advance and best regards
 
Here is an example with multiple sheets. Link.


Code:
Sub do_it()
    Dim x As Long
    x = Range("a" & Rows.Count).End(xlUp).Row
    Columns(4).NumberFormat = "general"
    Select Case x
        Case Is > 7
            Range("a7", Range("a7").End(xlDown)).Select
            Selection.Offset(, 3).FormulaArray = "=INDEX(R5C6:R5C41,MATCH(R1C11,MONTH(R4C6:R4C41),0))"
        Case Is = 7
            Range("d7").FormulaArray = "=INDEX(R5C6:R5C41,MATCH(R1C11,MONTH(R4C6:R4C41),0))"
        Case Is < 7
            Exit Sub
    End Select
End Sub

Code:
Sub Sheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Call do_it
    Next ws
End Sub
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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