Sady Home

Note my coding life

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
在EXCEL中添加一个按钮(Command Button),当点击时触发事件
Private Sub cmdButton_Click()
    
Dim cnnDB As ADODB.Connection
    
Dim cmdDB As ADODB.Command
    
Dim rsFC As ADODB.Recordset
    
Dim strSQL As String
    
Dim I As Integer
    
Dim J As Integer

    
Set cnnDB = New ADODB.Connection
    
Set cmdDB = New ADODB.Command

    cnnDB.Open 
"VPT_DB""VPT_DBUser""pw"
    cmdDB.ActiveConnection 
= cnnDB
    
    Application.DisplayStatusBar 
= True
    Application.ScreenUpdating 
= False
    shtLRP.Select
    
'We can use a variable to replace A5032     
    For I = 2 To 5032 Step 10
        strSQL 
= "SELECT * FROM ForeCast_12M WHERE ProjectCode='" & shtLRP.Cells(I, 2& "' AND CustomerCode='" & shtLRP.Cells(I, 1& "'"
        cmdDB.CommandText 
= strSQL
        
Set rsFC = cmdDB.Execute
        
        
If rsFC.BOF And rsFC.EOF Then
            Application.StatusBar 
= "Not find out " & shtLRP.Cells(I, 2)
        
Else
            Application.StatusBar 
= "Find out" & shtLRP.Cells(I, 2)
            
For J = 1 To 12
                
If J < 10 Then
                    shtLRP.Cells(I, J 
+ 7= rsFC("FC_MCOS_M0" & J)
                    shtLRP.Cells(I 
+ 1, J + 7= rsFC("FC_ASP_BL_M0" & J)
                    shtLRP.Cells(I 
+ 2, J + 7= rsFC("FC_BL_M0" & J)
                
Else
                    shtLRP.Cells(I, J 
+ 7= rsFC("FC_MCOS_M" & J)
                    shtLRP.Cells(I 
+ 1, J + 7= rsFC("FC_ASP_BL_M" & J)
                    shtLRP.Cells(I 
+ 2, J + 7= rsFC("FC_BL_M" & J)
                
End If
            
Next
        
End If
        rsFC.Close
    
Next
    cnnDB.Close
    
    Application.StatusBar 
= "Completed"
    Application.ScreenUpdating 
= True
End Sub
这段代码当然还有些问题,但为了赶时间,先这么做了,以后再看有什么更好的方法。
posted on 2007-10-24 14:14  Sady  阅读(276)  评论(0)    收藏  举报
凭飞堂