2012-03-28 97 views
0

我有大约40个工作簿,列数超过1000列,记录近100万条。Excel:如何将文本转换为多个工作簿的数字格式

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

除了使用粘贴特殊>乘法技术手动编辑每个文件,有没有一种方法来宏这个,以便它遍历一个特定文件夹中的所有excel文件?

+0

要通过所有的Excel文件迭代特定文件夹中看到这个[我的答案](HTTP:// stackoverflow.com/a/9864075/973283)。 – 2012-03-28 18:55:28

回答

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 
0
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 
相关问题