Filed under: Excel VBA 範例, Top, 一般程序

進度條範例(UserForm Lable 顏色漸進)

by chijanzen on 七月 20th, 2008 | View: 1,870 views

Tags Share Comments (0)
索   引 H0007
主   題 進度條範例(UserForm Lable 顏色漸進)
版   本 >= 8.0(Office 97)
說   明 本範例使用兩個Label控件來製作顏色漸進的進度條範例
參   考   



複製以下程式碼到Module

Code


Public j As Double

 


Sub UpdateProgress(Pct)

    With UserForm1

        .LabPct1.Caption = Pct * 100 & " %": DoEvents  '顯示百分比

        .LabProg21.Width = Pct * (.LabProgressBar2.Width - j)

        intPctColor = CInt((Pct * 255) * 0.75)  '紅色色系

        lngColor = RGB(64 + intPctColor, 0, 0)

        .LabProg21.BackColor = lngColor: DoEvents  '顯示進度條背景色

        DoEvents

        .Repaint

    End With

End Sub

 


複製以下程式碼到UserForm

Code


Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim strText1 As String

Dim intI, i As Integer

 


Private Sub UserForm_activate()

    Me.Label1 = "程式執行中"

    Me.LabProg21.Width = 0        '清除Type 2進度條方塊

    Me.LabPct1.Visible = True

    j = (Me.LabProg21.Left - Me.LabProgressBar2.Left) * 2

    With Application

        .EnableEvents = False

        .ScreenUpdating = False  '關閉復螢幕更新

    End With

    strText1 = Sheets("LL").ComboBox1.Text

    For i = 5 To 26

        rng(i).Copy Destination:=Sheets("LL").Cells(i, 2)

        K = K + 1

        DcPct = K / ((26 - 5) + 1) '進度百分比

        Call UpdateProgress(DcPct)   '更新進度條百分比

    Next i

    Worksheets("SJ").Cells.AutoFilter   '關閉篩選

    With Application

        .EnableEvents = True

        .ScreenUpdating = True   '開啟螢幕更新

    End With

    Sleep 1000   '1秒

    Unload Me

End Sub

 


Function rng(i) As Range

    With Worksheets("SJ")

        ctxt = Worksheets("LL").Cells(i, 1)

        '篩選公司

        .Cells.AutoFilter Field:=2, Criteria1:=ctxt

        strText1 = Sheets("LL").ComboBox1.Text

        '篩選月份

        .Cells.AutoFilter Field:=1, Criteria1:=strText1

        '指定範圍

        Set rng = .Range("C2:W" & .[A65536].End(xlUp).Row). _

            SpecialCells(xlCellTypeVisible)

    End With

End Function

 

Help

Code


 本範例是一個典型的進度條範例的運用,本範例中在Sheets("LL")儲存格C1選擇要查詢的年月後程式碼就會自動更
   新資料;在進度條的規劃設計最重要的就是百分比的規劃,在本範例中要處裡的是從第5列-26列合計共有22家公司,
   所以在規劃進度條的時候就知道程式會處裡22次,因此每次要處裡的百分比是

   DcPct = (K / ((26 - 5) + 1)) * 100 

    將每次要更新的百分比帶入UpdateProgress程序中,就簡單的完成了進度條的範例了
 
 
 SpecialCells(xlCellTypeVisible):傳回一個 Range 物件,此物件代表與指定型態及值相符合的所有儲存格

     xlCellTypeVisible:所有可見儲存格

 
     當使用Excel篩選功能後,如果篩選的資料有好幾列,而使用者只想複製篩選後的資料到其他地方做資料處裡,這時候
     就必須使用SpecialCells(xlCellTypeVisible)方法來複製篩選後的資料    
 
 Sleep API 函數: 用來指定需要延遲的時間,它的單位是毫秒
    此函數類似 VBA的 Wait 方法 Application.Wait (Now + TimeValue("0:00:01"))

 本範例進度條所使用的色係為紅色色系lngColor = RGB(64 + intPctColor, 0, 0),也可以改成藍色色系或其他色系 
   

   
 例如改成藍色色系 lngColor = RGB(0, 0, 64 + intPctColor)

   
 例如改成綠色色系 lgColor = RGB(0, 64 + intPctColor, 0)

 

 

DownLoad

Code



檔案下載

Popularity: 18%

About the Author
    網路化名: chijanzen、中國龍、邪兵衛 經歷: 第二屆微軟社群之星 第三屆微軟「最有價值專家」 第五屆微軟「最有價值專家」
No comments currently exist for this post.

Why don't you make one?

Get a GravatarLeave a Reply

Name: « Required

Email Address: « Required

Website URL: « Optional

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me
chijanzen 分享個人Excel VBA 學習經驗,架站心得, 日常生活記事等...

Add to Google

分類
Translator
Chinese (Simplified) flagItalian flagKorean flagEnglish flagGerman flag
French flagJapanese flagRussian flagBulgarian flagFinnish flag
相簿