PC Review


Reply
Thread Tools Rate Thread

Command Button misfire

 
 
Bishop
Guest
Posts: n/a
 
      22nd May 2009
The below code creates a toolbar, adds a button to that toolbar and assigns a
sub to that button. When I name a specific workbook, say ("Bothell C&A PF
May wk 1 bm"), it works fine when I run it from within VBE. But when I try
to use a wild card, say ("*C&A PF*"), I get a run-time '9' error. Also, if I
try to use the actual button from a spreadsheet it says it can't find X
spreadsheet and then lists the name. But the name it lists is not even a
spreadsheet I have open or have used in a long time. I'm using 2007 so the
button is in Add-Ins. I would like to make this work from any spreadsheet.

Sub CatalystDumpToolBar()

Dim CDToolBar As CommandBar

Set CDToolBar = CommandBars.Add(temporary:=True)
With CDToolBar
.Name = "CDToolBar"
.Position = msoBarTop
.Visible = True
End With
End Sub

Sub CatalystToTally()

Dim wb As Workbook
Dim ws As Worksheet
Dim CDLastRow As Long 'Catalyst Dump
Dim EDLastRow As Long 'Exported Data

'This doesn't work
Workbooks("*C&A PF*.xls").Activate
'This does work... how can I use wildcards for this?
Workbooks("Bothell C&A PF May 09 wk1 bm.xls").Activate
CDLastRow = Workbooks("*C&A PF*.xlsm").Worksheets _
("Catalyst Dump").Cells(Rows.Count, "A").End(xlUp).Row
Worksheets("Catalyst Dump").Columns("D").ColumnWidth = 13

For Each wb In Workbooks
'Test to see if wb's name is like "ExportedData*"
If wb.Name Like "ExportedData*" Then
'Create a worksheet object to reference the appropriate
'worksheet in the wb
Set ws = wb.ActiveSheet

With ws
.Rows("1:1").Delete Shift:=xlUp
EDLastRow = .Range("A" & Rows.Count).End(xlUp).Row
.Columns("D").ColumnWidth = 13
.Columns("D").NumberFormat = "0"
.Rows("1:" & EDLastRow).Copy ThisWorkbook.Worksheets _
("Catalyst Dump").Rows(CDLastRow + 1)
End With
wb.Close savechanges:=False
End If
Next
End Sub

Sub AddCustomControl()

Dim CBar As CommandBar
Dim CTTally As CommandBarControl
Dim PFNum As CommandBarControl

Set CBar = CommandBars("CDToolBar")
Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
Set PFNum = CBar.Controls.Add(Type:=msoControlButton)

With CTTally
.FaceId = 1763
.OnAction = "CatalystToTally"
End With

With PFNum
.FaceId = 643
.OnAction = "PFNumber"
End With

CBar.Visible = True

End Sub
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      22nd May 2009
Bishop, If you are sure there is only one workbook by that name and want to
activate the workbook with a keyword paste the below function and call from
code..to activate..Try and feedback..

Call ActivateBookwithKeyword ("C&A PF")


Sub ActivateBookwithKeyword(strSearch As String)
For intTemp = 1 To Workbooks.Count
If InStr(1, Workbooks(intTemp).Name, strSearch, 1) <> 0 Then
Workbooks(intTemp).Activate
Exit Sub
End If
Next
End Sub
--
If this post helps click Yes
---------------
Jacob Skaria


"Bishop" wrote:

> The below code creates a toolbar, adds a button to that toolbar and assigns a
> sub to that button. When I name a specific workbook, say ("Bothell C&A PF
> May wk 1 bm"), it works fine when I run it from within VBE. But when I try
> to use a wild card, say ("*C&A PF*"), I get a run-time '9' error. Also, if I
> try to use the actual button from a spreadsheet it says it can't find X
> spreadsheet and then lists the name. But the name it lists is not even a
> spreadsheet I have open or have used in a long time. I'm using 2007 so the
> button is in Add-Ins. I would like to make this work from any spreadsheet.
>
> Sub CatalystDumpToolBar()
>
> Dim CDToolBar As CommandBar
>
> Set CDToolBar = CommandBars.Add(temporary:=True)
> With CDToolBar
> .Name = "CDToolBar"
> .Position = msoBarTop
> .Visible = True
> End With
> End Sub
>
> Sub CatalystToTally()
>
> Dim wb As Workbook
> Dim ws As Worksheet
> Dim CDLastRow As Long 'Catalyst Dump
> Dim EDLastRow As Long 'Exported Data
>
> 'This doesn't work
> Workbooks("*C&A PF*.xls").Activate
> 'This does work... how can I use wildcards for this?
> Workbooks("Bothell C&A PF May 09 wk1 bm.xls").Activate
> CDLastRow = Workbooks("*C&A PF*.xlsm").Worksheets _
> ("Catalyst Dump").Cells(Rows.Count, "A").End(xlUp).Row
> Worksheets("Catalyst Dump").Columns("D").ColumnWidth = 13
>
> For Each wb In Workbooks
> 'Test to see if wb's name is like "ExportedData*"
> If wb.Name Like "ExportedData*" Then
> 'Create a worksheet object to reference the appropriate
> 'worksheet in the wb
> Set ws = wb.ActiveSheet
>
> With ws
> .Rows("1:1").Delete Shift:=xlUp
> EDLastRow = .Range("A" & Rows.Count).End(xlUp).Row
> .Columns("D").ColumnWidth = 13
> .Columns("D").NumberFormat = "0"
> .Rows("1:" & EDLastRow).Copy ThisWorkbook.Worksheets _
> ("Catalyst Dump").Rows(CDLastRow + 1)
> End With
> wb.Close savechanges:=False
> End If
> Next
> End Sub
>
> Sub AddCustomControl()
>
> Dim CBar As CommandBar
> Dim CTTally As CommandBarControl
> Dim PFNum As CommandBarControl
>
> Set CBar = CommandBars("CDToolBar")
> Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
> Set PFNum = CBar.Controls.Add(Type:=msoControlButton)
>
> With CTTally
> .FaceId = 1763
> .OnAction = "CatalystToTally"
> End With
>
> With PFNum
> .FaceId = 643
> .OnAction = "PFNumber"
> End With
>
> CBar.Visible = True
>
> End Sub

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      22nd May 2009
Probably going to need a For ...Each loop. Something like:

For Each wb In Application.Workbooks
If wb.Name Like "*C&A PF*.xls" Then
wb.Activate
End If
Next


"Bishop" <(E-Mail Removed)> wrote in message
news:40086381-4361-48F7-8B58-(E-Mail Removed)...
> The below code creates a toolbar, adds a button to that toolbar and
> assigns a
> sub to that button. When I name a specific workbook, say ("Bothell C&A PF
> May wk 1 bm"), it works fine when I run it from within VBE. But when I
> try
> to use a wild card, say ("*C&A PF*"), I get a run-time '9' error. Also,
> if I
> try to use the actual button from a spreadsheet it says it can't find X
> spreadsheet and then lists the name. But the name it lists is not even a
> spreadsheet I have open or have used in a long time. I'm using 2007 so
> the
> button is in Add-Ins. I would like to make this work from any
> spreadsheet.
>
> Sub CatalystDumpToolBar()
>
> Dim CDToolBar As CommandBar
>
> Set CDToolBar = CommandBars.Add(temporary:=True)
> With CDToolBar
> .Name = "CDToolBar"
> .Position = msoBarTop
> .Visible = True
> End With
> End Sub
>
> Sub CatalystToTally()
>
> Dim wb As Workbook
> Dim ws As Worksheet
> Dim CDLastRow As Long 'Catalyst Dump
> Dim EDLastRow As Long 'Exported Data
>
> 'This doesn't work
> Workbooks("*C&A PF*.xls").Activate
> 'This does work... how can I use wildcards for this?
> Workbooks("Bothell C&A PF May 09 wk1 bm.xls").Activate
> CDLastRow = Workbooks("*C&A PF*.xlsm").Worksheets _
> ("Catalyst Dump").Cells(Rows.Count, "A").End(xlUp).Row
> Worksheets("Catalyst Dump").Columns("D").ColumnWidth = 13
>
> For Each wb In Workbooks
> 'Test to see if wb's name is like "ExportedData*"
> If wb.Name Like "ExportedData*" Then
> 'Create a worksheet object to reference the appropriate
> 'worksheet in the wb
> Set ws = wb.ActiveSheet
>
> With ws
> .Rows("1:1").Delete Shift:=xlUp
> EDLastRow = .Range("A" & Rows.Count).End(xlUp).Row
> .Columns("D").ColumnWidth = 13
> .Columns("D").NumberFormat = "0"
> .Rows("1:" & EDLastRow).Copy ThisWorkbook.Worksheets _
> ("Catalyst Dump").Rows(CDLastRow + 1)
> End With
> wb.Close savechanges:=False
> End If
> Next
> End Sub
>
> Sub AddCustomControl()
>
> Dim CBar As CommandBar
> Dim CTTally As CommandBarControl
> Dim PFNum As CommandBarControl
>
> Set CBar = CommandBars("CDToolBar")
> Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
> Set PFNum = CBar.Controls.Add(Type:=msoControlButton)
>
> With CTTally
> .FaceId = 1763
> .OnAction = "CatalystToTally"
> End With
>
> With PFNum
> .FaceId = 643
> .OnAction = "PFNumber"
> End With
>
> CBar.Visible = True
>
> End Sub



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print command from command button in userform causes double chart Mike Jamesson Microsoft Excel Programming 5 11th Aug 2009 03:42 AM
BUG: print command from command button in userform causes double c Mike Jamesson Microsoft Excel Programming 0 10th Aug 2009 04:19 PM
Deselect Command Button by Selecting another Command Button gmcnaugh Microsoft Excel Programming 3 2nd Sep 2008 05:59 PM
Command Button Pictures Taken from Command Bar Button Icons acx@centrum.cz Microsoft Access Forms 0 2nd Dec 2007 12:23 PM
IE6 misfire !!! Boris Windows XP Internet Explorer 1 7th Feb 2004 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 PM.