ExcelVBA±àʵս±à³Ì(ºÜʵÓÃŶ)

(1)Excel 2003¸ß¼¶VBA±à³Ì±¦µä

(2)Excel 2003ÓëVBA±à³Ì´ÓÈëÃŵ½¾«Í¨(ÖÐÎÄ°æ) (3)ÇÉѧÇÉÓÃExcel 2003 VBAÓëºê(ÖÐÎÄ°æ) (4)ExcelVBAÓ¦ÓóÌÐòרҵÉè¼ÆʵÓÃÖ¸ÄÏ (5)ExcelVBAÓ¦Óÿª·¢ÓëʵÀý¾«½² (6)һЩÍøÉÏ×ÊÔ´

¸ü¶àµÄÐÅÏ¢

¹ØÓÚExcelVBAµÄ¸ü¶à²Î¿¼ºÍѧϰ×ÊÔ´£¬¿ÉÒÔÔÚwww.excelhome.netÉϲéÕÒ£¬ÓÐÒÉÎÊÒ²¿ÉÒÔÔÚExcelHomeÂÛ̳ÖÐÌáÎÊ¡£ÄúÒ²¿ÉÒԵǼÎҵIJ©¿Íhttp://fanjy.blog.excelhome.net£¬ÉÏÃæÓкܶàExcelµÄѧϰ×ÊÁÏ¡£Í¬Ê±£¬»¶Ó­ÓëÎÒÁªÏµ½»Á÷£¬ÎÒµÄe-mailÊÇ£ºxhdsxfjy@163.com¡£

¡°Ñ§Ï°Excel£¬Ê¹ÓÃVBA¶ÔExcel½øÐпØÖƲÙ×÷ÊÇÎÒºÜÈÈÖÔµÄÒµÓà°®ºÃÖ®Ò»¡£¡±¡ª¡ªfanjy

µÚÒ»Õ ExcelÓ¦ÓóÌÐò¶ÔÏó(Application¶ÔÏó)¼°Æä³£Ó÷½·¨

»ù±¾²Ù×÷Ó¦ÓÃʾÀý ·ÖÀà:ExcelVBA>>ExcelVBA±à³ÌÈëÃÅ·¶Àý Application¶ÔÏó´ú±íÕû¸öMicrosoft ExcelÓ¦ÓóÌÐò£¬´øÓÐ175¸öÊôÐÔºÍ52¸ö·½·¨£¬¿ÉÒÔÉèÖÃÕû¸öÓ¦ÓóÌÐòµÄ»·¾³»òÅäÖÃÓ¦ÓóÌÐò¡£

ʾÀý01-01£ºÌåÑ鿪/¹ØÆÁÄ»¸üÐÂ(ScreenUpdatingÊôÐÔ) Sub ¹Ø±ÕÆÁÄ»¸üÐÂ()

MsgBox \˳ÐòÇл»¹¤×÷±íSheet1¡úSheet2¡úSheet3¡úSheet2£¬ÏÈ¿ªÆôÆÁÄ»¸üУ¬È»ºó¹Ø±ÕÆÁÄ»¸üÐÂ\ Worksheets(1).Select

MsgBox \Ä¿Ç°ÆÁÄ»ÖÐÏÔʾ¹¤×÷±íSheet1\ Application.ScreenUpdating = True Worksheets(2).Select

MsgBox \ÏÔʾSheet2ÁËÂð£¿\ Worksheets(3).Select

MsgBox \ÏÔʾSheet3ÁËÂð£¿\ Worksheets(2).Select

MsgBox \ÏÂÃæÓëÇ°ÃæÖ´ÐеijÌÐò´úÂëÏàͬ,µ«¹Ø±ÕÆÁÄ»¸üй¦ÄÜ\ Worksheets(1).Select

MsgBox \Ä¿Ç°ÆÁÄ»ÖÐÏÔʾ¹¤×÷±íSheet1\& Chr(10) & \¹ØÆÁÆÁÄ»¸üй¦ÄÜ\ Application.ScreenUpdating = False

9

Worksheets(2).Select

MsgBox \ÏÔʾSheet2ÁËÂð£¿\ Worksheets(3).Select

MsgBox \ÏÔʾSheet3ÁËÂð£¿\ Worksheets(2).Select

