|
楼主 |
发表于 2010-12-21 11:03
|
显示全部楼层
1楼附件里的移植VBA完整代码,不包括VSTO模板代码。- Imports Microsoft.Office.Tools.Ribbon
- Imports Excel = Microsoft.Office.Interop.Excel
- Public Class Ribbon1
- Public ProtectOffOn&
- Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
- Call ListSheets()
- End Sub
- Sub 单元格写入值()
- Dim i&
- With Globals.ThisWorkbook.Worksheets("工作表一")
- .select()
- .RANGE("A1:B1") = {"名称", "数量"}
- For i = 2 To 10
- .range("A" & i).VALUE2 = "数据-" & i
- .range("B" & i).VALUE2 = i * (100 - i * 10)
- Next i
- End With
- End Sub
- Sub ListSheets() '遍历工作表并生成目录
- Dim i&, j&, k&
- MsgBox("请确认工作表未保护!" & Chr(10) & "这是一个综合示例,包括:" & Chr(10) & "1、遍历工作表;" & Chr(10) & "2、获取特定名称工作表的位置:" & Chr(10) & "3、使用数组;" & Chr(10) & "4、把数组中的值写入工作表;" & Chr(10) & "5、选择工作表。")
- With Globals.ThisWorkbook
- .Worksheets("工作表目录").select()
- j = .Worksheets("汇总表").index '确定开始提取工作表名称的位置,还是确定数组大小的参数
- k = .Worksheets.Count - j '确定单元格区域大小
- Dim Rng As Excel.Range = .Worksheets("工作表目录").Range("B3").Resize(k, 1)
- Dim Arr(0 To k - 1, 0 To 0) As String '数组从0开始
- For i = j + 1 To .Worksheets.Count
- Arr(i - j - 1, 0) = .Worksheets(i).Name
- Next i
- Rng.Value2 = Arr
- End With
- End Sub
- Sub Button2_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button2.Click
- With Globals.ThisWorkbook
- .Worksheets("工作表目录").select()
- If ProtectOffOn = 0 Then
- .Worksheets("工作表目录").protect(password:="123456")
- ProtectOffOn = 1
- MsgBox("工作表已保护!再次点击此按钮会解除保护。")
- ElseIf ProtectOffOn = 1 Then
- .Worksheets("工作表目录").unprotect(password:="123456")
- ProtectOffOn = 0
- MsgBox("已撤消工作表保护!再次点击此按钮会重新保护。")
- End If
- End With
- End Sub
- Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button4.Click
- MsgBox("此示例更改最后一个工作表的名称为《更名工作表》。")
- With Globals.ThisWorkbook
- .Worksheets(.Worksheets.Count).name = "更名工作表"
- End With
- End Sub
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button3.Click
- MsgBox("此示例删除最后一个工作表。")
- With Globals.ThisWorkbook
- .Worksheets(.Worksheets.Count).delete()
- End With
- End Sub
- Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button5.Click
- MsgBox("此示例在最后增加一个工作表。")
- With Globals.ThisWorkbook
- .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
- End With
- End Sub
- Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button6.Click
- Call 单元格写入值()
- End Sub
- Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button7.Click
- Call 单元格写入值()
- MsgBox("下面开始排序。")
- With Globals.ThisWorkbook.Worksheets("工作表一")
- .range("A:B").sort(key1:=.range("B1"), Header:=Excel.XlYesNoGuess.xlYes)
- End With
- End Sub
- Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button8.Click
- Dim i&
- Call 单元格写入值()
- MsgBox("下面设置边框")
- With Globals.ThisWorkbook.Worksheets("工作表一")
- i = .range("a" & .Rows.count).end(3).row
- .range("A1:B" & i).Borders.LineStyle = 1
- MsgBox("下面设置单元格颜色")
- .range("A1:A" & i).Interior.ColorIndex = 6
- MsgBox("下面设置字体颜色")
- .range("B1:B" & i).Font.ColorIndex = 3
- End With
- End Sub
- End Class
复制代码 |
|