Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 08-05-2012, 05:36 PM
macropod's Avatar
macropod macropod is offline Variable to rename a tab in a worksheet via VBA Windows 7 64bit Variable to rename a tab in a worksheet via VBA Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'd prefer to work with sheet names. That way, if someone re-orders the worksheets, the code won't try to process the wrong one. To ensure the code always processes a sheet named "File 1", even if that sheet starts out named "Sheet1", you could use code like:
Code:
Sub Demo()
Dim xlWs As Worksheet, bFound1 As Boolean, bFound2 As Boolean
With ThisWorkbook
  bFound1 = False
  For Each xlWs In .Worksheets
    With xlWs
      If .Name = "File 1" Then
        bFound1 = True
        MsgBox .Range("A1").Value
        Exit For
      ElseIf .Name = "Sheet1" Then
        bFound2 = True
      End If
    End With
  Next
  If bFound1 = False And bFound2 = True Then
    .Worksheets("Sheet1").Name = "File 1"
    MsgBox .Worksheets("File 1").Range("A1").Value
  End If
End With
End Sub
Note also that nothing needs to be selected.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Rename a Quick Part sleake Word 14 10-14-2013 09:21 AM
Variable to rename a tab in a worksheet via VBA Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM
Rename Files gsrikanth Excel Programming 3 05-14-2012 03:03 AM
Variable to rename a tab in a worksheet via VBA "Auto-populating" data-worksheet to worksheet. meggenm Excel 4 02-04-2012 02:04 AM
Variable to rename a tab in a worksheet via VBA Rename File cksm4 Word VBA 2 02-25-2011 09:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:20 AM.


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