简体   繁体   中英

Excel VBA copy the results in a For loop and paste it into a new sheets with numbers only

I am very new to VBA, and I have tried to write a paragraph to copy the results in a For loop and paste it into a new sheets with numbers only.

Range(Cells(42, 1), Cells(86, 20)).Select
Selection.Copy
Sheets("output").Select
b = 1 + 35 * a
Range(Cells(b, 1), Cells(b + 32, 20)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

The code can be executed, however, the output in the new sheet is empty. There will be 20 multiple empty blocks been copied into the new sheet. Send Help!!!

Try this:

b = 1 + 35 * a    

Range(Cells(42, 1), Cells(86, 20)).Copy 'consider using sheet reference here, like below :)
Sheets("output").Range(Cells(b, 1), Cells(b + 44, 20)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

First, I took care of ranges, so their dimensions match.

Secondly, it is highly recommended not to use Select and Selection . Use references instead as I did. It also increases readability of a code.

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