Consulting

Results 1 to 3 of 3

Thread: Changing code from copy entire row to copy specific cells

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    15
    Location

    Changing code from copy entire row to copy specific cells

    Good Day

    I have the following piece of code to copy an entire row when a specific cell (AD of assetSheet) equals or is less than a specific date (A1 of sheet1), I would like to change that now only to copy specific cell values. cells A, B, C & M from that row on assetSheet should be copied to cells A,B,C & D in the next available row in dueSheet.

    The code is as follow:
    Private Sub CommandButton3_Click()
    
    Dim assetSheet As Worksheet
    Dim dueSheet As Worksheet
    Dim nextRow As Long
    Dim lastRow As Long
    Dim thisRow As Long
    
    
    ' Get the sheet references
    Set assetSheet = Sheet2
    Set dueSheet = Sheet6
    
    
    ' Find the last row on the asset sheet and the next row on the due sheet
    lastRow = assetSheet.Cells(assetSheet.Rows.Count, "A").End(xlUp).Row
    nextRow = dueSheet.Cells(dueSheet.Rows.Count, "A").End(xlUp).Row + 1
    
    
    ' Look at all rows in the asset sheet
    For thisRow = 1 To lastRow
        ' Check if column AD contains today's date
        If assetSheet.Cells(thisRow, "AD").Value <= sheet1.range("A1") Then
            ' Copy the cells to the due sheet
            assetSheet.Cells(thisRow, "A").EntireRow.Copy Destination:=dueSheet.Cells(nextRow, "A")
                 
            ' Move to the next row on the due sheet
            nextRow = nextRow + 1
        End If
    Next thisRow
    
    
    End Sub

  2. #2
    Try
    Intersect(assetsheet.Rows(thisRow), assetsheet.Range("A:C,M:M")).Copy Destination:=dueSheet.Cells(nextrow, "A")

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        
        With Sheet2.Range("A1").CurrentRegion
            .AutoFilter
            .AutoFilter 30, "<=" & Sheet1.Range("A1").Value2
            If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                Intersect(.Offset(1), .Range("A:C,M:M")).Copy _
                     Sheet6.Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
            .AutoFilter
        End With
    
    End Sub

Posting Permissions

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