Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2006, 02:12 PM
Authorized User
 
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel vba, assigning a Name to a sheet

I found the following code to be useful when I need to copy a worksheet from and to whithin a workbook. However, I cannot find a way to name newly copied sheets as desired using the following code. All works except the line below sht.Name = ml

This returns the error "Variable or with block not set."

If I remove the line, all sheets are created. I figure, If I can create then, I should be able to rename them.

THE CODE:


Sub CopyX_click()


Dim Sht As Worksheet, i As Integer
Dim iCount As Integer
iCount = Worksheets.Count
Application.ScreenUpdating = False
ml = ("act")

        For i = 1 To 3
            'Set Sht = Sheets("Sheet3")
            Worksheets("sheet3").Copy After:=Sheets(iCount)
            iCount = iCount + 1
            Sht.Name = ml
        Next

        Set Sht = Nothing
        Application.ScreenUpdating = True

End Sub

Is there anyone able in answering this one?

Help!

 
Old May 15th, 2006, 03:57 AM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Of course is there anyone to help you :D


this code should run:

Dim Sht As Worksheet, i As Integer
Dim iCount As Integer
iCount = Worksheets.Count
Application.ScreenUpdating = False
ml = ("act")

        For i = 1 To 3
            'Set Sht = Sheets("Sheet3")
            Worksheets("sheet3").Copy After:=Sheets(iCount)
            iCount = iCount + 1
            ActiveSheet.Name = ml
        Next

        Set Sht = Nothing
        Application.ScreenUpdating = True

regards
crack

I hope this is good english, because this tongue is not my native language.
Please improve me if my syntax is wrong.
Thanks
 
Old October 31st, 2015, 07:29 AM
Authorized User
 
Join Date: Oct 2015
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
Talking Excel vba, assigning a Name to a sheet

Sub WatchMeWhipNayNay()
Dim WB As Workbook, WS As Worksheet
Set WB = Application.ActiveWorkbook
Set WS = WB.ActiveSheet

'Get Last Sheet Position
LastSheetCount = WB.Worksheets.Count

'Add New Sheet after Last Sheet Position
WB.Worksheets.Add After:=WB.Sheets(LastSheetCount)

' Get the Last Sheet, factoring the new sheet
ExpandedSheetsCount = WB.Worksheets.Count

'Get the Current Name of the Last Sheet
NewSheetName = WB.Sheets(ExpandedSheetsCount).Name

'Change the Name of the Last Sheet to the desired Name.
WB.Sheets(ExpandedSheetsCount).Name = "Watch Me Whip, Watch me Nay Nay"
'Or WB.Worksheets(NewSheetName).Name = "Watch Me Whip, Watch me Nay Nay"
End Sub
 
Old October 31st, 2015, 07:32 AM
Authorized User
 
Join Date: Oct 2015
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
Default

@stepdev & @Crack - Your code generates an error for the simple reason below.

Each iteration of the for loop creates a sheet then gives it a name, ml = "act"
This will generate an error after the 1st iteration as a sheet named "act" already exists, after the 1st iteration.

The only way out is to use this : ActiveSheet.Name = ml + iCount
This way each name is unique.

If you plan to use your code elsewhere, I advise you don't hard-code the upper limit of your sheets to 3, in fact I'd completely do away with the for-loop in favor of the ForEach-loop over the sheets collection.

Note: I didn't verify if you 1st need to cast the iCount variable into string before adding to to ml string variable but I do not think it is necessary.

Last edited by Zakalwe; October 31st, 2015 at 07:45 AM.. Reason: Left part out.





Similar Threads
Thread Thread Starter Forum Replies Last Post
vba work outlook with excel sheet qunatphil Excel VBA 1 April 16th, 2008 12:30 PM
VBA code to email excel status sheet daily basis ramavtar Excel VBA 0 December 9th, 2006 05:28 AM
write multi-sheet Excel w/o Excel.Application manmoth Classic ASP Components 2 November 22nd, 2005 10:56 AM
why this sheet has lost VBA Project,ActiveX and ?? bq_wang Excel VBA 1 April 7th, 2005 10:21 AM
assigning a css style sheet from an element miguel XSLT 7 August 8th, 2004 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.