Macro for command button to copy + paste on each click next row from table to another table

ColdSpirit

New Member
Joined
Sep 30, 2022
Messages
18
Office Version
  1. 2010
Platform
  1. Windows
Hey everyone,
hope you are all fine.
I have 2 Tables:
MasterTable
UsadasTable
I need, on each click on command button, to cut values on MasterTable B4/C4/D4 and paste to UsadasTable I4/J4/K4
On the next click, i need cut the values of the next row from MasterTable B5/C5/C5 and paste to UsadasTable I5/J5/K5, then on each click repeat until there is no more data.

1668799335883.png


Many thanks in advance.
And have a great weekend.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
VBA Code:
Sub copiar_sequencial_colar()

    Dim lngRowCounter As Long
    Dim lngRowCurrent As Long, lngRowEnd As Long
    Dim strCutAddress As String, strPasteAddress As String
    
    With ThisWorkbook.Sheets("Sheet1")
    
        lngRowEnd = .Range("B" & Rows.Count).End(xlUp).Row
        
        'locate current row for cut-paste operation
        For lngRowCounter = 4 To lngRowEnd
            If (.Range("B" & lngRowCounter) <> "") Then
                lngRowCurrent = lngRowCounter
                Exit For
            End If
        Next lngRowCounter
        
        'use current row to define cut-paste ranges
        
        strCutAddress = "B" & lngRowCurrent & ":D" & lngRowCurrent
        strPasteAddress = "I" & lngRowCurrent & ":K" & lngRowCurrent

        
        'execute cut-paste
        .Range(strCutAddress).Cut .Range(strPasteAddress)
        
        'if you don't literallly need cut and paste with all the formatting included _
        You just want to transfer the values, this should be faster.
        
        '.Range(strPasteAddress).Value2 = .Range(strCutAddress).Value2
        '.Range(strCutAddress).Value2 = ""
    
    End With
End Sub

Attach it to a shape or command button.
 
Upvote 0
Hello dear MCLIFTO8,
i really apreciate your help.

Your code works almost as intended, however since i have formulas, when the code is cutted or transfered, the formulas are transfered or cutted and pasted, losing all the formulas i have originally (on B/C/D).

How can i use the code to just copy or transfer the values maintaining all the formulas in each cell?

OR if it is not possible, just to copy the values of "D" maintaining all the functionality.

Once again, many many thanks for your help. Your help is really much apreciated.
 
Upvote 0
Cut and paste =/= Copy and paste =/= copy and paste values. I'll explain.

Cut and paste
Transfer the complete formulas and formatting from one cell to another. You will delete everything in the cell that was cut.
VBA Code:
'uses clip board
.Range(strCutAddress).Cut .Range(strPasteAddress)

Copy and paste (uses clip board)
Copy the complete formulas and formatting from one cell to another. You will keep everything in the cell that was copied.
VBA Code:
'uses clip board
.Range(strCutAddress).Copy .Range(strPasteAddress)

Copy and paste values
Copy the only the value that you see from one cell to another. You will keep everything in the cell that was copied.
VBA Code:
'uses clip board
.Range(strCutAddress).Copy
.Range(strPasteAddress).PasteSpecial Paste:=xlPasteValues

'does not use clip board (faster, less readable to novices)
.Range(strPasteAddress).Value2 = .Range(strCutAddress).Value2

@ColdSpirit
I think you are asking for copy and paste values.
I have written how to do that as in my original post. Just delete the following line of code:
VBA Code:
.Range(strCutAddress).Cut .Range(strPasteAddress)

replace that line of code you deleted with
VBA Code:
.Range(strPasteAddress).Value2 = .Range(strCutAddress).Value2
.Range(strCutAddress).Value2 = ""
 
Upvote 0
Hi again,
Once again, thank you for your explanation, which is appreciated.

I tried the code:
VBA Code:
.Range(strPasteAddress).Value2 = .Range(strCutAddress).Value2
.Range(strCutAddress).Value2 = ""

