首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何优化VBA代码以进行格式化?

如何优化VBA代码以进行格式化?
EN

Stack Overflow用户
提问于 2020-03-23 06:44:13
回答 1查看 58关注 0票数 0

我有下面的代码,这将帮助我做一些格式化。但是我想通过减少时间来提高代码的效率。下面是宏将要执行的格式化步骤。

    1. Convert "Q“和"S”列,通过在"I“列旁边插入列,将”i“列编号到新列。
    2. 剪切"AD”列并粘贴到“O”列。
    3. 删除列("A:A“,AD:AG")
    4. Replace "#“表示空,"OUT”在"AC“中使用P输入值,column.
    5. Round中的"Q”和"S“列编号为2 decimal.
    6. Change --通过将"Q”列上的-1(*-1)
    7. Filter与"0“相乘,并在"S”列上筛选"0“,使Q列中的值符号变为2 decimal.
    8. Change。然后删除"Q“和"S”为零的行。只清除"S“列上的"Q”和"R“Columns.
    9. Filter 0的可见单元格,只清除"S”和"T“的可见单元格Columns.
    10. Copy (ThisWorkbook.Sheets("Tool").Range("A20:AC20").Copy),并将除已使用的range.

之外没有数据的所有列和行粘贴到文件formatted.

  • Remove的A1中

目前,宏工作正常,但是使用一些time.As,我对VBA来说还是新手,不知道如何优化代码。因此,我在这里寻求专家的帮助。提前谢谢。

下面是代码

代码语言:javascript
运行
复制
Sub Ananplan_to_BPM()
Dim LastRow As Long
Dim Lastcol As Long
Dim P As String
 'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
  With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = True
        'Filter to just the following types of files to narrow down selection options
        '.Filters.Add "All Files", "*.xlsx; *.xlsm; *.xls; *.xlsb; *.csv"
        'Show the dialog box
        .Show
        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
    End With
    'It's a good idea to still check if the file type selected is accurate.
    'Quit the procedure if the user didn't select the type of file we need.
    If InStr(fullpath, ".xls") = 0 Then
    If InStr(fullpath, ".csv") = 0 Then
        Exit Sub
    End If
    End If
 'Open the file selected by the user
    Workbooks.Open fullpath
    P = InputBox("Please Enter the Version")
    Application.ScreenUpdating = False
With ActiveWorkbook
    Columns(17).NumberFormat = "0"
    Columns(19).NumberFormat = "0"
    LastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
    Columns("I").Copy
    Columns("I").Insert Shift:=xlToRight
    'Range("AE2").Value = P
    'Range("AE2", "AE" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
    Columns("AE").Copy
    Columns("P").PasteSpecial xlPasteValues
    ActiveSheet.Range("A:A,AE:AG").EntireColumn.Delete
    Columns("AC").Replace What:="#", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Columns("AC").Replace What:="OUT", Replacement:=P, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Range("AD2").Formula = "=Round(Q2,2)"
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
    Range("Q2").PasteSpecial xlPasteValues
    Range("AD2").Formula = "=Round(S2,2)"
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
    Range("S2").PasteSpecial xlPasteValues
    Range("AD2").Formula = "=(Q2*-1)"
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
    Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
    Range("Q2").PasteSpecial xlPasteValues
    Columns("AD:AD").EntireColumn.Delete
With ActiveSheet.Range("A:AC")
    .AutoFilter Field:=17, Criteria1:="0"
    .AutoFilter Field:=19, Criteria1:="0"
    .Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter
    .AutoFilter Field:=17, Criteria1:="0"
    .Range("Q2:R" & LastRow).SpecialCells(xlCellTypeVisible).Clear
    .AutoFilter
    .AutoFilter Field:=19, Criteria1:="0"
    .Range("S2:T" & LastRow).SpecialCells(xlCellTypeVisible).Clear
    .AutoFilter
    '.Range("C2").AutoFill .Range("C2:C" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    End With
End With
ThisWorkbook.Sheets("Tool").Range("A20:AC20").Copy
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Rows("1").SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
'ActiveWorkbook.Save
'ActiveWorkbook.Close
MsgBox "Done With Farmatting"
End Sub
EN

回答 1

Stack Overflow用户

发布于 2020-03-23 08:52:00

