|
-
Aug 10th, 2012, 04:48 AM
#1
Thread Starter
New Member
VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming WS]
Hello
I wish to design a VBA that allows me to alter the number of sheets in a template excel file
The template excel file contains a set of blank sheets, and a final summary sheet at the end, hence I wish the VBA to adjust the number before the summary sheet
If possible, I would also like the VBA to rename all the blank to the specific dates ie ddmmyyyy
Ideally, the VBA is designed within excel, and I could enter the number of blank sheet I want, and the range of the dates, and the VBA detects it and adjust my template sheet automatically
Many thanks
Thsi is what I have so far, I'm a programming noob
Sub newsheet()
Dim newsheet As Worksheet
Set wks = Worksheets.Add(after:=Worksheets(Worksheets.Count - 1))
wks.Name = "Sheet" & Worksheets.Count + 1
With wks
.Cells(1, 1) = "TTL Corp."
.Cells.Font.Bold = True
.Cells(1, 6) = Date
End With
End Sub
-
Aug 10th, 2012, 05:13 AM
#2
Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming
A very handy way of getting an idea of the code you need is to do what you want while recording a macro. Then you can look at the code that was generated. About half way down this link in the "How do I write code to … ? [a.k.a. recording macro's]" section is an explanation.
http://www.vbforums.com/showthread.p...6-(or-VB5-VBA)
-
Aug 10th, 2012, 05:14 AM
#3
Thread Starter
New Member
Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming
 Originally Posted by TysonLPrice
A very handy way of getting an idea of the code you need is to do what you want while recording a macro. Then you can look at the code that was generated. About half way down this link in the "How do I write code to … ? [a.k.a. recording macro's]" section is an explanation.
http://www.vbforums.com/showthread.p...6-(or-VB5-VBA)
Thank you, will give that a go
-
Aug 10th, 2012, 05:24 AM
#4
Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming
try like
Code:
cnt = 'number of sheets to add
strtdate = ' date for first sheet name
for i = 1 to cnt
Set wks = Worksheets.Add(after:=Worksheets(Worksheets.Count - 1))
wks.name = format(strtdate, "ddmmyyyy")
strtdat = strtdate + 1
next
where strtdate is a date variable, cnt can be from a textbox, inputbox or any other user input
sheets are, in this case, incremented daily, if you prefer weekly use + 7 instead, etc
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 10th, 2012, 05:30 AM
#5
Re: VBA coding help [Adding new worksheets before the last sheet in Excel | Renaming
VBA question moved to Office Development
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|