隨筆-94  評論-56  文章-3  trackbacks-0
          http://kmyrw.bokee.com/2027437.html
          Excel中可以使用VBA進行編程,下面是總結的幾點,幾個代碼示例是從Microsoft Excel Visual Basic參考中Copy過來的,以供參考。

          1.Excel中“視圖”,工具欄,控件工具箱。可以從控件工具中拖入各種VBA控件。

          2.打開“工具“,宏,Visual Basic編輯器,可以對控件進行編程。(表格中的按鈕雙擊后即可進入VBA代碼編輯界面).

          3.表格的Range屬性和Cells屬性使用示例:

          本示例將 Sheet1 上 A1 單元格的值設置為 3.14159。
          Worksheets("Sheet1").Range("A1").Value = 3.14159

          本示例在 Sheet1 的 A1 單元格中創建一個公式。
          Worksheets("Sheet1").Range("A1").Formula = "=10*RAND()"

          本示例在 Sheet1 的單元格區域 A1:D10 上進行循環。如果某個單元格的值小于 0.001,則此代碼將用 0(零)來取代該值。
          For Each c in Worksheets("Sheet1").Range("A1:D10")
          ??? If c.Value < .001 Then
          ??????? c.Value = 0
          ??? End If
          Next c

          本示例在名為“TestRange”的區域上進行循環,并顯示該區域中空白單元格的個數。
          numBlanks = 0
          For Each c In Range("TestRange")
          ??? If c.Value = "" Then
          ??????? numBlanks = numBlanks + 1
          ??? End If
          Next c
          MsgBox "There are " & numBlanks & " empty cells in this range"

          本示例將 Sheet1 中單元格區域 A1:C5 上的字體樣式設置為斜體。本示例使用 Range 屬性的語法2。
          Worksheets("Sheet1").
          Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

          本示例將 Sheet1 中單元格 C5 的字體大小設置為 14 磅。
          Worksheets("Sheet1").Cells(5, 3).Font.Size = 14

          本示例清除 Sheet1 上第一個單元格的公式。
          Worksheets("Sheet1").Cells(1).ClearContents

          本示例將 Sheet1 上所有單元格的字體設置為 8 磅的“Arial”字體。
          With Worksheets("Sheet1").Cells.Font
          ??? .Name = "Arial"
          ??? .Size = 8
          End With

          本示例在 Sheet1 上的單元格區域 A1:J4 中循環,將其中小于 0.001 的值替換為 0(零)。
          For rwIndex = 1 to 4
          ??? For colIndex = 1 to 10
          ??????? With Worksheets("Sheet1").Cells(rwIndex, colIndex)
          ??????????? If .Value < .001 Then .Value = 0
          ??????? End With
          ??? Next colIndex
          Next rwIndex

          本示例將設置一行的背景色
          Excel.ActiveSheet.Rows(1).Interior.Color = vbRed


          本示例把Sheet1中6行5列的區域中單元格值全為0的那些行標紅。

          1.Excel中“視圖”,工具欄,控件工具箱。可以從控件工具中拖入各種VBA控件。

          2.打開“工具“,宏,Visual Basic編輯器,可以對控件進行編程。(表格中的按鈕雙擊后即可進入VBA代碼編輯界面).

          3.輸入代碼
          Private Sub CommandButton1_Click()
          ? ? Dim allIsZero As Boolean
          ? ? For rwIndex = 1 To 6
          ? ? allIsZero = True
          ? ? For colIndex = 1 To 5
          ? ?? ???With Worksheets("Sheet1").Cells(rwIndex, colIndex)
          ? ?? ?? ?? ?If .Value > 0 Then allIsZero = False
          ? ?? ???End With
          ? ? Next colIndex
          ? ? If allIsZero = True Then Worksheets("Sheet1").Rows(rwIndex).Interior.Color = RGB(255, 0, 0)
          ? ? Next rwIndex
          End Sub




          								
          posted on 2006-10-29 12:26 小言身寸 閱讀(1207) 評論(0)  編輯  收藏 所屬分類: 軟件應用
          主站蜘蛛池模板: 大兴区| 闽侯县| 泸西县| 唐海县| 台湾省| 南康市| 安岳县| 平定县| 铜山县| 凌云县| 上饶市| 北票市| 通道| 仁寿县| 惠来县| 凌海市| 东乌| 巴塘县| 普兰店市| 鸡西市| 礼泉县| 桦川县| 项城市| 大城县| 长乐市| 安龙县| 乡宁县| 泌阳县| 随州市| 靖远县| 从化市| 马公市| 屯门区| 利辛县| 望谟县| 青神县| 进贤县| 梁山县| 新密市| 灵璧县| 太谷县|