简体   繁体   中英

VBA Excel 2013 Copy certain information from one worksheet to anther worksheet - weekly reports

I'm super new to VBA and hope that someone can help. This is what I have so far:

Public Sub Late()

    Dim LastRow As Long
    Dim MasterLastRow As Integer
    Dim NewRange As Range
    Dim TrackingCount As Integer


    Worksheets("Master").Select
    Range("A4").Select

    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Set NewRange = Cells(LastRow + 1, 1)
    Dim i As Integer
    For i = 1 To LastRow
        If TrackingCount > 14 Then 
           'Copy row, col. A:M of "Master" worksheet into Col. A:M of "Late Report" worksheet
        End If
    Next i

Explanation: For each TrackingCount in Col Q of "Master" worksheet that is > 14, I need to copy only these rows, from columns A thru M and paste them into "Late Report" worksheet (beginning with A:3), one row after the other (for each one with TrackingCount > 14).

Once that's accomplished, I need to do the same thing when Tracking count is between 7 & 14 (to paste in the Late Report spreadsheet following the first report); then between 2 & 6 for a third report; and finally when =< 0 for the final report.

All reports need to paste one after the other. This is for a weekly report in which the the row counts will be different each week.

I can't really see the whole problem without the sheet, but it seems to me you try to copy a certain amount of rows that satisfy a requirement.

A more efficient way to program this is to first

  • set a table in Excel using "Insert > Table".
  • Then use Data > Filter to filter all rows that satisfy the requirement
  • Then copy those rows (or part of the rows)

If you are unsure how to program this I recommend recording a macro. Also look into the Excel ListObject in VBA.

Ok, let's see if this is enough to get you started.

Option Explicit

Public Sub LateReport()
Dim MasterSheet As Worksheet
Dim DestinationSheet As Worksheet
Dim workingCell As Range
Dim workingRange As Range
Dim DestinationRow As Integer

Set MasterSheet = ThisWorkbook.Worksheets("Sheet1") ' You may want to correct the name
Set DestinationSheet = ThisWorkbook.Worksheets("Sheet2") ' And for this as well


Set workingRange = MasterSheet.Range("A4:M4").CurrentRegion ' Adjust this starting point as required

For Each workingCell In workingRange.Columns(1).Cells ' We'll work our way down each cell in the first column

    If workingCell.Offset(0, 16).Value > 14 Then ' The Offset method looks at the cell X rows and Y columns away; in our case, 0 rows down and 16 columns across
        workingCell.Range(workingCell, workingCell.Offset(0, 20)).Copy ' Copy that row
        DestinationSheet.Range("A3").Offset(DestinationRow, 0).PasteSpecial xlPasteValuesAndNumberFormats ' Paste it to our destination
    End If

    DestinationRow = DestinationRow + 1

Next

Set MasterSheet = Nothing
Set DestinationSheet = Nothing

End Sub

Some keywords

Option Explicit is a must. It ensures that we don't get vague errors if we haven't declared our variables correctly.

The next one is the CurrentRegion property. That selects all the cells around the one we've defined (in this case, cells A4 through M4) that have a value. It's a quick and easy way to look at and work with a block of cells in a collection.

Your next task is to adapt this code to handle cases where the value in column "Q" is between 7 and 14, between 2 and 6, and less than or equal to 1 (which I'm assuming is a typo in your original question: you've said "0").

Good luck!

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