Excel (这是问题所在) Sub Test23() Dim rng As Range: Set rng = Application.Range("B11:G200") Dim cel As Range For E
(这是问题所在)Excel (这是问题所在) Sub Test23() Dim rng As Range: Set rng = Application.Range("B11:G200") Dim cel As Range For E,excel,vba,Excel,Vba,(这是问题所在) Sub Test23() Dim rng As Range: Set rng = Application.Range("B11:G200") Dim cel As Range For Each cel In rng.Cells With cel cel.Value = cel.Text End With Next cel End Sub Sub LoopAllE
Sub Test23()
Dim rng As Range: Set rng = Application.Range("B11:G200")
Dim cel As Range
For Each cel In rng.Cells
With cel
cel.Value = cel.Text
End With
Next cel
End Sub
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
Dim cel As Range
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
Set rng = Worksheets(1).Range("B11:G200")
For Each cel In rng.Cells
With cel
cel.Value = cel.Text
End With
Next cel
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
Dim cel As Range
On Error GoTo ErrHandler 'added this so that if something breaks, you remember to reset the screenupdating
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
GoTo NextCode 'if other than -1, user hit cancel so go to NextCode to kick off the resetsettings (this seems redundant, why not go directly to ResetSettings?)
ElseIf .Show = -1 Then 'this could also be a simple Else with no condition attached; I tend to use ElseIf with a specific condition to make sure I get exactly what I want
myPath = .SelectedItems(1) & "\" 'if user hit the action button, add it to myPath -- your earlier code skipped this part because there was no else statement
End If
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings 'before the change I made above, you would always jump right to ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
Set rng = Worksheets(1).Range("B11:G200")
For Each cel In rng.Cells
With cel
cel.Value = cel.Text
End With
Next cel
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Exit Sub 'exit the sub if we got this far without an error so that we don't run the error handler unless there is an error
ErrHandler:
MsgBox ("Sorry, something went wrong. The error code was " & Err.Number & " on line: " & Erl) 'tell the user what went wrong
GoTo ResetSettings 'cleanup
End Sub