Application.ScreenUpdating = True End Sub

ʾÀý˵Ã÷£ºScreenUpdatingÊôÐÔÓÃÀ´¿ØÖÆÆÁÄ»¸üС£µ±ÔËÐÐÒ»¸öºê³ÌÐò´¦ÀíÉæ¼°µ½¶à¸ö¹¤×÷±í»òµ¥Ôª¸ñÖеĴóÁ¿Êý¾Ýʱ£¬ÈôûÓйرÕÆÁÄ»¸üУ¬Ôò»áÕ¼ÓÃCPUµÄ´¦Àíʱ¼ä£¬´Ó¶ø½µµÍ³ÌÐòµÄÔËÐÐËٶȣ¬¶ø¹Ø±Õ¸ÃÊôÐÔÔò¿ÉÏÔÖøÌá¸ß³ÌÐòÔËÐÐËٶȡ£

ʾÀý01-02£ºÊ¹ÓÃ״̬À¸(StatusBarÊôÐÔ) Sub testStatusBar()

Application.DisplayStatusBar = True '¿ªÆô״̬À¸ÏÔʾ '¸³Öµ×´Ì¬À¸ÏÔʾµÄÎı¾

Application.StatusBar = \End Sub

ʾÀý˵Ã÷£ºStatusBarÊôÐÔÓÃÀ´Ö¸¶¨ÏÔʾÔÚ״̬À¸ÉϵÄÐÅÏ¢¡£Èô²»ÏëÔÙÏÔʾ״̬À¸Îı¾£¬¿ÉʹÓÃApplication.StatusBar = FalseÓï¾ä¹Ø±Õ״̬À¸ÏÔʾ£¬Ò²¿ÉÒÔÔÚ³ÌÐò¿ªÊ¼½«Ô­ÏȵÄ״̬À¸ÉèÖô洢£¬ÈçʹÓÃÓï¾äoldStatusBar = Application.DisplayStatusBar½«×´Ì¬À¸Ô­À´µÄÐÅÏ¢´æ´¢ÔÚ±äÁ¿oldStatusBar£¬ÔÚ³ÌÐòÔËÐÐÍê³É»òÍ˳öʱ£¬½«±äÁ¿ÖØи³Öµ¸ø״̬À¸£¬ÈçʹÓÃÓï¾äApplication.DisplayStatusBar = oldStatusBar£¬ÒÔ»Ö¸´×´Ì¬À¸Ô­×´¡£

ʾÀý01-03£º´¦Àí¹â±ê(CursorÊôÐÔ) Sub ViewCursors()

Application.Cursor = xlNorthwestArrow

MsgBox \Äú½«Ê¹ÓüýÍ·¹â±ê,Çл»µ½Excel½çÃæ²é¿´¹â±êÐÎ×´\ Application.Cursor = xlIBeam

MsgBox \Äú½«Ê¹Óù¤Ðιâ±ê,Çл»µ½Excel½çÃæ²é¿´¹â±êÐÎ×´\ Application.Cursor = xlWait

MsgBox \Äú½«Ê¹ÓõȴýÐιâ±ê,Çл»µ½Excel½çÃæ²é¿´¹â±êÐÎ×´\ Application.Cursor = xlDefault

MsgBox \ÄúÒѽ«¹â±ê»Ö¸´ÎªÈ±Ê¡×´Ì¬\End Sub

ʾÀý01-04£º»ñȡϵͳÐÅÏ¢ Sub GetSystemInfo()

MsgBox \°æ±¾ÐÅϢΪ:\& Application.CalculationVersion

MsgBox \µ±Ç°ÔÊÐíʹÓõÄÄÚ´æΪ:\& Application.MemoryFree MsgBox \µ±Ç°ÒÑʹÓõÄÄÚ´æΪ:\& Application.MemoryUsed MsgBox \¿ÉÒÔʹÓõÄÄÚ´æΪ:\& Application.MemoryTotal

MsgBox \±¾»ú²Ù×÷ϵͳµÄÃû³ÆºÍ°æ±¾Îª:\& Application.OperatingSystem

10

