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
 
That does not tell me what the value was when it failed. What is in the cell below 6-8
the cell is Blank.
The range from Q4:Q25 has a formula which gives the values, and when the formula is false it gives nothing. It works fine in my Code
 

Attachments

  • 6_code.png
    6_code.png
    5.8 KB · Views: 2
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The range from Q4:Q25 has a formula which gives the values, and when the formula is false it gives nothing
That's a crucial bit of info you missed out & if the cell is empty then your code will also fail.
Try
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))
         If cl.Value <> "" Then
            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
         End If
      Next cl
   End With
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
That's a crucial bit of info you missed out & if the cell is empty then your code will also fail.
Try
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))
         If cl.Value <> "" Then
            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
         End If
      Next cl
   End With
   Application.ScreenUpdating = True
End Sub
Perfect, thanks a ton,
I have more couple to ask, can i make a command button and put the code in it, and when i click the button it will trigger the code and create sheets?
Also, can i repeat the code 3 more time with column S, U, W in one code?
Last thing, the hyperlink does not work, can you modify it to activate the sheet when i click on the linked cell in sheet Data?

Thanks a lot Fluff
 
Upvote 0
In what way doesn't the hyperlink work?
 
Upvote 0
Ok, try
VBA Code:
            .Hyperlinks.Add cl, "", "'" & cl.Value & "'!A1", cl.Value
 
Upvote 0
Just add the part in blue
Rich (BB code):
      Next cl
      .Activate
   End With
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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