2012-12-20 39 views
1

我正在使用Excel VBA,它需要一個給定的時間範圍,拉取數據,刪除特定參數之外的額外數據,然後通過機器「Riveter 01 - Riveter 22」對數據進行排序。然後用排序數據創建圖表。當我有用戶點擊提交按鈕時,應該刪除所有數據。當表單關閉時,每次重新打開數據也會被清除。 (我已經完成了這個冗餘來嘗試清除剩餘的數據)。但是因爲某些原因,當我打開表單時,在錯誤的列中有剩餘的數據,我的圖表上有一個超過41,000的數據。Excel中的數據沒有正確刪除

我粘貼我的代碼,希望有人可以給出答案。我是VBA的新手,所以我確信我沒有做任何事情,所以請隨時告訴我,如果我輸入的內容是愚蠢的或不必要的。

Private Sub Submit_Button_Click() 

Dim cn As Object 
Dim rs As Object 
Dim strFile As String 
Dim strCon As String 
Dim strSQL As String 
Dim s As String 
Dim i As Integer, j As Integer 

''Access database 

strFile = "S:\IT\Databases\Main_BE.mdb" 

''This is the Jet 4 connection string, you can get more 
''here : http://www.connectionstrings.com/excel 

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";" 

''Late binding, so no reference is needed 

Set cn = CreateObject("ADODB.Connection") 
Set rs = CreateObject("ADODB.Recordset") 

cn.Open strCon 

'Rough idea 
StartDate = Sheet1.[C5] 
EndDate = Sheet1.[C10] 

ModStartDate = StartDate - 1 
ModEndDate = EndDate - 1 

strSQL = "SELECT * FROM Work_Orders " _ 
     & "WHERE Repair_Start_Date >= #" & ModStartDate & "# " _ 
     & "AND Repair_Start_Date <= #" & EndDate & "# " _ 
     & "ORDER BY Repair_Start_Date, Repair_Start_Time" 

'strSQL = "SELECT * FROM Work_Orders " _ 
'  & "WHERE Repair_Start_Date Between(" & ModStartDate & "+TimeSerial(17,30,0) And (" & EndDate & "+TimeSerial(17,29,0))" 



rs.Open strSQL, cn 


'Deletes all contents to J500 each time 

Sheet3.Range("A4:K5000").Delete True 

''Pick a suitable empty worksheet for the results 

Worksheets("Raw Data").Cells(4, 1).CopyFromRecordset rs 

Worksheets("Raw Data").Range("H4:H5000").NumberFormat = "hh:mm AM/PM" 

Sheet3.[L3] = "=Counta(H4:H500)" 

Dim Counter As Integer 

Counter = Sheet3.[L3] + 3 

Dim CompareTime As String 

CompareTime = Sheet3.Cells(4, 8) 

'Do While ((Sheet3.[G4] = ModStartDate) And (TimeNo("9:30 PM") > TimeNo(CompareTime))) 


    'Worksheets("Raw Data").Range("A4:L4").Select 
    'Sheet3.[A4].EntireRow.Delete Shift:=xlUp 
    'Worksheets("Raw Data").Cells(1, 1).Select 

'Loop 


Dim StringTime As String 

StringTime = Sheet3.Cells(Counter, 8) 

'If ((TimeNo(StringTime) > TimeNo("9:30PM")) And (Sheet3.Cells(Counter, 7) = EndDate)) Then 

' Sheet3.[L4] = "True" 
'Else 

' Sheet3.[L4] = "False" 

'End If 


Do While ((TimeNo(StringTime) > TimeNo("9:29 PM")) And (Sheet3.Cells(Counter, 7) = EndDate)) 

    Sheet3.Cells(Counter, 7).EntireRow.Delete 
    Counter = Counter - 1 

Loop 

