【问题标题】:Excel: How to convert text to number formats for multiple workbooksExcel:如何将文本转换为多个工作簿的数字格式
【发布时间】:2012-03-28 18:17:48
【问题描述】:

我有大约 40 个工作簿,包含 1000 多列和近 100 万条记录。

不幸的是,大部分数据是以文本格式导入的,我正在尝试将特定列转换为数字格式。

除了使用特殊粘贴 > 乘法技术手动编辑每个文件之外,有没有办法对其进行宏化,以便遍历特定文件夹中的所有 excel 文件?

【问题讨论】:

  • 要遍历特定文件夹中的所有 excel 文件,请参阅answer of mine。

标签: copy format excel


【解决方案1】:

您知道要更改的列和数字。您可以录制一个宏并将其插入到这个基本的 DIR() 技术中:

Option Explicit

Sub LoopThroughFolder()
Dim fPATH As String, fNAME As String
Dim wb As Workbook

fPATH = "C:\Path\To\My\Files\"      'remember the final \
fNAME = Dir(fPATH & "*.xl*")        'get first filename from fPATH
Application.ScreenUpdating = False  'speed up execution

    Do While Len(fNAME) > 0
        Set wb = Workbooks.Open(fPATH & fNAME)

        'your code here to format that activesheet

        wb.Close True       'save and close the edited file

        fNAME = Dir         'get the next filename
    Loop

Application.ScreenUpdating = True
End Sub

【讨论】:

    【解决方案2】:
    Option Compare Database
    Public Function format(filepath, sheetname, sheetpath)
    
    
    Set xls = CreateObject("EXCEL.APPLICATION")
    xls.screenupdating = False
    xls.displayalerts = False
    xls.Visible = True
    xls.workbooks.Open filepath
    Set xlsdd = xls.ActiveWorkbook
    
    'deleting headers
    
    xls.Range("1:1").Select
    xls.Selection.Delete Shift:=xlUp
    
    'adding one column
    
    xls.Columns("A:A").Select
    xls.Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    
    'adding 5 rows
     'ActiveWorkbook.Sheets("sheet1").Select
    xls.Rows("1:5").Insert Shift:=xlDown
    
     ' fetching rows from access and putting them into excel
    '  strsql = "select top 5 " & sheetname & ".* into top5_records from " & sheetname
    '  DoCmd.RunSQL strsql
    '  outputFileName = "C:\Users\hp\Desktop\top5_records.xls"
    '  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "top5_records",   outputFileName, True
    
    'then open that excel and copy the rows
    
    Set xls2 = CreateObject("EXCEL.APPLICATION")
    xls2.screenupdating = False
    xls2.displayalerts = False
    xls2.Visible = True
    xls2.workbooks.Open sheetpath
    Set xlsdd2 = xls2.ActiveWorkbook
    xls2.Rows("1:5").Select
    xls2.Selection.Copy
    xls.Cells(1, 1).Select
    xls.activesheet.Paste
    
    
    
     'making first 6th row to be bold
    
    xls.Rows("6:6").Select
    With xls.Selection.Font
    .Bold = True 
     .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
    End With
    
    'autofit the data
    
    xls.Sheets(sheetname).Cells.Columns.autofit
    xls.CutCopyMode = False
    
    'making both the excel objects to be free
    
    With xlsdd
    .Save
    .Close
    End With
    xls.Visible = False
    
    Set xlsdd = Nothing
    Set xls = Nothing
    
    With xlsdd2
    .Save
    .Close
    End With
    xls2.Visible = False
    
    Set xlsdd2 = Nothing
    Set xls2 = Nothing
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2017-11-18
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      相关资源
      最近更新 更多