如何优化Excel VBA代码

时间:2018-04-14 17:16:36

标签: excel vba excel-vba optimization

如何优化此代码?这需要花费大量时间来执行。

执行时间

Time of Execution

它的作用:将一个范围内的单元格列表与另一个范围内的另一个单元格列表进行比较,如果它们匹配,它将将第一个范围的第一个值替换为另一个范围内第二个值的相邻值。

注意:这是一个将在2000-5000行上运行的宏。

Sub Update_Btn()
    Application.ScreenUpdating = False
    Application.Cursor = xlWait

    Dim status As Range, r_status As Range, l_status As Range, rl_status As Range

    lastRowcs = Worksheets("Lists").Range("E" & Rows.Count).End(xlUp).Row
    Set status = Range("D2:D" & lastRow)
    Set l_status = Worksheets("Lists").Range("E3:E" & lastRowcs)

    For Each r_status In status
        For Each rl_status In l_status
            If r_status.Value = rl_status.Value Then
                rl_status.Offset(0, 1).Copy r_status
            End If
        Next rl_status
    Next r_status

    MsgBox "Done"

    Application.Cursor = xlDefault
    Application.ScreenUpdating = True
Exit Sub

1 个答案:

答案 0 :(得分:0)

使用数组 - class MyEdit(urwid.Edit): def __init__(self, my_attribute, caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None): super().__init__(caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None) self.my_attribute = [] # super().__super.__init__("", align, wrap, layout) def my_method(self): #some code that modifies my_attribute return self.my_attribute class Edit(Text): """ Text editing widget implements cursor movement, text insertion and deletion. A caption may prefix the editing area. Uses text class for text layout. Users of this class to listen for ``"change"`` events sent when the value of edit_text changes. See :func:``connect_signal``. """ # (this variable is picked up by the MetaSignals metaclass) signals = ["change"] def valid_char(self, ch): """ Filter for text that may be entered into this widget by the user :param ch: character to be inserted :type ch: bytes or unicode This implementation returns True for all printable characters. """ return is_wide_char(ch,0) or (len(ch)==1 and ord(ch) >= 32) def selectable(self): return True def __init__(self, caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None): """ :param caption: markup for caption preceeding edit_text, see :class:`Text` for description of text markup. :type caption: text markup :param edit_text: initial text for editing, type (bytes or unicode) must match the text in the caption :type edit_text: bytes or unicode :param multiline: True: 'enter' inserts newline False: return it :type multiline: bool :param align: typically 'left', 'center' or 'right' :type align: text alignment mode :param wrap: typically 'space', 'any' or 'clip' :type wrap: text wrapping mode :param allow_tab: True: 'tab' inserts 1-8 spaces False: return it :type allow_tab: bool :param edit_pos: initial position for cursor, None:end of edit_text :type edit_pos: int :param layout: defaults to a shared :class:`StandardTextLayout` instance :type layout: text layout instance :param mask: hide text entered with this character, None:disable mask :type mask: bytes or unicode >>> Edit() <Edit selectable flow widget '' edit_pos=0> >>> Edit("Y/n? ", "yes") <Edit selectable flow widget 'yes' caption='Y/n? ' edit_pos=3> >>> Edit("Name ", "Smith", edit_pos=1) <Edit selectable flow widget 'Smith' caption='Name ' edit_pos=1> >>> Edit("", "3.14", align='right') <Edit selectable flow widget '3.14' align='right' edit_pos=4> """ self.__super.__init__("", align, wrap, layout) self.multiline = multiline self.allow_tab = allow_tab self._edit_pos = 0 self.set_caption(caption) self.set_edit_text(edit_text) if edit_pos is None: edit_pos = len(edit_text) self.set_edit_pos(edit_pos) self.set_mask(mask) self._shift_view_to_cursor = False

Sheet "GB_Data" Rows: 5,001; Sheet "Lists" Rows: 5,001; Time: 6.438 sec

测试数据

表“GB_Data” - 之前

s1before

工作表“列表”

s2

表“GB_Data” - 之后

s1after