以下是一个可以实现将 Excel 文件夹中所有工作簿的单元格转换为文本格式,并删除公式内容的 VBA 代码:

Sub ConvertToText()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long
    Dim cell As Range
    
    'Loop through all workbooks in the folder
    For Each wb In Workbooks
        'Loop through all worksheets in the workbook
        For Each ws In wb.Worksheets
            'Find the last row and column with data
            lastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            lastCol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            
            'Loop through all cells with data and convert formulas to values
            For Each cell In ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
                If cell.HasFormula Then
                    cell.Value = cell.Value
                End If
                cell.NumberFormat = "@"
            Next cell
        Next ws
    Next wb
End Sub

将此代码复制到您的 Excel VBA 编辑器中,然后按 F5 运行。它将循环遍历您所选的文件夹中的所有 Excel 文件,并将每个工作表中的单元格转换为文本格式并删除公式。


原文地址: https://www.cveoy.top/t/topic/nBPo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录