With Sheet3 
    Sheet2.Range("A9:K5000").Delete True 
    Sheet2.Range("A9:K5000").Delete True 
    Sheet2.Range("A9:K5000").Delete True 
    .AutoFilterMode = False 
    With .Range("F2:J500") 
     .AutoFilter Field:=1, Criteria1:="Riveter 01" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 02" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("F10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 03" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("K10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 04" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("P10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 05" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("U10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 06" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("Z10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 07" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("AE10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 08" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("AJ10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 09" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("AO10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 10" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("AT10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 11" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("AY10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 12" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("BD10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 13" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("BI10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 14" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("BN10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 15" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("BS10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 16" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("BX10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 17" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("CC10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 18" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("CH10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 19" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("CM10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 20" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("CR10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 21" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("CW10") 
     .AutoFilter Field:=1, Criteria1:="Riveter 22" 
     .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("DB10") 

    End With 
    .AutoFilterMode = False 
End With 

With Sheet2 
    .[B4] = "=SUM(D10:D500)" 
    .[G4] = "=SUM(I10:I500)" 
    .[L4] = "=SUM(M10:M500)" 
    .[Q4] = "=SUM(S10:S500)" 
    .[V4] = "=SUM(X10:X500)" 
    .[AA4] = "=SUM(AC10:AC500)" 
    .[AF4] = "=SUM(AH10:AH500)" 
    .[AK4] = "=SUM(AM10:AM500)" 
    .[AP4] = "=SUM(AR10:AR500)" 
    .[AU4] = "=SUM(AW10:AW500)" 
    .[AZ4] = "=SUM(BB10:BB500)" 
    .[BE4] = "=SUM(BG10:BG500)" 
    .[BJ4] = "=SUM(BL10:BL500)" 
    .[BO4] = "=SUM(BQ10:BQ500)" 
    .[BT4] = "=SUM(BV10:BV500)" 
    .[BY4] = "=SUM(CA10:CA500)" 
    .[CD4] = "=SUM(CF10:CF500)" 
    .[CI4] = "=SUM(CK10:CK500)" 
    .[CN4] = "=SUM(CP10:CP500)" 
    .[CS4] = "=SUM(CU10:CU500)" 
    .[CX4] = "=SUM(CZ10:CZ500)" 
    .[DC4] = "=SUM(DE10:DE500)" 



End With 



''Tidy up 
rs.Close 
Set rs = Nothing 
cn.Close 
Set cn = Nothing 


End Sub 


Public Function TimeNo(Time As String) As Long 

'************************************** 
' Name: A Compare Time Function (like you can compare dates in VB) 
' Description:This will allow you to compare times. I noticed that there is a 'Date' type in VB, but no 'Time' type. So if you want to compare Dates you are fine, but for Time comparisons you are a bit stuffed. This is very simple, and will allow you to convert times into numbers so that you can make easy comparisons with them. 
' By: Proxy Avoidance 
' 
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=63363&lngWId=1'for details.'************************************** 

' This is the sort of code that makes you think 'Why didnt I think of that?!? 
' 
' EG: 
' IF TimeNo("21:55:32") < TimeNo("20:40:12") Then 
' msgbox "WHOOO!" 
' end if 
' 
' The code is also cross-compatible with different time formats... 
' 
' IF TimeNo("21:55:32") < TimeNo("8:40PM") Then 
' msgbox "WHOOO!" 
' end if 

TimeNo = CLng(Replace(Format(Time, "hhnnss"), ":", "")) 
End Function 
+1

我可以向你保證,沒有多少人想要通過這段代碼,並試圖破譯錯誤點。你是否瀏覽過VBE中的代碼?如果您在同時觀看電子表格的過程中執行此操作,則會看到哪一行將數據的行爲拋出數據。 –

+0

我想到了。我只是充滿希望。但我已經嘗試回溯它並且無法弄清楚「爲什麼」。我應該在每一步之後放置一個MsgBox,並在通過該過程時觀看我的電子表格?還是有更簡單的方法?謝謝 – Grant

+2

要做的方法是在第一行代碼中放置一個斷點,然後點擊按鈕。一旦VBE打開,使用'F8'逐行逐行瀏覽代碼,同時觀察它如何影響工作表。你會很快發現哪條線會把你扔掉,因爲你在某些時候不會得到你期望的結果。 –

回答

0

我最終使用:

Sheet3.Range("A10:L1000").Delete True 
Sheet2.Range("A9:DF1000").Clear 
Sheet4.Range("A9:EK1000").Clear 

我只是做了刪除第一個,看看是否有任何區別,但沒有,我可以看到。