简体   繁体   中英

Excel VBA copy template worksheet

I'm VBA noobs and in need to finish my assignment.

I would like to copy a template worksheets into and copy some cell automatically to it.
Here's what I get from webs, and I'm stuck now..

Sub CopyTemplate()
    Dim myCell As Range, MyRange As Range, Orange As Range
    Set MyRange = Sheets("Isolation Section").Range("B24")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))
    Set Orange = Sheets("Isolation Section").Range("D24")
    Set Orange = Range(MyRange, MyRange.End(xlDown))

    Application.ScreenUpdating = False
    For Each myCell In MyRange
        Sheets("Template").Copy After:=Sheets(Sheets.Count)
        With myCell
            ActiveSheet.Name = .Value
            ActiveSheet.Range("A13").Value = .Value
            ActiveSheet.Range("E13").Value = Orange.Value
            .Parent.Hyperlinks.Add Anchor:=myCell, Address:="", SubAddress:= _
               "'" & .Text & "'!B24", TextToDisplay:=.Text
        End With
    Next myCell
    Application.ScreenUpdating = True
End Sub

I have a template sheet called Template .
I create a copy of Template sheet and name it after each row from Isolation Section (so a loop).
Then place the cell data row B24 in cell A13 of the sheet.
But how about the cell data row D24 from Isolation Section copied to each sheets in new worksheets cell E13 ?

Image:

样本图片

Sorry if my English is bad..

Your description isn't very clear, but the easiest way to resolve your issue would be to just use macro recorder and copy and paste the code.

Here's microsoft's site on how to create/delete one: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-macro-HP010342374.aspx

I create macros sometimes and as long as you know something about coding, you can just glue together a couple of macro recordings and have a working macro... that's actually standardised. Good luck.

Try this:

For Each mycell In MyRange
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    With ActiveSheet
        .Name = mycell
        .Range("A1").Value = mycell.value
        .Range("E1").Value = mycell.Offset(0, 1).Value
    End With
    mycell.Parent.Hyperlinks.Add Anchor:=myCell, Address:="", SubAddress:= _
        "'" & mycell.Text & "'!B24", TextToDisplay:=mycell.Text
Next

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