excel 合并2个代码作为1个代码自动运行

66bbxpm5  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(123)

你能帮我把这2段代码合并起来,去掉所有不必要的行,避免长时间运行吗?我试着把它合并起来,但是得到了运行时错误#9

步骤1(代码#1)

Sub STEP1()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
    With ws.Range("B:B")
        .NumberFormat = "General"
        .Value = .Value
        .HorizontalAlignment = xlLeft
    End With
    Debug.Print ws.Name
Next ws
Application.ScreenUpdating = True
End Sub

步骤2(代码#2)

Sub STEP_2()

    Dim ws As Worksheet
    For Each ws In Sheets
        ws.Cells(1, 1).EntireColumn.Delete
    Next ws
    Sheets("x_ 659358").Select
    Rows("2:3").Select
    Selection.Delete Shift:=xlUp
    Sheets("x_682549 (2)").Select
    Application.DisplayAlerts = False
    ActiveSheet.Delete
    Application.DisplayAlerts = True
    Dim headers() As Variant
    Application.ScreenUpdating = False
    Set wb = ActiveWorkbook
    headers() = Array("sku", "barcode", "active", "price")
    For Each ws In wb.Sheets
        With ws
        .Rows(1).Value = ""
        For i = LBound(headers()) To UBound(headers())
            .Cells(1, 1 + i).Value = headers(i)
        Next i
        .Rows(1).Font.Bold = True
        End With
    Next ws
    
    Dim xWs As Worksheet
    Dim xDir As String
    Dim folder As FileDialog
    Set folder = Application.FileDialog(msoFileDialogFolderPicker)
    If folder.Show <> -1 Then Exit Sub
    xDir = folder.SelectedItems(1)
    For Each xWs In Application.ActiveWorkbook.Worksheets
        xWs.SaveAs xDir & "\" & xWs.Name, xlCSV
    Next

End Sub

我试过合并,但总是卡住

2ic8powd

2ic8powd1#

Sub STEP1()
' your code...
Call STEP_2() ' <----
End Sub

Sub STEP_2()
' your code...
End Sub

相关问题