MsgBox \±¾²úÆ·ËùµÇ¼ÇµÄ×éÖ¯ÃûΪ:\& Application.OrganizationName MsgBox \µ±Ç°Óû§ÃûΪ:\& Application.UserName

MsgBox \µ±Ç°Ê¹ÓõÄExcel°æ±¾Îª:\& Application.Version End Sub

ʾÀý˵Ã÷£º¿ÉÒÔʹÓøøUserNameÊôÐÔ¸³ÖµÒÔÉèÖÃÓû§Ãû³Æ¡£

ʾÀý01-05£ºÍ˳ö¸´ÖÆ/¼ôÇÐģʽ(CutCopyModeÊôÐÔ) Sub exitCutCopyMode()

Application.CutCopyMode = False End Sub

ʾÀý˵Ã÷£ºÍ˳ö¸´ÖÆ/¼ôÇÐģʽºó£¬ÔÚ³ÌÐòÔËÐÐʱËù½øÐеĸ´ÖÆ»ò¼ôÇвÙ×÷²»»áÔÚÔ­µ¥Ôª¸ñÇøÓòÁôÏÂÁ÷¶¯µÄÐé¿òÏß¡£ÐèÒªÌáÐѵÄÊÇ£¬ÔÚ³ÌÐòÔËÐÐÍêºó£¬Ó¦Ê¹ÓÃApplication.CutCopyMode = FalseÓï¾ä»Ö¸´¸ÃÊôÐÔµÄĬÈÏÉèÖá£

ʾÀý01-06£º½ûÖ¹µ¯³ö¾¯¸æÐÅÏ¢(DisplayAlertsÊôÐÔ) Sub testAlertsDisplay()

Application.DisplayAlerts = False End Sub ʾÀý˵Ã÷£ºÔÚ³ÌÐòÔËÐйý³ÌÖУ¬ÓÐʱÓÉÓÚExcel±¾ÉíÉèÖõÄÔ­Òò£¬»áµ¯³ö¶Ô»°¿ò£¬´Ó¶øÖжϳÌÐòµÄÔËÐУ¬Äú¿ÉÒÔÔÚ³ÌÐò֮ǰ¼ÓÉÏApplication.DisplayAlerts = FalseÓï¾äÒÔ½ûÖ¹µ¯³öÕâЩ¶Ô»°¿ò¶ø²»Ó°Ïì³ÌÐòÕý³£ÔËÐС£ÐèҪעÒâµÄÊÇ£¬ÔÚ³ÌÐòÔËÐнáÊøÇ°£¬Ó¦Ê¹DisplayAlertsÊôÐÔ»Ö¸´ÎªÈ±Ê¡×´Ì¬£¬¼´Ê¹ÓÃÓï¾äApplication.DisplayAlerts = True¡£¸ÃÊôÐÔµÄĬÈÏÉèÖÃΪTrue£¬µ±½«¸ÃÊôÐÔÉèÖÃΪFalseʱ£¬Excel»áʹֱ½ÓʹÓöԻ°¿òÖÐĬÈϵÄÑ¡Ôñ£¬´Ó¶ø²»»áÒòΪµ¯³ö¶Ô»°¿ò¶øÓ°Ïì³ÌÐòÔËÐС£

ʾÀý01-07£º½«ExcelÈ«ÆÁÄ»ÏÔʾ Sub testFullScreen()

MsgBox \ÔËÐкó½«ExcelµÄÏÔʾģʽÉèÖÃΪȫÆÁÄ»\ Application.DisplayFullScreen = True MsgBox \»Ö¸´ÎªÔ­À´µÄ״̬\

Application.DisplayFullScreen = False End Sub

ʾÀý01-08£ºExcelÆô¶¯µÄÎļþ¼Ð·¾¶ Sub ExcelStartfolder()

MsgBox \Æô¶¯µÄÎļþ¼Ð·¾¶Îª£º\& Chr(10) & Application.StartupPath

End Sub

ʾÀý01-09£º´ò¿ª×î½üʹÓùýµÄÎĵµ Sub OpenRecentFiles()

MsgBox \ÏÔʾ×î½üʹÓùýµÄµÚÈý¸öÎļþÃû,²¢´ò¿ª¸ÃÎļþ\

MsgBox \×î½üʹÓõĵÚÈý¸öÎļþµÄÃû³ÆΪ:\& Application.RecentFiles(3).Name

