求一段excel vba代码!实现以下功能

2024-11-08 09:00:32
推荐回答(3个)
回答1:

不知 选择性粘贴 的哪一项能删除单元格中的空格?
我在前不久编了与你说的类似的宏。为了达到你的要求,可能要适当修改。请hi 我

'新建一Excel文件,把代码粘进去,执行,按提示操作

Sub 删除空格()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

BkName = ActiveWorkbook.Name

MsgBox "请选择 Excel文件 的路径!"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
folderspec = .SelectedItems(1)
If Right(SavePath, 1) <> "\" Then
folderspec = folderspec + "\"
End If
End With
Dim fs, f, f1, fc
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
sc = 1
Dim ran As Range
For Each f1 In fc
If Right(f1.Name, 3) = "xls" Then
fName = folderspec + f1.Name
' sName = Left(f1.Name, Len(f1.Name) - 4)
Workbooks.Open fName
For s = 1 To Sheets.Count
Sheets(s).Select
Set ran = Cells(1, 1).CurrentRegion
Row = ran.Rows.Count
col = ran.Columns.Count
For r = 1 To Row
For c = 1 To col
Cells(r, c) = Trim(Cells(r, c))
Next c
Next r
Next s
ActiveWorkbook.Save
ActiveWorkbook.Close
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "空格删除完毕!"

End Sub

回答2:

用代码没试过,提供以下手动方法(前提是原表格不含公式):

1,打开原文件
2,新建一个文件
3,在新文件的A1单元格 =If(点到原文件A1="","",点到原文件A1),回车
4,复制新A1,Ctrl+A,Ctrl+V,这样就用公式将原表中导入新表
5,新表中Ctrl+A,Ctrl+C,右建 - 选择性粘贴,数值

回答3:

楼主的问题可行性蛮高滴,只是有人会用太多时间来百度空间做这个工作量不少的事么。