vbscript Excel宏用于我经常做的简单的事情,包括更改文本框和数字格式。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript Excel宏用于我经常做的简单的事情,包括更改文本框和数字格式。相关的知识,希望对你有一定的参考价值。

Sub ToLowerCase()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.Value = LCase(cell.Value)
    Next
    
End Sub

Sub ToUpperCase()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.Value = UCase(cell.Value)
    Next
    
End Sub

Sub ConvertToText()

    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "@"
        cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
    Next
    
End Sub

Sub ConvertToGeneral()

    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "General"
        cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
    Next
    
End Sub

'update cell values to match the number format of their cell
'if there are formulas in the selection, the user will be asked whether to convert them to values or not
Sub ForceNumberFormat()
    
    Dim cell As Object, answer
    
    answer = vbYes
    If Selection.HasFormula Then
        answer = MsgBox("Convert formulas to values?", vbYesNoCancel)
        If answer = vbCancel Then
            Exit Sub
        End If
    End If
    
    For Each cell In Selection
        If Not (cell.HasFormula And answer = vbNo) Then
            cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
        End If
    Next
    
End Sub

Sub PercentToInteger()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "0"
        If IsNumeric(cell) And Not IsEmpty(cell) Then
            cell.Value = cell.Value * 100
        End If
    Next
    
End Sub

以上是关于vbscript Excel宏用于我经常做的简单的事情,包括更改文本框和数字格式。的主要内容,如果未能解决你的问题,请参考以下文章

Excel 宏,用于从工作簿中的主工作表更新其他工作表

Excel里的宏有啥作用?

将 Excel 4 宏转换为 VBA

在VBScript文件中集成VBA

用于检查文件是不是存在的 Excel 宏

用于将数据粘贴到新表行中的 VBA 宏 - Excel