how to generate and add code with code

supper's ready

New Member
Joined
Jun 3, 2011
Messages
23
hello,

i have the following code:
Code:
Sub MakeSheets()

Dim ListofNames As Range
Dim c As Range

Set ListofNames = ActiveSheet.[B3:B17]

For Each c In ListofNames
If c.Text <> vbNullString Then
On Error GoTo InvalidName
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
NextC:
End If
Next

Exit Sub

InvalidName:
MsgBox """" & c.Value & """ is an invalid sheet name.", vbOKOnly + vbCritical, "Add Sheets"
Resume NextC

End Sub

i want to add code that will add an if statement to a sub called Sub CreateWorkbooks() (in module 1) for each instance of the loop.

The if statement will take a column address from C3:C17 (where c3 corresponds to b3 instance etc) to define a variable.
heres what the if statement looks like:
Code:
If Sheet.Name = "value from b3 Then
Columns("B:B").Select 'B:B comes from value in C3
Selection.NumberFormat = "m/d/yyyy"
dtMin = WorksheetFunction.Min(Columns("B:B"))
dtMax = WorksheetFunction.Max(Columns("B:B"))
DayMin = Day(dtMin)
DayMax = Day(dtMax)
End If

please please help! thanks!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I'm looking at this and wondering if I've missed something.

Why would you want to hard code it this way? Surely you can pull the relevant info from the cell on the fly and work with it at run time?
 
Upvote 0
the if statement will go to another macros which will be run at a different time. and the data in that worksheet will be entered in the future and will change. also the variables to be assigned get changed because its all part of a loop
 
Last edited:
Upvote 0
yeah i was thinking about it yesterday and realized that is the best way to do it.

i just need to figure out how to write an if statement like:

if sheet.name in some range then

what's the code for that lol? once i figure that out, i got the rest :) thanks for the help.
 
Upvote 0
If it's just 1 cell

Code:
if sheet.name=range("B3").value then

or maybe if it's a range

Code:
if worksheetfunction.countif(range("B3:B20"),sheet.name) > 0 then
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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