本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2009年05月15日 统计字数: 1013字 阅读时间: 3分钟阅读 本文链接: https://soulteary.com/2009/05/15/how-to-use-vb-control-the-excel.html ----- # [VB]使用VB操作EXCEL内的数据 本例是转换Excel中的A1中的数字为大写并保存到A2中。 ```vb Private Sub cmdGet_Click() Dim lngTmp As Long Dim xlsApp As Object Set xlsApp = Excel.Application With xlsApp .Visible = True .Workbooks.Open (App.Path & "\target.xls") lngTmp = .Workbooks("target").Sheets("Sheet1").Range("A1").Value .Workbooks("target").Close .Quit End With Set xlsApp = Nothing MsgBox "表格中的数字是:" & lngTmp & vbNewLine & "转换后的数字是:" & MyConvert(CStr(lngTmp)) End Sub Private Function MyConvert(strNum As String) As String Dim intLen As Integer, intIndex As Integer Dim strTmp As String, strResult As String intLen = Len(strNum): strResult = "" For intIndex = 1 To intLen strTmp = Mid(strNum, intIndex, 1) strTmp = ConvertHan(strTmp) strResult = strResult & strTmp Next MyConvert = strResult End Function Private Function ConvertHan(strNum As String) As String Select Case strNum Case Is = "0": ConvertHan = "零" Case Is = "1": ConvertHan = "壹" Case Is = "2": ConvertHan = "贰" Case Is = "3": ConvertHan = "叁" Case Is = "4": ConvertHan = "肆" Case Is = "5": ConvertHan = "伍" Case Is = "6": ConvertHan = "陆" Case Is = "7": ConvertHan = "柒" Case Is = "8": ConvertHan = "捌" Case Is = "9": ConvertHan = "玖" End Select End Function ```