Dynamic Range

Jabberwokki

New Member
Joined
Dec 2, 2020
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
Hi There And apologies cos i'm sure this has been answered hundreds of times before.

I'm trying to automate the creation of sheets in VBA using sheet names from a dynamic range of cells. The first cell containing a name is B14 and the last cell in the same column is dependent on a numeric value in cell D12 (this can vary).

This is what i've written so far but i'm getting an error when executing the code 'Wrong number of arguments or invalid property assignment'. Any help would be much appreciated.

Regards

Dominic

heres the code..........

Sub Add_Sheets_from_Cell_Value()


Sheets("Front").Select


Dim sRange

Dim eRange

Dim xRange As Range

Dim qq As Range



Set sRange = Range("B14")

eRange = Range("D12").Value



Set xRange = Range(sCell & eCell)



Application.ScreenUpdating = False



For Each qq In xRange

On Error Resume Next ' This will ignore any error

Sheets.Add(After:=Sheets(Sheets.Count)).Name = qq.Value



Next qq



Application.ScreenUpdating = True

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If you wanted to create sheets using the values in B14:B17 what would the value in D12 be?
 
Upvote 0
If you wanted to create sheets using the values in B14:B17 what would the value in D12 be?
Hi Fluff

Thanks for getting back to me. The start cell for the range is cell B14. D12 is a variable number and i want the VBA to count down the number of cells specified in cell D12 from B14.

Regards

Dominic
 
Upvote 0
Does that mean the answer to my question is 4?
 
Upvote 0
Ok, how about
VBA Code:
Sub jabberwokki()
   Dim Cl As Range
   
   With Sheets("Front")
      For Each Cl In .Range("B14").Resize(.Range("D12").Value - 1)
         If Cl.Value <> "" Then
            If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
               Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            End If
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub jabberwokki()
   Dim Cl As Range
  
   With Sheets("Front")
      For Each Cl In .Range("B14").Resize(.Range("D12").Value - 1)
         If Cl.Value <> "" Then
            If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
               Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            End If
         End If
      Next Cl
   End With
End Sub
That works a treat except it produces 1 less sheet than the number specified in cell D12. Is that because of the '-1' in row 5 of the code?
 
Upvote 0
Oops, yes. It needs to be removed.
 
Upvote 0
Solution
That works a treat except it produces 1 less sheet than the number specified in cell D12. Is that because of the '-1' in row 5 of the code?
So it should read:

For Each Cl In .Range("B14").Resize(.Range("D12").Value

?
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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