Results 1 to 4 of 4

Thread: [RESOLVED] Excel Macro In Vb

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Resolved [RESOLVED] Excel Macro In Vb

    CAN ANYONE HELP ME?

    IN THE FOLLWING EXCEL MACRO, I WANT THE MACRO TO CHECK IF THERE IS ALREADY A SHEET BY THE NAME OF "AHTBARCHART" IF THERE IS THEN IT SHOULD DISPLAY A POPUP WINDOW "CHART ALREADY EXISTS" ELSE IT SHOULD GO AHEAD AND CREATE A CHART AS MENTIONED BELOW.


    VB Code:
    1. Sub AHTBARCHART()
    2. '
    3. ' AHTBARCHART Macro
    4. '
    5.  
    6. '
    7.    Range("D3:D13,F3:F13").Select
    8.    Range("F3").Activate
    9.    Charts.Add
    10.    ActiveChart.ChartType = xlColumnClustered
    11.    ActiveChart.SetSourceData
    12. Source:=Sheets("JANUARY").Range("D3:D13,F3:F13"), _
    13.        PlotBy:=xlColumns
    14.    [U]ActiveChart.Location Where:=xlLocationAsNewSheet,
    15. Name:="AHTBARCHART"[/U]
    16.    With ActiveChart
    17.        .HasTitle = True
    18.        .ChartTitle.Characters.Text = "AVERAGE AHT PER TEAM LEADER"
    19.        .Axes(xlCategory, xlPrimary).HasTitle = True
    20.        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "TEAM
    21. LEADER"
    22.        .Axes(xlValue, xlPrimary).HasTitle = True
    23.        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "AVERAGE
    24. AHT"
    25.    End With
    26.    Sheets("JANUARY").Select
    27.    Range("A1").Select
    28.    Sheets("AHTBARCHART").Select
    29. End Sub
    Last edited by Siddharth Rout; Feb 7th, 2006 at 07:54 PM. Reason: RESOLVED

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel Macro In Vb

    Add the following at the top of the code..

    VB Code:
    1. Dim i As integer
    2. Dim SheetFound As Boolean
    3. For i = 1 to Sheets.Count
    4.   If Sheets(i).Name = "AHTBARCHART" Then
    5.     SheetFound
    6.     Exit Do
    7.   End If
    8. Next i
    9. If SheetFound Then
    10.   Msgbox "your Message"
    11.   Exit Sub
    12. End If
    13. 'Otherwise contiue on..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Macro In Vb

    Here is another way to do it that does not require iterating through all the sheets, though any time saved would not likely be noticeable. This is just a template to show you the code flow ...
    Code:
    Option Explicit
    Sub Macro1()
        Dim i As Integer
        
        On Error GoTo ERR_NO_SHEET
        i = ActiveWorkbook.Sheets("Sheet3").Index
        
        'Here is where processing resumes after the Error Handler is done
        On Error GoTo 0
        MsgBox "Sheet 3 Exists and will be used"
        Exit Sub
        
    ERR_NO_SHEET:
        'Here is where you would create the new sheet
        MsgBox "There is no Sheet 3"
        Resume Next
    
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel Macro In Vb

    THANKS DANNYMKING

    YOUR CODE WAS A GREAT HELP. THOUGH I HAD TO MAKE A FEW CHANGES LIKE

    1) SHEETFOUND= TRUE in the if-endif loop
    2) 'EXIT FOR' INSTEAD OF Exit Do

    and it worked like a miracle. thanks a ton once again :-)



    Quote Originally Posted by dannymking
    Add the following at the top of the code..

    VB Code:
    1. Dim i As integer
    2. Dim SheetFound As Boolean
    3. For i = 1 to Sheets.Count
    4.   If Sheets(i).Name = "AHTBARCHART" Then
    5.     SheetFound
    6.     Exit Do
    7.   End If
    8. Next i
    9. If SheetFound Then
    10.   Msgbox "your Message"
    11.   Exit Sub
    12. End If
    13. 'Otherwise contiue on..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width