Microsoft Excel - Macro code + UserForm + Buttons

Asked By Blue J on 11-Jan-11 02:00 AM
Hi Jackpot,
can u help me with a macro code in Excel, I am creating a UserForm, it should have 2 buttons and one text box.
on button1 should act like a browse button, on click of button1 it opens a window from where we can select only an excel file and on click of button2 it should perform a function on the same excel sheet.
The code of this function is :

Sub valuee()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheet3
Set ws2 = Sheets.Add(After:=Sheets(Sheets.Count))
ws2.Cells(1, 1) = "No Of Test Cases"
ws2.Cells(1, 2) = "No Of Passed Test Cases"
ws2.Range("A2") = Application.Sum(ws1.Columns(1))
ws2.Range("B2") = Application.CountIf(ws1.Range("E:E"), "Passed")

End Sub



please help.

Thanks

Jackpot . replied to Blue J on 11-Jan-11 02:21 AM
Hi Ashfaque

The below code would allow you to select a file and open that file and check the number of passed items in Sheet3 of the workbook and write the results to a new sheet....


Dim strFile As String
Private Sub CommandButton1_Click()
  
With Application.FileDialog(msoFileDialogOpen)
  .AllowMultiSelect = False
  .Filters.Add "Excel Workbooks", "*.xl*", 1
  .InitialFileName = "C:\"
  .Show
strFile = .SelectedItems(1)
End With
  
End Sub
  
Private Sub CommandButton2_Click()
  
Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
  
Set wb = Workbooks.Open(strFile)
Set ws1 = wb.Sheets("Sheet3")
Set ws2 = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
  
ws2.Cells(1, 1) = "No Of Test Cases"
ws2.Cells(1, 2) = "No Of Passed Test Cases"
ws2.Range("A2") = Application.Sum(ws1.Columns(1))
ws2.Range("B2") = Application.CountIf(ws1.Range("E:E"), "Passed")
  
End Sub

Blue J replied to Jackpot . on 11-Jan-11 04:27 AM
Hi Jackpot,


when i am running it, its giving an error
the error is Run-time error '-2147352565(8002000b)':
Index refers beyond end of list.
on clicking "Debug" button it highlights the below statement
 Set ws1 = wb.Sheets("Sheet3")
what should i do?
Please help.

Thanks
Blue J replied to Jackpot . on 11-Jan-11 04:27 AM
Hi Jackpot,


when i am running it, its giving an error
the error is Run-time error '-2147352565(8002000b)':
Index refers beyond end of list.
on clicking "Debug" button it highlights the below statement
 Set ws1 = wb.Sheets("Sheet3")
what should i do?
Please help.

Thanks
Jackpot . replied to Blue J on 11-Jan-11 04:31 AM
Which sheet do you need to check the number of "passed". As per the code it is currently "Sheet3". Change it to suit.


Set ws1 = wb.Sheets("Sheet3")
Blue J replied to Jackpot . on 11-Jan-11 04:43 AM
Hi,

on clicking button1 i can select a file, say file1.xls
on clickin button2, in file1, a new sheet will be created, say sheetA, sheetA wil have the details of No Of Test Cases & No Of Passed Test Cases. The No Of Passed Test Cases will be the count of the number of times Passed is written in sheet3 of file1.xls.

The output SheetA, will be a part of file1.xls

Pls help.

Thanks
Blue J replied to Jackpot . on 11-Jan-11 04:59 AM
Hi Jackpot,

Thank you so much.

Everything in the code is working except for the  summation part that is:
Set ws1 = wb.Sheet("Sheet3")
ws2.Range("A2") = Application.Sum(ws1.Columns(1))
ws2.Range("B2") = Application.CountIf(ws1.Range("E:E"), "Passed")
the above three lines are giving a problem and i am not able to figure it out.

Please help.

Thanks
Jackpot . replied to Blue J on 11-Jan-11 05:38 AM
I have retried the above code and this is what it exactly does...with an output such as below ....Only thing is that the new sheet is not renamed...



No Of Test Cases No Of Passed Test Cases
3 1
Blue J replied to Jackpot . on 11-Jan-11 05:47 AM
Hi,

Can you please mail me that excel file in which u have made the User Form.
My email id is ashfaque.memon@patni.com

i am not able to get the right solution.

Please help .
Thanks
Jackpot . replied to Blue J on 11-Jan-11 06:30 AM
Try this...2workbooks 1 with userform and another the file to be selected from commandbutton browse

test1.zip
Blue J replied to Jackpot . on 11-Jan-11 06:39 AM
Hi jackpot,

Thank you so much for your help.
I really appreciate it.

Thanks a ton.

Thanks :)