+ Reply to Thread
Results 1 to 10 of 10

VBA copy and paste between worksheets with a variable

  1. #1
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    VBA copy and paste between worksheets with a variable

    Hi,

    i've tried to put a code together below but ive got quite confused so hoping someone can correct it or write a new one that is correct.

    Basically i want to copy data from one worksheet to another based on a variable.

    The data i want to copy is in worksheet "Labour Payments" ranging from column E6 to E57 (however this will eventually extend past E57, hence me trying to count the last cell).

    the variable data column is in worksheet "Labour Payments" alongside the data above ranging from column D6 to D57 (however this will eventually extend as stated above)

    the defined variable for the VBA copy and paste data required will be set in worksheet "CIS Return" cell D2 (below i just set as "15" as temporary whilst trying to work out the code)

    the location to paste is in worksheet "CIS Return" column B10 onwards.

    Any help would be much appreciated.

    Kind Regards

    Danny

    Below is the incorrect code i was attempting to get working:

    PHP Code: 
    Private Sub CommandButton1_Click()

    Worksheets("Labour Payments").Cells(Rows.Count5).End(xlUp).Row 5

    For 6 To a

        
    If Worksheets("Labour Payments").Cells(i4).Value "15" Then
            
            Worksheets
    ("Labour Payments").Rows(i).Copy
            Worksheets
    ("CIS Return").Activate
            b 
    Worksheets("CIS Return").Cells(Rows.Count2).End(xlUp).Row
            Worksheets
    ("CIS Return").Cells(12).Select
            activeworksheet
    .Paste
            Worksheets
    ("CIS Return").Activate
            

        End 
    If

    Next

    Application
    .CutCopyMode False


    End Sub 

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA copy and paste between worksheets with a variable

    Hello.
    The following two lines -combined- will make your attempt never work:

    Quote Originally Posted by Danny1986 View Post
    PHP Code: 
    Private Sub CommandButton1_Click()
    ...
            
    Worksheets("Labour Payments").Rows(i).Copy
    ...
            
    Worksheets("CIS Return").Cells(12).Select
    ...
    End Sub 
    After fixing that detail the macro will work but in a highly inefficient way.

    I suggest you upload your test workbook (with no more than 10-20 rows of data) to the Forum to see how you have structured your two sheets.
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: VBA copy and paste between worksheets with a variable

    Try:
    PHP Code: 
    Option Explicit
    Private Sub CommandButton1_Click()
    Dim a&, i&, CIS As WorksheetAs Range
    Set CIS 
    Worksheets("CIS Return")
    CIS.Range("B10:B10000").ClearContents ' delete old data
    a = Cells(Rows.Count, "E").End(xlUp).Row

    '
    Loop thru column Dthen combine cells (=15into an Union
    For 6 To a
        
    If Cells(i"D").Value CIS.Range("D2").Value Then
            
    If u Is Nothing Then
                Set u 
    Cells(i"E")
            Else
                
    Set u Union(uCells(i"E"))
            
    End If
        
    End If
    Next
    u
    .Copy ' copy union of cells then paste once from cell B10
    With CIS
        .Select
        .Range("B10").Select
        .Paste
        .Range("B10").Select
    End With
    Application.CutCopyMode = False
    End Sub 
    Attached Files Attached Files
    Quang PT

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,234

    Re: VBA copy and paste between worksheets with a variable

    Loop is always last option...Use Filter or advance filter...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: VBA copy and paste between worksheets with a variable

    Hi all, thanks for the comments.

    bebo021999 - your code doesn't seem to be working.

    beyond Excel - Please see attached template document Attachment 811012

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA copy and paste between worksheets with a variable

    Quote Originally Posted by Danny1986 View Post
    beyond Excel - Please see attached template document Attachment 811012
    Hello.
    What you posted looks like this:


    Therefore, I suggest you again: upload an extract of your workbook to the Forum as the images are of no use to understand the structure of your data.

  7. #7
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: VBA copy and paste between worksheets with a variable

    Apologies, hopefully it's attached correctly now.

    Kind Regards

    Danny
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this !


    According to the post #7 attachment an Excel basics VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
            
    Sheet22.[B8].CurrentRegion.Offset(2).ClearContents
        With Sheet2
    .[_FilterDatabase].Rows
            
    If .Parent.FilterMode Then .Parent.ShowAllData
           
    .AutoFilter 3Format(Sheet22.[D2], .Cells(23).NumberFormat)
            If 
    Application.Subtotal(103, .Columns(1)) > 1 Then .Item("2:" & .Count).Columns(4).Copy Sheet22.[B10]
            If .
    Parent.FilterMode Then .Parent.ShowAllData
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA copy and paste between worksheets with a variable

    .
    Hello Danny. We are improving.

    And now you need to indicate which columns (and in what order) of the 'Labor Payments' sheet should be moved to the four columns of the 'CIS Return' sheet: Hurry up, we're coming to the end of the year!

  10. #10
    Registered User
    Join Date
    12-16-2020
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    28

    Re: Hi, try this !

    Hi Marc,

    Great that worked perfect. Thanks for your help. As you can probably tell i dont have much knowledge of VBA coding but i do attempt to try, just very badly atm lol.
    i have extended your code for the next steps of what im trying to achieve. i have written VBA "sumifs" to pull through data from "sheet2" onto "sheet22". please see below:
    PHP Code: 
    Sub Demo1()

    Dim Name As String
    Dim Period_Number 
    As String
    Dim total_quantity 
    As Long



            Sheet22
    .[B8].CurrentRegion.Offset(2).ClearContents
        With Sheet2
    .[_FilterDatabase].Rows
            
    If .Parent.FilterMode Then .Parent.ShowAllData
           
    .AutoFilter 3Format(Sheet22.[D2], .Cells(23).NumberFormat)
            If 
    Application.Subtotal(103, .Columns(1)) > 1 Then .Item("2:" & .Count).Columns(4).Copy Sheet22.[B10]
            If .
    Parent.FilterMode Then .Parent.ShowAllData
        End With
        Range
    ("B9:B100").RemoveDuplicates Columns:=Array(1), Header:=xlYes
        
     
    'Total payments made (do not include VAT - whole £ only)
        total_quantity = 0
        Name = Worksheets("CIS Return").Cells(10, 2).Value
        Period_Number = Worksheets("CIS Return").Cells(2, 4).Value
        
        lastrow = Worksheets("Labour Payments").Cells(Rows.Count, 5).End(xlUp).Row
        
        For i = 6 To lastrow
        
        If Worksheets("Labour Payments").Cells(i, 5).Value = Name And Worksheets("Labour Payments").Cells(i, 4).Value = Period_Number Then
        total_quantity = total_quantity + Worksheets("Labour Payments").Cells(i, 10).Value
        End If
       
       Next
          
        Worksheets("CIS RETURN").Cells(10, 3).Value = total_quantity
        
        Worksheets("CIS RETURN").Cells(1, 1).Select
        
     '
    Cost of materials (paid for by subcontractor whole £ only)
        
    total_quantity 0
        Name 
    Worksheets("CIS Return").Cells(102).Value
        Period_Number 
    Worksheets("CIS Return").Cells(24).Value
        
        lastrow 
    Worksheets("Labour Payments").Cells(Rows.Count5).End(xlUp).Row
        
        
    For 6 To lastrow
        
        
    If Worksheets("Labour Payments").Cells(i5).Value Name And Worksheets("Labour Payments").Cells(i4).Value Period_Number Then
        total_quantity 
    total_quantity Worksheets("Labour Payments").Cells(i21).Value
        End 
    If
       
       
    Next
          
        Worksheets
    ("CIS RETURN").Cells(104).Value total_quantity
        Worksheets
    ("CIS RETURN").Cells(11).Select
        
     
    'Total tax deducted (£ and pence)
        total_quantity = 0
        Name = Worksheets("CIS Return").Cells(10, 2).Value
        Period_Number = Worksheets("CIS Return").Cells(2, 4).Value
        
        lastrow = Worksheets("Labour Payments").Cells(Rows.Count, 5).End(xlUp).Row
        
        For i = 6 To lastrow
        
        If Worksheets("Labour Payments").Cells(i, 5).Value = Name And Worksheets("Labour Payments").Cells(i, 4).Value = Period_Number Then
        total_quantity = total_quantity + Worksheets("Labour Payments").Cells(i, 18).Value
        End If
       
       Next
          
        Worksheets("CIS RETURN").Cells(10, 5).Value = total_quantity
        Worksheets("CIS RETURN").Cells(1, 1).Select
        
           
        
    End Sub 

    All is working, for the first row within "Sheet22" but i need it to repeat for the other rows below (again the number of rows may fluctuate on "Sheet22" cells B10 onwards from your initial code).

    please can you confirm how to adjust.

    Kind Regards

    Danny

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 07-04-2019, 08:14 AM
  2. [SOLVED] Copy/paste variable wbk names
    By Wheelie686 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2015, 12:35 PM
  3. [SOLVED] Copy paste value from variable rows below
    By Monty59 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2014, 03:29 AM
  4. Copy/Paste Range of Data from Multiple Workbooks/Worksheets to Master Workbook/Worksheets
    By NumberCruncher311 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-19-2013, 08:21 PM
  5. Copy and paste data in different worksheets of workbookA to diff worksheets in workbookB
    By preciousmetal2004 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2012, 05:50 PM
  6. Copy Paste Values all worksheets & creating new worksheets
    By ryan2600 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 11:46 AM
  7. [SOLVED] VBA - copy & paste to variable cells
    By nugey67 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-07-2012, 09:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1