Custom Toolbar button not running full sub

ollienashchapman

New Member
Joined
May 15, 2014
Messages
4
Hi All,

I have created a Sub which does a few things:

1) Ask for a string
2) Queries a db with said string
3) Creates a new sheet on active workbook with the name of said string
4) Past the recordset in the new sheet

If I run the Macro on its own it does everything fine. However i have now created a Custom Toolbar button and assigned the sub to this button. When I run the macro from the button it gets the data from the database but does not create the new sheet. I think it has something to do with my VBA for the command bar?

Any help would be appreciated

Thanks for your time!

Code:

MAIN SUB
Code:
Public Sub GetSolaComp()

'Get product RIC code


myRIC = InputBox("Please provide Index/ETF")


'Get results from the database


GetAllRecordsFromDB (myRIC)


'Creating new sheet named by product


CreateSheet (myRIC)


'Copy recordset to excel sheet


Range("A2").CopyFromRecordset objMyRecordset
 
'Get table headings
 
For intColIndex = 0 To objMyRecordset.Fields.Count - 1
    Range("A1").Offset(0, intColIndex).Value = objMyRecordset.Fields(intColIndex).Name
Next


End Sub
CREATE SHEET FUNCTION

Code:
Public Function CreateSheet(tempRic As String)

'On Error GoTo endThis


Dim ws As Worksheet
sNane = Application.ActiveWorkbook.Name
Application.Workbooks(sNane).Activate
Set ws = Application.ActiveWorkbook.Sheets.Add
    ws.Name = tempRic
    
'endThis:
'   MsgBox "Problems with creating new sheet called " & tempRic & " - " & Err.Description
'End


End Function
CREATING EXCEL COMMAND TOOLBAR BUTTON

Code:
Private Sub Workbook_Open()Start
End Sub


Public Sub Start()


ActiveWorkbookName = Application.ActiveWorkbook.Name
ThisWorkbook.Activate
For Each Bar In Application.CommandBars
    If Bar.Name = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) Then Bar.Delete
Next


Set mybar = CommandBars _
    .Add(Name:=Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4), _
             Position:=msoBarTop, Temporary:=False)
With mybar
   Set newItem = .Controls.Add(Type:=msoControlButton, ID:=1742, Before:=1)
   With newItem
       .Caption = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
       .OnAction = ThisWorkbook.Path & "\" & ThisWorkbook.Name & "!GetSolaComp"
       .Style = msoButtonIconAndCaption
   End With
   .Visible = True
End With


Application.Workbooks(ActiveWorkbookName).Activate


End Sub
 

Forum statistics

Threads
1,081,972
Messages
5,362,495
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top