VB Code to copy and paste values

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
<article>
Dear Sir,
I need a simple vb code and will link it to the button, when I lick it, the value should be copied from;

Copy T2 into B22 to B31 next empty cell
Copy X2 into D22 to D31 next empty cell
Copy AB2 into E22 to E31 next empty cell
Copy AF2 into F22 to F31 next empty cell
Copy AJ2 into G22 to G31 next empty cell

Example;
"1" When Click the button, Value T2 should be copied to B22, similarly X2 TO D22 and so on
"2" When click same button again, the value of cell T2 should be copied in B23 since B22 is no mire a empty cell
if I clear value in Cell B22, it should work in same way "1"​
</article>


 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this code

Code:
Option Explicit


Sub CopyStuff()
Dim RowStartB As Integer
Dim RowStartD As Integer
Dim RowStartE As Integer
Dim RowStartF As Integer
Dim RowStartG As Integer
Dim DoneB As Boolean
Dim DoneD As Boolean
Dim DoneE As Boolean
Dim DoneF As Boolean
Dim DoneG As Boolean


RowStartB = 22
RowStartD = 22
RowStartE = 22
RowStartF = 22
RowStartG = 22
DoneB = False
DoneD = False
DoneE = False
DoneF = False
DoneG = False


Do While DoneB = False Or DoneD = False Or DoneE = False Or DoneF = False Or DoneG = False
    If DoneB = False Then
        If IsEmpty(ActiveSheet.Range("B" & RowStartB).Value) Then
            ActiveSheet.Range("B" & RowStartB).Value = ActiveSheet.Range("T2").Value
            DoneB = True
        Else
            RowStartB = RowStartB + 1
        End If
    End If
    If DoneD = False Then
        If IsEmpty(ActiveSheet.Range("D" & RowStartD).Value) Then
            ActiveSheet.Range("D" & RowStartD).Value = ActiveSheet.Range("T2").Value
            DoneD = True
        Else
            RowStartD = RowStartD + 1
        End If
    End If
    If DoneE = False Then
        If IsEmpty(ActiveSheet.Range("E" & RowStartE).Value) Then
            ActiveSheet.Range("E" & RowStartE).Value = ActiveSheet.Range("T2").Value
            DoneE = True
        Else
            RowStartE = RowStartE + 1
        End If
    End If
    If DoneF = False Then
        If IsEmpty(ActiveSheet.Range("F" & RowStartF).Value) Then
            ActiveSheet.Range("F" & RowStartF).Value = ActiveSheet.Range("T2").Value
            DoneF = True
        Else
            RowStartF = RowStartF + 1
        End If
    End If
    If DoneG = False Then
        If IsEmpty(ActiveSheet.Range("G" & RowStartG).Value) Then
            ActiveSheet.Range("G" & RowStartG).Value = ActiveSheet.Range("T2").Value
            DoneG = True
        Else
            RowStartG = RowStartG + 1
        End If
    End If
Loop
 
Last edited:
Upvote 0
Sorry didn't copy the end sub. need to add one after loop. Also they all copy from "T" so for "D" "T" should be "X" and so on.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top