简体   繁体   中英

Excel - VBA - Range Selection Slows Down PC

Is there a way to turn these commands into one?

Worksheets("Agent html").Select
Range(Cells(7, TotalCallsRow), Cells(lastrow, TotalCallsRow)).Copy

When i give:

Worksheets("Agent html").Range(Cells(7, TotalCallsRow), Cells(lastrow, TotalCallsRow)).Copy

It returns me error

Application Defined or object-defined error.

Thanks in advance.

It should be:

Worksheets("Agent html").Range(Worksheets("Agent html").Cells(7, TotalCallsRow), Worksheets("Agent html").Cells(lastrow, TotalCallsRow)).Copy

It may be cleaner to define the worksheet as an object variable like:

Dim ws As Worksheet
Set ws = Worksheets("Agent html")

ws.Range(ws.Cells(7, TotalCallsRow), ws.Cells(lastrow, TotalCallsRow)).Copy

You are using Cells within Range , I'm not sure that's allowed. Either way, try to not use .Select

Dim wksht As Worksheet
Set wksht = Worksheets("Agent html")
Dim rng As Range
rng = wksht.Range(Cells(7, TotalCallsRow), Cells(lastrow, TotalCallsRow))
rng.Copy

Will do what you are asking above without selecting anything. I've not tested the Range(Cells but I can't help with that as I don't know what TotalCallsRow is?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM