PDA

View Full Version : CheckBox and ListBox Problem



Ann_BBO
08-06-2007, 09:01 AM
In my worksheet, It have the 50 standard form for the data of each Input file. Click the ?Input File? button to input the standard files in the listbox, say 5 files. Then, click the ?Data Analysis? button to analysis the data from the 5 files and extra the data into 5 standard forms. It is the first time to analysis data. Now, I want to do the below action. I had added the CheckBox in the UserForm. After 1st analysis, I input the new 2 files in the listbox (maybe I had deleted the previous filepath before).
(1) If I click the checkbox, it will add the new sheet and copy the standard format from activesheet into Sheet. Then, the data of new 2 files will be extracted in the 1st & 2nd standard form in the new sheet.
(2) If I do not click the checkbox, the new 2 file data will be extracted in the 6th & 7th form.
The VBA as shown below:
Private Sub cmdShowdata_Click()
Dim Tgt As Worksheet ' The name of the active.sheet
Dim Source As Range ' The name of the Filepaths, sheets, column in the source.workbook
Dim wbSource As Workbook ' The name of the source.workbooks
Dim cel As Range ' The individual cell of the active.sheet
Dim Rng As Range ' The range of the source.workbooks
Dim c As Range ' The target range of the source.workbooks
Dim x As Long ' The variable of the listBox1 sequence
Dim y As Integer ' The variable of the range distance between the Data range

' Open the File(s) according the ListBox1 sequence
For x = 0 To ListBox1.ListCount - 1
Application.ScreenUpdating = False
Set Tgt = ActiveSheet
Set wbSource = Workbooks.Open(Filename:=ListBox1.List(x))
Set Source = wbSource.Sheets(1).Columns(1)
y = x * 38

With Tgt
.Activate
'Loop through names in column A with STAFF 001 and extra the data in source.workbooks
For Each cel In Range(Cells((y + 33), 1), Cells((y + 37), 1))
If Not cel = "" Then
Set c = Source.Range("A3")
Set Rng = Nothing
Do While c.Row < Source.Range("A" & Source.Rows.Count).End(xlUp).Row
If c = cel Then
If Rng Is Nothing Then Set Rng = c.Offset(1)
Set Rng = Union(Rng, Range(c.Offset(1), c.Offset(1).End(xlDown)))
Set c = c.Offset(1).End(xlDown).Offset(1)
Else
Set c = c.Offset(1)
End If
Loop
' Extra the data to the active.sheet in the suitable range
cel.Offset(, 1) = Application.Average(Rng.Offset(, 7).End(xlDown)) / 1000
End If
Next
End With
wbSource.Close False
Application.ScreenUpdating = True
Next x
End Sub

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range("A1:N1911").Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste
Application.ScreenUpdating = False
Set Tgt = ActiveSheet

For x = 0 To 50
y = x * 38
With Tgt
.Activate
'clear old data
Range(.Cells((y + 33), 2), .Cells((y + 37), 3)).ClearContents
Range(.Cells((y + 33), 5), .Cells((y + 37), 5)).ClearContents

Range(.Cells((y + 33), 10), .Cells((y + 39), 10)).ClearContents
Range(.Cells((y + 33), 11), .Cells((y + 33), 11)).ClearContents
Cells((y + 49), 3).ClearContents
Cells((y + 45), 6).ClearContents
End With
Next
Application.ScreenUpdating = True

End If
End Sub

I had do that the copy of the blank form activesheet into the new sheet. But I don't know how to write the above action1,2.
Thanks

Norie
08-06-2007, 10:44 AM
You have 50 userforms? Why?:eek:

Ann_BBO
08-06-2007, 05:34 PM
Not 50 Userforms, maybe say 50 blank sheet as well

Ann_BBO
08-06-2007, 06:44 PM
From my vba program, I want to know which parameters can let me know the cmdShowdata had proccessed once (Had run first time).

Thanks