A VBA code to create sheets based on a list

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I want to create and rename number of sheets based on values in range of cells, also after that i want to distribute each value of another list in a fixed cell, B39, of each sheet created.
I have already done the first part but i want someone to modify it to not fixed list (or range), i make the code with the range Q4:Q11, how to make the module executable when the rnge changes to Q4:Q7 or Q4:Q20? Thanks
/
VBA Code:
Option Explicit
Sub CreateAndName6s()
    Dim c As Range
    Application.ScreenUpdating = False
    Sheets("Data").Select
    For Each c In Range("Q4:Q11")
        c.Select
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = c.Value
        Sheets("Template").Cells.Copy
        ActiveSheet.Paste
        Range("A1").Select
        Application.CutCopyMode = False
        Range("E4").Select
        ActiveSheet.Range("E4").Value = c.Value
        Sheets("Data").Select
        ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=c.Value & "!A1", TextToDisplay:=c.Value

    Next c
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about
VBA Code:
Sub CreateAndName6s()
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   With Sheets("Data")
      For Each Cl In .Range("Q4", .Range("Q" & Rows.Count).End(xlUp))
         Sheets("Template").Copy , Sheets(Sheets.Count)
         ActiveSheet.Name = Cl.Value
         ActiveSheet.Range("E4").Value = Cl.Value
         .Hyperlinks.Add Cl, "", Cl.Value & "!A1", Cl.Value
      Next Cl
   End With
   Application.ScreenUpdating = True
End Sub
Where are the values that need to go in B39?
 
Upvote 0
How about
VBA Code:
Sub CreateAndName6s()
   Dim Cl As Range
  
   Application.ScreenUpdating = False
   With Sheets("Data")
      For Each Cl In .Range("Q4", .Range("Q" & Rows.Count).End(xlUp))
         Sheets("Template").Copy , Sheets(Sheets.Count)
         ActiveSheet.Name = Cl.Value
         ActiveSheet.Range("E4").Value = Cl.Value
         .Hyperlinks.Add Cl, "", Cl.Value & "!A1", Cl.Value
      Next Cl
   End With
   Application.ScreenUpdating = True
End Sub
Where are the values that need to go in B39?
Value is adjacent to the range Q4:Q11, R4 will bevin the first sheet, R5 in the second, etc.
 
Upvote 0
Ok, how about
VBA Code:
Sub seragrefaat()
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   With Sheets("Data")
      For Each Cl In .Range("Q4", .Range("Q" & Rows.Count).End(xlUp))
         Sheets("Template").Copy , Sheets(Sheets.Count)
         ActiveSheet.Name = Cl.Value
         ActiveSheet.Range("E4").Value = Cl.Value
         ActiveSheet.Range("B39").Value = Cl.Offset(, 1).Value
         .Hyperlinks.Add Cl, "", Cl.Value & "!A1", Cl.Value
      Next Cl
   End With
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub seragrefaat()
   Dim Cl As Range
  
   Application.ScreenUpdating = False
   With Sheets("Data")
      For Each Cl In .Range("Q4", .Range("Q" & Rows.Count).End(xlUp))
         Sheets("Template").Copy , Sheets(Sheets.Count)
         ActiveSheet.Name = Cl.Value
         ActiveSheet.Range("E4").Value = Cl.Value
         ActiveSheet.Range("B39").Value = Cl.Offset(, 1).Value
         .Hyperlinks.Add Cl, "", Cl.Value & "!A1", Cl.Value
      Next Cl
   End With
   Application.ScreenUpdating = True
End Sub
Thanks alot, I will try your code tomorrow, and give you my feedback. ?
 
Upvote 0
Ok, how about
VBA Code:
Sub seragrefaat()
   Dim Cl As Range
  
   Application.ScreenUpdating = False
   With Sheets("Data")
      For Each Cl In .Range("Q4", .Range("Q" & Rows.Count).End(xlUp))
         Sheets("Template").Copy , Sheets(Sheets.Count)
         ActiveSheet.Name = Cl.Value
         ActiveSheet.Range("E4").Value = Cl.Value
         ActiveSheet.Range("B39").Value = Cl.Offset(, 1).Value
         .Hyperlinks.Add Cl, "", Cl.Value & "!A1", Cl.Value
      Next Cl
   End With
   Application.ScreenUpdating = True
End Sub
There is an issue in the code, see screenshots plz, Everything is ok except it stops and when i press debug, i gives me an extra sheet (Template (2))

Thanks
 

Attachments

  • 1_Before the code.png
    1_Before the code.png
    57.3 KB · Views: 6
  • 2_Running the code.png
    2_Running the code.png
    40.8 KB · Views: 6
  • 3_After pressing Debug.png
    3_After pressing Debug.png
    46.4 KB · Views: 6
Upvote 0
What is the value of Cl.Value when you get the error?
 
Upvote 0
That does not tell me what the value was when it failed. What is in the cell below 6-8
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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