Solved

VBA Excel 2000 - Rename sheet if that exist

Posted on 2011-03-25
5
542 Views
Last Modified: 2012-05-11
Dear Experts,

In a file it can happen that there are two sheets "Domestic" and "Export", but also that any of them does not exist or just one of them.

Could you please how to write a macro which would be able to identify what is the actual scenario, and based on that rename any of them to "Total"?

With IFs would be imagined like this
- if "Domestic" exist then rename it to "Total" (if also exist "Export" than just leave it unchanged)
- if "Export" exist then rename it to "Total" (if also exist "Domestic" than just leave it unchanged)
- if any of them does not exist then add a sheet as "Total"

thanks,





0
Comment
Question by:csehz
  • 2
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35214015
Try this

Sub Sample()
    Dim w As Worksheet
    
    If CheckIfSheetExist("Total") = True Then
        MsgBox "There is already a sheet named 'Total'"
        Exit Sub
    End If
    
    If CheckIfSheetExist("Domestic") = True And CheckIfSheetExist("Export") = False Then
        Sheets("Domestic").Name = "Total"
    ElseIf CheckIfSheetExist("Domestic") = False And CheckIfSheetExist("Export") = True Then
        Sheets("Export").Name = "Total"
    ElseIf CheckIfSheetExist("Domestic") = False And CheckIfSheetExist("Export") = False Then
        Set w = Sheets.Add
        w.Name = "Total"
    End If
End Sub

Function CheckIfSheetExist(ws As String) As Boolean
    For Each wsheets In ActiveWorkbook.Sheets
        If UCase(wsheets.Name) = UCase(ws) Then
            CheckIfSheetExist = True
            Exit Function
        End If
    Next
End Function

Open in new window


Sid
0
 
LVL 1

Author Closing Comment

by:csehz
ID: 35214041
Sid thanks very much it works, finally added a line yet if both exist


    ElseIf CheckIfSheetExist("Domestic") = True And CheckIfSheetExist("Export") = True Then
        Sheets("Export").Name = "Total"
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35214050
While I see you have close this I would do this a little differently

1) test for the existence of "Total" before causinbg an error by duplicating the name
2) you can test for the sheet names directly rather than looping through all the sheet names

Cheers

Dave
Sub SheetNames()
    Dim wsx As Worksheet
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    On Error Resume Next
    Set wsx = Sheets("Total")
    Set ws1 = Sheets("Domestic")
    Set ws1 = Sheets("Export")
    On Error GoTo 0
    If Not wsx Is Nothing Then
        MsgBox "you already have a sheet called nothing", vbCritical
        Exit Sub
    End If

    If Not ws1 Is Nothing Then
        ws1.Name = "Total"
    Else
        If Not ws2 Is Nothing Then
            ws2.Name = "Total"
        Else
            Set ws3 = Sheets.Add
            ws3.Name = "Total"
        End If
    End If
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:csehz
ID: 35214080
Dave thanks, sorry that closed the topic so can not give points, your code also works and moreover automatically handles the both existing scenarios correctly which I left out from the IF listings at the question :-))
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 35214085
No probs :)

You closed as I was finalising the code so I'd figured I'd carry though with it

Cheers

Dave
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Manipulation Macro 28 36
Sum Based On Criteriya 7 19
Vb.net dynamic formulas in runtime 11 61
File not loading into PowerPivot 4 9
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now