11

Application.RecentFiles(3).Open End Sub

ʾÀý01-10£º´ò¿ªÎļþ(FindFile·½·¨) Sub FindFileOpen()

On Error Resume Next

MsgBox \Çë´ò¿ªÎļþ\vbOKOnly + vbInformation, \´ò¿ªÎļþ\ If Not Application.FindFile Then

MsgBox \ÎļþδÕÒµ½\vbOKOnly + vbInformation, \´ò¿ªÊ§°Ü\ End If End Sub

ʾÀý˵Ã÷£º±¾Ê¾Àý½«ÏÔʾ¡°´ò¿ª¡±Îļþ¶Ô»°¿ò£¬ÈôÓû§Î´´ò¿ªÎļþ(¼´µã»÷¡°È¡Ïû¡±°´Å¥)£¬Ôò»áÏÔʾ¡°´ò¿ªÊ§°Ü¡±ÐÅÏ¢¿ò¡£Ê¾ÀýÖÐʹÓÃÁËFindFile·½·¨£¬ÓÃÀ´ÏÔʾ¡°´ò¿ª¡±¶Ô»°¿ò²¢ÈÃÓû§´ò¿ªÒ»¸öÎļþ¡£Èç¹û³É¹¦´ò¿ªÒ»¸öÐÂÎļþ£¬Ôò¸ÃֵΪTrue¡£Èç¹ûÓû§È¡ÏûÁ˲Ù×÷²¢Í˳ö¸Ã¶Ô»°¿ò£¬Ôò¸ÃֵΪFalse¡£

ʾÀý01-11£ºÎļþ¶Ô»°¿ò²Ù×÷(FileDialogÊôÐÔ) Sub UseFileDialogOpen() Dim lngCount As Long '¿ªÆô\´ò¿ªÎļþ\¶Ô»°¿ò

With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = True .Show

'ÏÔʾËùÑ¡µÄÿ¸öÎļþµÄ·¾¶

For lngCount = 1 To .SelectedItems.Count MsgBox .SelectedItems(lngCount) Next lngCount End With End Sub ʾÀý˵Ã÷£º±¾Ê¾ÀýÏÔʾ¡°´ò¿ªÎļþ¡±¶Ô»°¿ò£¬µ±Óû§ÔÚÆäÖÐÑ¡ÔñÒ»¸ö»ò¶à¸öÎļþºó£¬½«ÒÀ´ÎÏÔʾÿ¸öÎļþµÄ·¾¶¡£ÆäÖУ¬FileDialogÊôÐÔ·µ»Ø´ò¿ªºÍ±£´æ¶Ô»°¿òÖÐһϵÁжÔÏóµÄ¼¯ºÏ£¬Äú¿ÉÒԶԸü¯ºÏ¶ÔÏóµÄÊôÐÔ½øÐнøÒ»²½µÄÉèÖã¬ÈçÉÏÀýÖеÄAllowMultiSelectÊôÐÔÉèÖÃΪTrue½«ÔÊÐíÓû§Ñ¡Ôñ¶à¸öÎļþ¡£

ʾÀý01-12£º±£´æExcelµÄ¹¤×÷»·¾³ Sub ±£´æExcelµÄ¹¤×÷»·¾³()

MsgBox \½«ExcelµÄ¹¤×÷»·¾³±£´æµ½D:\\ExcelSample\\ÖÐ\ Application.SaveWorkspace \End Sub

ʾÀý˵Ã÷£ºÔËÐб¾³ÌÐòºó£¬½«¹¤×÷²¾ÒÔ´øºó׺Ãû.xlw±£´æµ½DÅ̵ÄExcelSampleÎļþ¼ÐÖУ¬Éú³ÉµÄÎļþÈ«ÃûΪSample.xlw¡£µ±¸Ä±ä¹¤×÷²¾²¢±£´æʱ£¬Excel»áѯÎÊÊǸ²¸ÇÔ­Îļþ»¹ÊDZ£´æ¸±±¾¡£

ʾÀý01-13£º¸Ä±äExcel¹¤×÷²¾Ãû×Ö(CaptionÊôÐÔ)

12

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ì滻Ϊ@)