Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Worksheet Functions / March 2007

Tip: Looking for answers? Try searching our database.

Command Button Error - Help Needed Urgently Please

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Salman - 07 Mar 2007 08:33 GMT
A. Below is the code that work perfectly when i run it through macro but not
working through command button, i have refer to other post but cant change it
as per my need, help required.

B. after running whole macro print area left the grand total out of the
range, if any correction needed, please advise.

Command button is at Summary Sheet where data is required and to be formatted.

below is the code:

Private Sub CommandButton1_Click()

   Sheets("Scrip Position").Select

   Range("B7").Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.ClearContents
   Range("C1").Select
   
   Dim CurWks As Worksheet
   Dim NewWks As Worksheet
   Dim FirstRow As Long
   Dim LastRow As Long
   Dim iRow As Long
   Dim HowMany As Long
   Dim DestCell As Range

   Set CurWks = Worksheets("Scrip Position")
   Set NewWks = Worksheets("Scrip Position")
   Set DestCell = NewWks.Range("B7")

   With CurWks
       FirstRow = 1 'no headers??
       LastRow = .Cells(.Rows.Count, "w").End(xlUp).Row
        For iRow = FirstRow To LastRow
           HowMany = .Cells(iRow, "v").Value
           DestCell.Resize(HowMany, 1).Value = .Cells(iRow, "w").Value
           
            On Error Resume Next
           Set DestCell = DestCell.Offset(HowMany, 0)
       Next iRow
   End With
   
   Sheets("Summary").Select
   Range("C1").Select

   Selection.AutoFilter Field:=2
   Cells.Select
   Range("B1").Activate
   Selection.RemoveSubtotal
   Range("B5").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 11,
14, _
       15, 16), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Range("B5").Select
   Selection.AutoFilter Field:=2, Criteria1:="=*Total*", Operator:=xlAnd
   Rows("7:1046").Select
   Range("B7").Activate
   With Selection.Interior
       .ColorIndex = 15
       .Pattern = xlSolid
   End With
   Selection.Font.Bold = False
   Selection.Font.Bold = True
   Selection.AutoFilter Field:=2
   Selection.AutoFilter Field:=2, Criteria1:="=*Grand*", Operator:=xlAnd
   Range("E996").Select
   Selection.Font.ColorIndex = 2
   Range("L996").Select
   Selection.Font.ColorIndex = 2
   Rows("519:519").Select
   Range("B519").Activate
   Selection.Interior.ColorIndex = xlNone
   Selection.Borders(xlDiagonalDown).LineStyle = xlNone
   Selection.Borders(xlDiagonalUp).LineStyle = xlNone
   Selection.Borders(xlEdgeLeft).LineStyle = xlNone
   With Selection.Borders(xlEdgeTop)
       .LineStyle = xlContinuous
       .Weight = xlThin
       .ColorIndex = xlAutomatic
   End With
   With Selection.Borders(xlEdgeBottom)
       .LineStyle = xlDouble
       .Weight = xlThick
       .ColorIndex = xlAutomatic
   End With
   Selection.Borders(xlEdgeRight).LineStyle = xlNone
   Selection.Borders(xlInsideVertical).LineStyle = xlNone
   Range("B5").Select
   Selection.AutoFilter Field:=2
   Selection.AutoFilter Field:=2, Criteria1:="<>"
   Selection.End(xlDown).Select
   'ActiveWindow.SmallScroll Down:=957
   ActiveSheet.PageSetup.PrintArea = "$B$1:$Q$519"
   Selection.End(xlUp).Select
   Selection.End(xlToLeft).Select
   Selection.End(xlUp).Select
End Sub
Joel - 07 Mar 2007 09:53 GMT
I don't know what the data on the worksheet should look like. but was able to
step through code with only one error.  The On Error statement is in the
wrong location.  You also shouold return the error checking back to normal
with the On Error go to 0 statement.

      For iRow = FirstRow To LastRow
           HowMany = .Cells(iRow, "v").Value
            On Error Resume Next
           
           DestCell.Resize(HowMany, 1).Value = .Cells(iRow, "w").Value
           Set DestCell = DestCell.Offset(HowMany, 0)
On Error GoTo 0 ' return to normal error checking            

       Next iRow

> A. Below is the code that work perfectly when i run it through macro but not
> working through command button, i have refer to other post but cant change it
[quoted text clipped - 107 lines]
>     Selection.End(xlUp).Select
> End Sub
Salman - 07 Mar 2007 10:24 GMT
noted and corrected thanks

but for now the main issue is that button is in "Summary" Sheet and that
code u have corrected have to run in another sheet "Scrip Position" after
clicking command button nothing happens else than re-formatting on the same
sheet i.e. "Summary".
"Scrip Position" is linked with several other sheets and loosing data if try
to format and subtotal there, dont know why, thats y extracting data to
another sheet after running first part of the code
But first part of the code not working through Command button , although
whole code works great  (on both sheets) if run through macro.

> I don't know what the data on the worksheet should look like. but was able to
> step through code with only one error.  The On Error statement is in the
[quoted text clipped - 122 lines]
> >     Selection.End(xlUp).Select
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.