Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-25-2016, 05:50 AM
rod147 rod147 is offline Setting a Worksheet Name as a Variable Help Windows XP Setting a Worksheet Name as a Variable Help Office 2007
Novice
Setting a Worksheet Name as a Variable Help
 
Join Date: Oct 2009
Posts: 3
rod147 is on a distinguished road
Default Setting a Worksheet Name as a Variable Help

I'm modifying some existing code to clear the contents of worksheet "RDBMerge" and then to set this worksheet as a variable called DestSh.Name
I've managed to clear the contents, but I need help with the second part to set it as a variable instead of creating a new RDBMerge worksheet.


' Clear the summary sheet.
Application.DisplayAlerts = False
On Error Resume Next


Sheets("RDBMergeSheet").UsedRange.ClearContents
On Error GoTo 0
Application.DisplayAlerts = True

' Add a new summary worksheet.
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
Reply With Quote
  #2  
Old 11-25-2016, 06:48 AM
NoSparks NoSparks is offline Setting a Worksheet Name as a Variable Help Windows 7 64bit Setting a Worksheet Name as a Variable Help Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Use the VBA code name of the sheet so you will always be dealing with the same sheet no matter what the current tab name is.
Code:
Dim DestSh As String

DestSh = "The_New_Name"

With Sheet1     '<~~~~ the sheet's code name
    .UsedRange.ClearContents
    .Name = DestSh
End With
Reply With Quote
  #3  
Old 11-25-2016, 08:10 AM
rod147 rod147 is offline Setting a Worksheet Name as a Variable Help Windows XP Setting a Worksheet Name as a Variable Help Office 2007
Novice
Setting a Worksheet Name as a Variable Help
 
Join Date: Oct 2009
Posts: 3
rod147 is on a distinguished road
Default

Nosparks, Highlighted DestSh is now coming up as Invalid qualifier.
Originally I had "Dim DestSh As Worksheet" rather than "string"?



Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As String
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

' Clear the summary sheet.
DestSh = "RDBMergeSheet"
With Sheet26 '<~~~~ the sheet's code name
.UsedRange.ClearContents
.Name = DestSh
End With

' Fill in the start row.
StartRow = 7

' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
Reply With Quote
  #4  
Old 11-25-2016, 08:50 AM
NoSparks NoSparks is offline Setting a Worksheet Name as a Variable Help Windows 7 64bit Setting a Worksheet Name as a Variable Help Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Remove .Name from DestSh.Name

What that line is saying...
If the sheet name of the sheet being considered is not equal to the variable DestSh
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge Berryblue Mail Merge 1 11-13-2014 05:36 PM
Setting a Worksheet Name as a Variable Help Run-time error 91 object variable or with block variable not set JUST ME Word VBA 4 03-25-2014 06:56 AM
Setting a Worksheet Name as a Variable Help object variable or with block variable not set MJP143 Excel 1 02-11-2013 05:07 AM
Variable to rename a tab in a worksheet via VBA Chayes Excel Programming 5 08-07-2012 02:03 AM
Setting a Worksheet Name as a Variable Help Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft