简体   繁体   中英

Copy and paste in VBA macro

I am really just starting to learn VBA and have used the site a number of times.

I have a real head scratcher and would love some help and guidance - hopefully it will help me understand it a bit better.

What I essentially have is a big long list of Staff names in a column, and I need to create a row for each of those with the dates for the year 01/01/2016-31/12/2016.

I have in column A on one sheet 3666 rows (10 x 01/01/2016-31/12/2016) and I wondered if I can write VBA to copy the values of the first staff name, and paste it down 366 rows in column B, then step down 1 row, copy the next staff Name, and paste down the next 366 rows, and repeat until all 10 staff names have been completed.

Leaving me with a row for every staff name and every date of the year.

Hope this makes sense.

Any help would be awesome

you can use this code:

Sub test()
Dim i&, cl As Range, SDate As Date, FDate As Date, k As Variant
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
i = [A:A].Find("*", , , , , xlPrevious).Row
For Each cl In Range("A2:A" & i)
    If Not Dic.exists(cl.Value2) Then Dic.Add cl.Value2, ""
Next cl
Workbooks.Add: [A1] = "Name": [B1] = "Date"
FDate = "01/01/2017": i = 2
For Each k In Dic
    SDate = "01/01/2016"
    While SDate < FDate
        Cells(i, "A").Value2 = k
        Cells(i, "B").Value2 = SDate
        Cells(i, "B").NumberFormat = "DD/MM/YYYY"
        SDate = SDate + 1: i = i + 1
    Wend
Next k
End Sub

source:

在此处输入图片说明

output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM