How to improve the speed of VBA macro code?

前端 未结 7 1538
孤城傲影
孤城傲影 2020-12-06 07:11

I do not have much experience with writing macros, and therefore need the help of this community for the following issue encountered:

My macro copies a range of valu

7条回答
  •  萌比男神i
    2020-12-06 07:39

    Best way to improve performance based on my experience is to work on variables in code rather than accessing the spreadsheet every time you want to lookup a value. Save any range you want to work with in a variable(variant) and then iterate through it as if it was the sheet.

        dim maxRows as double
        dim maxCols as integer.
        dim data as variant
    with someSheet
        maxRows = .Cells(rows.count, 1).end(xlUp).row             'Max rows in sheet
        maxCols = .Cells(1, columns.count).end(xlToLeft).column   'max columns in sheet
        data = .Range(.Cells(1,1), .Cells(maxRows, maxCols))      'copy range in a variable
    end with
    

    From here you can access the data variable as if it was the spreadsheet like - data(row, column) with MUCH MUCH faster read speed.

提交回复
热议问题