Excel VBA小程序

您所在的位置:网站首页 vba文本转换为数值 Excel VBA小程序

Excel VBA小程序

2023-07-18 11:23| 来源: 网络整理| 查看: 265

根据操作范围,这可以分为三种情况。

❶多工作表公式转数值

❷多工作簿公式转数值

1、多工作表公式转数值

如果是将当前工作簿所有工作表的公式转换为数值,需要使用到VBA代码。

Sub FunctionTransValue_Sheets() Dim sht As Worksheet For Each sht In Worksheets '遍历工作表 sht.UsedRange.Value = sht.UsedRange.Value Next MsgBox "ok" End Sub

2、多工作簿公式转数值

复制运行以下代码即可▼

Sub FunctionTransValue_Workbooks() '全部工作簿 Dim strPath As String, sht As Worksheet Dim strWbName As String, wb As Workbook '1、选择目标文件夹,并获取文件夹的路径。 With Application.FileDialog(msoFileDialogFolderPicker) '获取文件夹路径 If .Show Then strPath = .SelectedItems(1) & "\" Else Exit Sub End With On Error Resume Next '2、取消一系列系统设置 With Application .ScreenUpdating = False '取消屏幕刷新 .DisplayAlerts = False '取消警告信息 .EnableEvents = False '取消事件 .Calculation = xlCalculationManual '取消公式重算 .AskToUpdateLinks = False '取消外链询问 End With '3、遍历工作簿,工作表,将公式转换为数值。 strWbName = Dir(strPath & "*.xls*") Do While strWbName "" 'dir语句遍历excel文件 If strWbName ThisWorkbook.Name Then Set wb = Workbooks.Open(strPath & strWbName) '打开工作簿 For Each sht In wb.Worksheets '遍历工作表公式转数值 sht.UsedRange.Value = sht.UsedRange.Value Next wb.Close True '保存关闭工作簿 End If strWbName = Dir() '下一个excel文件 Loop ’4、恢复一系列系统设置 With Application .ScreenUpdating = True .DisplayAlerts = True .EnableEvents = True .Calculation = xlCalculationAutomatic .AskToUpdateLinks = True End With '5、反馈程序运行结果 If Err.Number Then MsgBox Err.Description Else MsgBox "转换完成。" End If End Sub


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3