简体   繁体   中英

Excel VBA: Copy increment cell value

I'm new to VBA so i'm turning to you guys for some hints for the right direction.

I have a workbook with two sheets (Blad1, Blad2) and i'm trying to make a function that copies a premade range of cells from Blad1 to Blad2 in the next available cell. At the same time i wish for a number in the copied cells to increase by 10 each time i press the button so that every copy have different values.

This is the code i have written so far:

    Sub new_Group()
Dim rnTarget As Range
Set rnTarget = Blad2.Cells(Range("A:A").Cells.Count, 1).End(xlUp)
If rnTarget <> "" Then Set rnTarget = rnTarget.Offset(1)
Blad1.Range("A8:F15").Copy rnTarget
Blad1.Range("J9").Value = Blad1.Range("J9").Value + 10
End Sub

The copy/paste function is working as intended. But the problem is with the increment. I do not know how to copy the value only and not the formula so the value in the copied cells change and it also changes the J9 source cell to J10, J17 and so on.

Image of Blad1 cells

This is the premade cells in Blad1 ready for copy. The number after POS is what i want to increase by 10 after each copy.

Either i should use some other way of copying the cells so that it do not keep the formula but only the value or i'm going about the increment all wrong. Happy for any kind of pointers.

The below should paste only values

Sub new_Group()
Dim rnTarget As Range
Set rnTarget = Blad2.Cells(Range("A:A").Cells.Count, 1).End(xlUp)
If rnTarget <> "" Then Set rnTarget = rnTarget.Offset(1)
Blad1.Range("A8:F15").Copy 
Blad2.range(rnTarget.address).PasteSpecial xlValues
Blad1.Range("J9").Value = Blad1.Range("J9").Value + 10
End Sub

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