However it makes the origin cells on (strCutAddress) blank, removing the formula (I need to reutilize the formulas on that range later for when I click "inserir jogo AWB."
(I think is this part of the code:
VBA Code:
.Range(strCutAddress).Value2 = ""

The code is working as intended, however, this is the only "problem" I am having.
I am not with the file at the moment, when I will be I will post here the file.

Many thanks for your explanation. I learned a bit more of vba!

God bless you.
 
Upvote 0
(I think is this part of the code:
VBA Code:
.Range(strCutAddress).Value2 = ""
:eek: Yep that's an oversight, good catch. Delete that line and you will be good.

Many thanks for your explanation
No worries. Learning VBA is an incredibly rewarding experience, I hope it goes well for you.
 
Upvote 0
@MCLIFTO8
thank you for your reply.
When i remove that part of the code, it will just copy the first item. When i click again, nothing happens, that is, doesn't go to the other row.
I did some change to the code and the layout ( it was happening the same in the original layout).

I added one more columnn (E) to paste the data (same function) to (J), instead transfering/cutting from 3 columns and edited the code to fit the new layout:

VBA Code:
    Dim lngRowCounter As Long
    Dim lngRowCurrent As Long, lngRowEnd As Long
    Dim strCutAddress As String, strPasteAddress As String
    
    With ThisWorkbook.Sheets("047")
        
        lngRowEnd = .Range("E" & Rows.Count).End(xlUp).Row
        
        'locate current row for cut-paste operation
        For lngRowCounter = 5 To lngRowEnd
            If (.Range("E" & lngRowCounter) <> "") Then
                lngRowCurrent = lngRowCounter
                Exit For
            End If
        Next lngRowCounter
        
        'use current row to define cut-paste ranges
        
        strCutAddress = "E" & lngRowCurrent & ":E" & lngRowCurrent
        strPasteAddress = "J" & lngRowCurrent & ":J" & lngRowCurrent
        
        
        'execute cut-paste
        '.Range(strCutAddress).Cut .Range(strPasteAddress)
        
        'if you don't literallly need cut and paste with all the formatting included _
        You just want to transfer the values, this should be faster.
        
        .Range(strPasteAddress).Value2 = .Range(strCutAddress).Value2
        
        
    End With

1669281593801.png


I wonder if it isnt because of this part of the code:
VBA Code:
      If (.Range("E" & lngRowCounter) <> "") Then
                lngRowCurrent = lngRowCounter

Many thank for your amazing help.
 
Upvote 0
Here is my a piece of my sheet:

047.xlsm
ABCDEFGHIJK
1NI :0806055USADAS
2NF :0806154
3TOTAL AWB100
4SEQUÊNCIACHECK DIGITSEQ + DIGITAWBTAPAWBAWB2
5080605558060 0555047 - 8060 05558060555
6080605668060 0566047 - 8060 05668060566
7080605708060 0570047 - 8060 05708060570
8080605818060 0581047 - 8060 05818060581
9080605928060 0592047 - 8060 05928060592
10080606038060 0603047 - 8060 06038060603
047
Cell Formulas
RangeFormula
D3D3=D2-D1+1
C5:C10C5=IF(B5="","",MOD(B5,7))
D5:D10D5=CONCAT(LEFT(G5,4)&" "&RIGHT(G5,4))
E5:E10E5="047 - " &D5
G5,G7:G10G5=IFS(B$5="","",MOD(B$5,7),IF(B$5="","",CONCAT(B5,C5)))
G6G6=IFS(B$5="","",MOD(B$5,7),IF(B$5="","",CONCAT(B6,C6)))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Right, because it's not cut and pasting anymore it's copying and pasting.

In that case I'd check for the presence of a value on the paste column instead of the copy column as your copy column is no longer changing.

VBA Code:
Sub copiar_sequencial_colar()
    Dim lngRowCurrent As Long
    Dim strCopyAddress As String, strPasteAddress As String
    
    With ThisWorkbook.Sheets("047")
        lngRowCurrent = .Range("J" & Rows.Count).End(xlUp).Row + 1
        
        strCopyAddress = "E" & lngRowCurrent & ":E" & lngRowCurrent
        strPasteAddress = "J" & lngRowCurrent & ":J" & lngRowCurrent
        
        .Range(strPasteAddress).Value2 = .Range(strCopyAddress).Value2 'execute copy-paste
    End With
End Sub
 
Upvote 0
Solution
Hello again,
with the last code, it is not working. When i click the cmd button it just doesn't do anything.

Am i missing something or not doing the right way?

I noticed that on the original code we had this part of code:
VBA Code:
For lngRowCounter = 5 To lngRowEnd

It has anything to do with it?
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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