首頁 >軟體教學 >辦公室軟體 >Excel中詳細製作圓角分的宏,實現數字轉換成大寫

Excel中詳細製作圓角分的宏,實現數字轉換成大寫

WBOY
WBOY轉載
2024-01-22 11:24:23564瀏覽

一、excel中把數字轉換成大寫帶圓角分的詳細製作方法?

如果你想將Excel中的數字轉換成大寫形式並帶有圓角分,可以透過以下步驟實現:

  1. 1. 準備資料: 在Excel中輸入需要轉換的數字。

  2. 2. 開啟VBA編輯器: 按下Alt   F11開啟VBA編輯器。

  3. 3. 插入新模組: 在VBA編輯器中,右鍵點選左側專案瀏覽器中的任意項,選擇「插入」 > “模組」以插入一個新的VBA模組。

  4. 4. 寫巨集程式碼: 在新模組中寫VBA程式碼,例如:

    Function ConvertToWordsWithCents(ByVal MyNumber)
        Dim Temp
        Dim DecimalPlace, Count
        ReDim Place(9) As String
        Place(2) = " Thousand "
        Place(3) = " Million "
        Place(4) = " Billion "
        Place(5) = " Trillion "
    
        ' Convert MyNumber to STRING
        MyNumber = Trim(CStr(MyNumber))
    
        ' If MyNumber is blank then we're done
        If MyNumber = "" Then Exit Function
    
        ' If MyNumber is 0 then we're done
        If Val(MyNumber) = 0 Then Exit Function
    
        ' Convert MyNumber to hopper
        DecimalPlace = InStr(MyNumber, ".")
        If DecimalPlace > 0 Then
            Temp = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
            MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
        End If
    
        Count = 1
        Do While MyNumber <> ""
            Temp = GetHundreds(Right(MyNumber, 3))
            If Temp <> "" Then OutF = Temp & Place(Count) & OutF
            If Len(MyNumber) > 3 Then
                MyNumber = Left(MyNumber, Len(MyNumber) - 3)
            Else
                MyNumber = ""
            End If
            Count = Count + 1
        Loop
    
        Select Case DecimalPlace
            Case 0
                ConvNumToWordsWithCents = " Dollar " & OutF & "Only"
            Case 1
                ConvNumToWordsWithCents = " Dollar " & OutF & "and " & GetTens(Left(Mid(MyNumber & "00", 2), 2)) & " Cents Only"
            Case 2
                ConvNumToWordsWithCents = " Dollar " & OutF & GetTens(Left(Mid(MyNumber & "00", 2), 2)) & " Cents Only"
        End Select
    End Function
    
    &#39; Converts a number from 100-999 into text
    Function GetHundreds(ByVal MyNumber)
        Dim Result As String
        If Val(MyNumber) = 0 Then Exit Function
        MyNumber = Right("000" & MyNumber, 3)
        &#39; Convert the hundreds place.
        If Mid(MyNumber, 1, 1) <> "0" Then
            Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
        End If
        &#39; Convert the tens and ones place.
        If Mid(MyNumber, 2, 1) <> "0" Then
            Result = Result & GetTens(Mid(MyNumber, 2))
        Else
            Result = Result & GetDigit(Mid(MyNumber, 3))
        End If
        GetHundreds = Result
    End Function
    
    &#39; Converts a number from 10 to 99 into text.
    Function GetTens(TensText)
        Dim Result As String
        Result = ""           &#39; Null out the temporary function value.
        If Val(Left(TensText, 1)) = 1 Then   &#39; If value between 10-19...
            Select Case Val(TensText)
                Case 10: Result = "Ten"
                Case 11: Result = "Eleven"
                Case 12: Result = "Twelve"
                Case 13: Result = "Thirteen"
                Case 14: Result = "Fourteen"
                Case 15: Result = "Fifteen"
                Case 16: Result = "Sixteen"
                Case 17: Result = "Seventeen"
                Case 18: Result = "Eighteen"
                Case 19: Result = "Nineteen"
                Case Else
            End Select
        Else                                 &#39; If value between 20-99...
            Select Case Val(Left(TensText, 1))
                Case 2: Result = "Twenty "
                Case 3: Result = "Thirty "
                Case 4: Result = "Forty "
                Case 5: Result = "Fifty "
                Case 6: Result = "Sixty "
                Case 7: Result = "Seventy "
                Case 8: Result = "Eighty "
                Case 9: Result = "Ninety "
                Case Else
            End Select
            Result = Result & GetDigit _
                (Right(TensText, 1))   &#39; Retrieve ones place.
        End If
        GetTens = Result
    End Function
    
    &#39; Converts a number from 1 to 9 into text.
    Function GetDigit(Digit)
        Select Case Val(Digit)
            Case 1: GetDigit = "One"
            Case 2: GetDigit = "Two"
            Case 3: GetDigit = "Three"
            Case 4: GetDigit = "Four"
            Case 5: GetDigit = "Five"
            Case 6: GetDigit = "Six"
            Case 7: GetDigit = "Seven"
            Case 8: GetDigit = "Eight"
            Case 9: GetDigit = "Nine"
            Case Else: GetDigit = ""
        End Select
    End Function
  5. 5. 關閉VBA編輯器: 關閉VBA編輯器,回到Excel。

  6. 6. 使用自訂函數: 在Excel中使用自訂函數ConvertToWordsWithCents,例如:

    =ConvertToWordsWithCents(A1)

以上程式碼會將數字轉換成大寫形式,並帶有圓角分。

二、教你如何製作Excel巨集?

製作Excel巨集可以透過以下步驟:

  1. 1. 開啟Excel: 開啟包含你要製作巨集的工作簿。

  2. 2. 開啟VBA編輯器: 按下Alt   F11開啟VBA編輯器。

  3. 3. 插入新模組: 在VBA編輯器中,右鍵點選左側專案瀏覽器中的任意項,選擇「插入」 > “模組」以插入一個新的VBA模組。

  4. 4. 寫巨集程式碼: 在新模組中寫VBA程式碼。這可以包括對單元格的操作、資料處理、圖表生成等。

    Sub MyMacro()
        &#39; Your VBA code here
        Range("A1").Value = "Hello, World!"
    End Sub
  5. 5. 儲存巨集: 在VBA編輯器中,點擊“檔案” > “另存為”,選擇檔案類型為“Excel巨集啟用工作簿(*.xlsm)」並儲存。

  6. 6. 執行巨集: 在Excel中,按#Alt   F8開啟巨集對話框,選擇你的巨集並點選「執行」。

總結:

對於將數字轉換成大寫形式並帶有圓角分,你可以使用VBA編寫自訂函數,並在Excel中呼叫。製作Excel巨集的步驟包括開啟VBA編輯器、插入新模組、編寫巨集程式碼、儲存巨集、執行巨集。這樣可以實現自訂功能和自動化操作。

Excel中詳細製作圓角分的宏,實現數字轉換成大寫

#

以上是Excel中詳細製作圓角分的宏,實現數字轉換成大寫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:docexcel.net。如有侵權,請聯絡admin@php.cn刪除