这不是一个审查代码的网站。还有另外一个,特别是在StackOverflow家族中。尽管如此,我还是检查了您的代码,但没有发现任何可能使其特别慢的地方。应该有更快地完成这项工作的方法,但它们需要了解你的意图。你好像有一张大工作表。所以喝杯咖啡可能需要一点时间,但还不够。因此,我的评论集中在代码固有的不精确性上,这使得代码很容易崩溃,如果它丢失在错误的工作表上,也容易造成无法描述的损坏。我补充了一些意见。

代码语言:javascript
运行
复制
Sub Ananplan_to_BPM()

    Dim LastRow As Long
    Dim LastCol As Long
    Dim P As String

    ' Display a Dialog Box that allows to select a single file.
    ' The path for the file picked will be stored in fullpath variable
    With Application.FileDialog(msoFileDialogFilePicker)
        ' Makes sure the user can select only one file - quite the opposite
        .AllowMultiSelect = True
        'Filter to just the following types of files to narrow down selection options
        '.Filters.Add "All Files", "*.xlsx; *.xlsm; *.xls; *.xlsb; *.csv"
        'Show the dialog box
        .Show
        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
    End With

    ' It's a good idea to still check if the file type selected is accurate.
    If InStr(fullpath, ".xls") = 0 Or InStr(fullpath, ".csv") = 0 Then
        ' Quit the procedure if the user didn't select the type of file we need.
        Exit Sub
    End If

    'Open the file selected by the user
    Workbooks.Open fullpath
    P = InputBox("Please Enter the Version")
    Application.ScreenUpdating = False

    With ActiveWorkbook
        ' There isn't a single reference to the ActiveWorkbook
        ' in the entire 'With' bracket.
        ' Create a link to the 'With' object by a leading period.
        ' Example:-
'        With .Worksheets(1)                 ' linked to ActiveWorkbook
'            ' below, both cells and Rows.Count of Worksheets(1)
'            LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
'        End With

        ' which sheet are you working on here?
        LastRow = Cells(Rows.Count, 2).End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        Columns(17).NumberFormat = "0"
        Columns(19).NumberFormat = "0"
        Columns("I").Copy
        Columns("I").Insert Shift:=xlToRight
        'Range("AE2").Value = P
        'Range("AE2", "AE" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
        Columns("AE").Copy
        Columns("P").PasteSpecial xlPasteValues

        ' You didn't activate any sheet
        ActiveSheet.Range("A:A,AE:AG").EntireColumn.Delete
        ' everything you do above or below this line
        '' is done to the ActiveSheet


        Columns("AC").Replace What:="#", Replacement:="", LookAt:=xlPart, _
                              SearchOrder:=xlByRows, MatchCase:=False, _
                              SearchFormat:=False, ReplaceFormat:=False
        Columns("AC").Replace What:="OUT", Replacement:=P, LookAt:=xlPart, _
                              SearchOrder:=xlByRows, MatchCase:=False, _
                              SearchFormat:=False, ReplaceFormat:=False

        ' This should probably be done using a cell format.
        ' If you need rounded values in later calculations do
        ' the rounding in later calculations, not in the original data.
        Range("AD2").Formula = "=Round(Q2,2)"
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
        Range("Q2").PasteSpecial xlPasteValues

        Range("AD2").Formula = "=Round(S2,2)"
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
        Range("S2").PasteSpecial xlPasteValues

        Range("AD2").Formula = "=(Q2*-1)"
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
        Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
        Range("Q2").PasteSpecial xlPasteValues

        Columns("AD:AD").EntireColumn.Delete
    End With

    With ActiveSheet.Range("A:AC")
        ' This method will throw an error if there are no visible cells
        ' why not suppress the display of zero with a CellFormat?
        .AutoFilter Field:=17, Criteria1:="0"
        .AutoFilter Field:=19, Criteria1:="0"
        .Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
        .AutoFilter Field:=17, Criteria1:="0"
        .Range("Q2:R" & LastRow).SpecialCells(xlCellTypeVisible).Clear
        .AutoFilter
        .AutoFilter Field:=19, Criteria1:="0"
        .Range("S2:T" & LastRow).SpecialCells(xlCellTypeVisible).Clear
        .AutoFilter
        '.Range("C2").AutoFill .Range("C2:C" & .Cells(.Rows.Count, "B").End(xlUp).Row)
    End With

    ThisWorkbook.Sheets("Tool").Range("A20:AC20").Copy
    ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues

    ' you are still working on the undefined ActiveSheet
    Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Rows("1").SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
    'ActiveWorkbook.Save
    'ActiveWorkbook.Close
    MsgBox "Done With Formatting"
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60808965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档