Name worksheets based on a list of Names

2019-08-12 18:25发布

I am trying to Name new created worksheets based on list of names in sheets("Run") from range ("F4") to lastrow.

My problem: The macro is only creating one new worksheet, instead of creating new worksheets based on the number of names in the list. Please see my code below:

Sub new_1()

  Dim RCount As Integer
  Dim n As Integer
  Dim test As Worksheet

  Sheets("Security Distribution").Copy After:=Sheets(Sheets.Count)
  Set test = ActiveSheet

  Application.ScreenUpdating = False

  Sheets("Run").Activate

  RCount = Range(Range("F5000").End(xlUp), Range("F4")).Rows.Count

  For n = 1 To RCount
    test.Name = Sheets("Run").Range("F4").Offset(n - 1, 0)
  Next n

  Application.ScreenUpdating = False

End sub

1条回答
beautiful°
2楼-- · 2019-08-12 19:10

Your operation to create the new worksheet(s) was outside the loop to rename. You would create a new worksheet and rename it a number of times.

Sub new_1()

    Dim RCount As Integer
    Dim n As Integer

      Application.ScreenUpdating = False

      With Sheets("Run")
              RCount = .Range(.Range("F" & Rows.Count).End(xlUp), .Range("F4")).Rows.Count
              For n = 1 To RCount
                Sheets("Security Distribution").Copy After:=Sheets(Sheets.Count)
                Sheets(Sheets.Count).Name = .Range("F4").Offset(n - 1, 0).Value
              Next n
      End With

      Application.ScreenUpdating = False

End Sub
查看更多
登录 后发表回答