显示标签为“VBA”的博文。显示所有博文
显示标签为“VBA”的博文。显示所有博文

2011年1月6日星期四

在Office中判断打开文件的数量

 

excel中的判断

If Workbooks.count = 0 Then
        Exit Sub
End If

 

word中的判断

If Documents.count = 0 Then
        Exit Sub
End If

2011年1月4日星期二

Office2007扩展Ribbon界面

参考资料:
http://sourcedaddy.com/ms-excel/customizing-existing-tab.html
http://openxmldeveloper.org/articles/customuieditor.aspx
具体过程两篇参考文献中都有,这里只讲要点:
1. 在什么文件中扩展Ribbon?
If you only want the custom Ribbon interface available for a specific document, create a macro-enabled document (.docm, .xlsm, or .pptm).
  • If you want the custom Ribbon interface available only for any document based on a particular template, create a macro-enabled template (.dotm, .xltm, or .potm).
  • If you want the custom Ribbon interface available for any open document in Word, modify the Normal template.
  • If you want the custom Ribbon interface available for any open document in Excel or PowerPoint, you need to create an Add-in file (.xlam or .ppam).
2. Office2007的文件格式实际是一个标准zip文件,可以通过修改扩展名后进行文件的添加、删除和修改
3. CustomUI Editor可以简化扩展的过程,包括图标
4. CustomUI Editor不支持中文,因此需要从文档包中把CustomUI.xml取出来,输入中文,保存时选择为unicode编码(UTF-8),然后去替代文档包中的该UI配置文件
5.图标的添加,如果是自定义文件图标的话,应该用image而不是imageMso
附件解压缩后是一个制作好的xlam文件,将其放到用户目录的\AppData\Roaming\Microsoft\AddIns下,并在Excel选项中启用该Add-in即可生效

http://u.115.com/file/f0b4513719

2011年1月2日星期日

补充几个Excel宏

 

'统计不同类型的实验项目的个数,并将数据拷贝到剪贴板方便使用,在使用时需要在“工程|引用”菜单中添加fm20.dll

Sub CountCells()
    Dim cell As Object
    Dim str As String
    Dim count, bcount, zcount, rcount As Integer
    Dim objData As DataObject
   
    count = 0
    bcount = 0
    zcount = 0
    rcount = 0
    Set objData = New DataObject
   
    For Each cell In Selection
        str = cell.Value
        If str = 基本" Then
            bcount = bcount + 1
        ElseIf str = 综合设计" Then
            zcount = zcount + 1
        ElseIf str = "研究" Then
            rcount = rcount + 1
        End If
        count = count + 1
    Next cell
   
    objData.SetText count & vbTab & bcount & vbTab & zcount & vbTab & rcount
    objData.PutInClipboard
End Sub

'填充选择的区域中的空白单元格为0
Sub FillCells()
    Dim cell As Object
    For Each cell In Selection
       If cell.Value = "" Then
        cell.Value = 0
       End If
    Next cell
End Sub

'替换选择区域中的单元格内容为指定内容

Sub ReplaceCells()
    Dim cell As Object
    For Each cell In Selection
       If Left(cell.Value, 2) = "验证" Or Left(cell.Value, 2) = "演示" Or Left(cell.Value, 2) = "基本" Then
        cell.Value = "基本"
       ElseIf Left(cell.Value, 2) = "综合" Or Left(cell.Value, 2) = "设计" Then
        cell.Value = "综合设计"
       Else
        cell.Value = "研究"
       End If
    Next cell
End Sub