Excel 如何从工作表中提取行并逐行粘贴到另一个工作表上

Excel 如何从工作表中提取行并逐行粘贴到另一个工作表上,excel,vba,Excel,Vba,我是vba/编码新手,不知道如何从这一点开始。我目前正在尝试复制符合我的三个条件的行的某些列,并将其逐行粘贴到另一个工作表中,以便将其列出。 我知道我的逻辑很全面,但如果我能得到一些建议,我将不胜感激 Application.ScreenUpdating = False Dim Today, EndDate as Date Dim MainWorksheet as worksheet Today = Sheets("sheet1").Range("k8&qu

我是vba/编码新手,不知道如何从这一点开始。我目前正在尝试复制符合我的三个条件的行的某些列,并将其逐行粘贴到另一个工作表中,以便将其列出。 我知道我的逻辑很全面,但如果我能得到一些建议,我将不胜感激

Application.ScreenUpdating = False 

Dim Today, EndDate as Date

Dim MainWorksheet as worksheet

Today = Sheets("sheet1").Range("k8").Value
EndDate = Sheets("sheet1").Range("k9").Value

Set MainWorksheet = Worksheets("sheet2")

Dim Name as String

Name = "Condition 1"

a = MainWorksheet.Cells(Rows.Count, 1).End(xlUP).Row

For i = 2 to a

    Dim z as boolean
    Dim x as boolean
    Dim c as boolean

    z = Mainworksheet.Cells(i,7).Value >= Today
    x = Mainworksheet.Cells(i,8).Value <= EndDate
    c = Mainworksheet.Cells(i,6).Value = Name

If z And x And c = True Then
    
    MainWorksheet.Rows(i).Range("b1,f1,g1,h1,k1,d1").copy

    worksheets("sheet1").Activate

    Range("k8").Select

        If ActiveCell.Value = "" then

        Activecell.PasteSpecial

            Else

                ActiveCell.offset(1,0).select

                Activecell.PasteSpecial

            End if

       End if

 Next i


End sub

Application.ScreenUpdate=False
Dim Today,EndDate作为日期
将主工作表设置为工作表
今天=板材(“板材1”).范围(“k8”).值
EndDate=图纸(“图纸1”).范围(“k9”).值
设置主工作表=工作表(“表2”)
将名称设置为字符串
Name=“条件1”
a=mainsheet.Cells(Rows.Count,1).End(xlUP).Row
对于i=2到a
将z作为布尔值
将x作为布尔值
作为布尔值的dimc
z=mainsheet.Cells(i,7).Value>=今天
x=mainsheet.Cells(i,8)。值复制行的指定单元格(列)
  • 虽然它能完成任务,但它做得很慢
  • 请注意,如果只需要复制值(无公式、格式),则不同的解决方案可能会将执行速度提高几十倍。此外,实现阵列可能会将执行速度提高数百倍
代码

Option Explicit

Sub copyReport()
    
    ' Constants
    Const Criteria As String = "Condition 1"
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' Workbook containing this code.
    
    ' Source Worksheet
    Dim src As Worksheet: Set src = wb.Worksheets("Sheet2")
    ' Last Row, the row of the last non-empty cell in column 'A'
    Dim srcLast As Long: srcLast = src.Cells(src.Rows.Count, "A").End(xlUp).Row
    ' Destination Worksheet
    Dim dst As Worksheet: Set dst = wb.Worksheets("Sheet1")
    ' Last Cell, the last non-empty cell in column 'K'
    Dim dCell As Range: Set dCell = dst.Cells(dst.Rows.Count, "K").End(xlUp)
    ' Dates: Start Date, End Date
    Dim sDate As Date: sDate = dst.Range("K8").Value
    Dim eDate As Date: eDate = dst.Range("K9").Value
    
    ' Declare variables.
    Dim rng As Range ' Current Source Range
    Dim sCell As Range ' Current Cell in Current Area of Current Source Range
    Dim i As Long ' Source Rows Counter
    Dim j As Long ' Destination Columns Counter
    Dim bCrit As Boolean ' Criteria Validator
    Dim bStart As Boolean ' Start Date Validator
    Dim bEnd As Boolean ' End Date Validator
    
    Application.ScreenUpdating = False
    
    ' Loop
    For i = 2 To srcLast
        
        bCrit = (src.Cells(i, "F").Value = Criteria)
        bStart = (src.Cells(i, "G").Value >= sDate)
        bEnd = (src.Cells(i, "H").Value <= eDate)
        
        If bCrit And bStart And bEnd Then
            Set rng = src.Rows(i).Range("B1,F1,G1,H1,K1,D1")
            Set dCell = dCell.Offset(1)
            ' You cannot use 'rng.Copy dCell' because it will copy
            ' "B1,D1,F1,G1,H1,K1".
            j = 0
            For Each sCell In rng.Areas
                sCell.Copy dCell.Offset(, j)
                j = j + 1
            Next sCell
        End If
    
    Next i

    Application.ScreenUpdating = False

End Sub
选项显式
子副本报告()
“常数
常量条件为String=“条件1”
'工作簿
将wb设置为工作簿:设置wb=ThisWorkbook包含此代码的工作簿。
'源工作表
将src标注为工作表:设置src=wb。工作表(“表2”)
'最后一行,列'A'中最后一个非空单元格的行'
将srcLast设置为Long:srcLast=src.Cells(src.Rows.Count,“A”).End(xlUp).Row
'目标工作表
将dst标注为工作表:设置dst=wb。工作表(“表1”)
'最后一个单元格,列'K'中最后一个非空单元格'
Dim dCell As Range:设置dCell=dst.Cells(dst.Rows.Count,“K”).End(xlUp)
'日期:开始日期,结束日期
尺寸sDate作为日期:sDate=dst.范围(“K8”)值
尺寸eDate作为日期:eDate=dst.范围(“K9”)值
'声明变量。
变暗rng As范围“电流源范围
Dim sCell As Range“电流源范围的当前区域中的当前单元格
Dim i As Long“源行计数器
Dim j As Long“目标列计数器
Dim bCrit作为布尔标准验证器
Dim bStart作为布尔值的“开始日期验证器”
Dim bEnd作为布尔值“结束日期验证器”
Application.ScreenUpdating=False
'循环
对于i=2到srcLast
bCrit=(src.单元格(i,“F”).值=标准)
bStart=(src.Cells(i,“G”).Value>=sDate)
bEnd=(src.Cells(i,“H”)。值复制行的指定单元格(列)
  • 虽然它能完成任务,但它做得很慢
  • 请注意,如果只需要复制值(无公式、格式),则不同的解决方案可能会将执行速度提高数十倍。此外,实现数组可能会将执行速度提高数百倍
代码

Option Explicit

Sub copyReport()
    
    ' Constants
    Const Criteria As String = "Condition 1"
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' Workbook containing this code.
    
    ' Source Worksheet
    Dim src As Worksheet: Set src = wb.Worksheets("Sheet2")
    ' Last Row, the row of the last non-empty cell in column 'A'
    Dim srcLast As Long: srcLast = src.Cells(src.Rows.Count, "A").End(xlUp).Row
    ' Destination Worksheet
    Dim dst As Worksheet: Set dst = wb.Worksheets("Sheet1")
    ' Last Cell, the last non-empty cell in column 'K'
    Dim dCell As Range: Set dCell = dst.Cells(dst.Rows.Count, "K").End(xlUp)
    ' Dates: Start Date, End Date
    Dim sDate As Date: sDate = dst.Range("K8").Value
    Dim eDate As Date: eDate = dst.Range("K9").Value
    
    ' Declare variables.
    Dim rng As Range ' Current Source Range
    Dim sCell As Range ' Current Cell in Current Area of Current Source Range
    Dim i As Long ' Source Rows Counter
    Dim j As Long ' Destination Columns Counter
    Dim bCrit As Boolean ' Criteria Validator
    Dim bStart As Boolean ' Start Date Validator
    Dim bEnd As Boolean ' End Date Validator
    
    Application.ScreenUpdating = False
    
    ' Loop
    For i = 2 To srcLast
        
        bCrit = (src.Cells(i, "F").Value = Criteria)
        bStart = (src.Cells(i, "G").Value >= sDate)
        bEnd = (src.Cells(i, "H").Value <= eDate)
        
        If bCrit And bStart And bEnd Then
            Set rng = src.Rows(i).Range("B1,F1,G1,H1,K1,D1")
            Set dCell = dCell.Offset(1)
            ' You cannot use 'rng.Copy dCell' because it will copy
            ' "B1,D1,F1,G1,H1,K1".
            j = 0
            For Each sCell In rng.Areas
                sCell.Copy dCell.Offset(, j)
                j = j + 1
            Next sCell
        End If
    
    Next i

    Application.ScreenUpdating = False

End Sub
选项显式
子副本报告()
“常数
常量条件为String=“条件1”
'工作簿
将wb设置为工作簿:设置wb=ThisWorkbook包含此代码的工作簿。
'源工作表
将src标注为工作表:设置src=wb。工作表(“表2”)
'最后一行,列'A'中最后一个非空单元格的行'
将srcLast设置为Long:srcLast=src.Cells(src.Rows.Count,“A”).End(xlUp).Row
'目标工作表
将dst标注为工作表:设置dst=wb。工作表(“表1”)
'最后一个单元格,列'K'中最后一个非空单元格'
Dim dCell As Range:设置dCell=dst.Cells(dst.Rows.Count,“K”).End(xlUp)
'日期:开始日期,结束日期
尺寸sDate作为日期:sDate=dst.范围(“K8”)值
尺寸eDate作为日期:eDate=dst.范围(“K9”)值
'声明变量。
变暗rng As范围“电流源范围
Dim sCell As Range“电流源范围的当前区域中的当前单元格
Dim i As Long“源行计数器
Dim j As Long“目标列计数器
Dim bCrit作为布尔标准验证器
Dim bStart作为布尔值的“开始日期验证器”
Dim bEnd作为布尔值“结束日期验证器”
Application.ScreenUpdating=False
'循环
对于i=2到srcLast
bCrit=(src.单元格(i,“F”).值=标准)
bStart=(src.Cells(i,“G”).Value>=sDate)

bEnd=(src.Cells(i,“H”).Value要理解您想要什么并不容易。我编写了以下代码,希望能有所帮助

Sub New_List()

Dim cond(1 To 3), created As Boolean
Dim db_first_row, db_last_row As Integer
Dim today, endate, name As Integer
Dim paste_row_start, paste_col_start As Integer
Dim data_sheet, target_sheet As String

'CONFIG (MODIFY THIS VARIABLES)

data_sheet = "worksheet2"    'sheet where the conditions and data are
target_sheet = "worksheet1"  'in what sheet do you want to paste?
db_first_row = 1             'in what row dose main table starts?
db_last_row = 10             'in what row dose main table ends?
paste_row_start = 1          'from what ROW would you like to start pasting?
paste_col_start = 2          'from what COL would you like to start pasting?

For db_first_row = 1 To db_last_row
    
    'check conditions
    With Sheets(data_sheet)
    today = .Range("L3")    'where is your condition "today"?
    endate = .Range("L4")   'where is your condition "endate"?
    name = .Range("L5")     'where is your condition "name"?

    cond(1) = .Cells(db_first_row, 7) >= today
    cond(2) = .Cells(db_first_row, 8) <= endate
    cond(3) = .Cells(db_first_row, 6) = name
    End With
    
    'if conditions are met, proceed
    If cond(1) = True And cond(2) = True And cond(3) = True Then
        
        'if you need to create a new worksheet
        'activate this CODE:
        
        'If created = False Then
            'Sheets.Add.name = "NAME OF SHEET"
            'created = True
        'End If
        
        With Sheets(data_sheet)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start) = _
        .Range("B" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 1) = _
        .Range("D" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 2) = _
        .Range("F" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 3) = _
        .Range("G" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 4) = _
        .Range("H" & db_first_row)
        End With
    
    paste_row_start = paste_row_start + 1
    
    End If
    
Next db_first_row

End Sub
Sub New_List()
Dim cond(1到3),创建为布尔值
Dim db_第一行,db_最后一行为整数
Dim today,endate,名称为整数
Dim粘贴\u行\u开始,粘贴\u列\u开始为整数
尺寸数据表,目标数据表为字符串
'配置(修改此变量)
数据表=“工作表2””包含条件和数据的表
target_sheet=“worksheet1””要粘贴到哪张表中?
db_first_row=1'主表在哪一行开始?
db_last_row=10'在主表的哪一行结束?
粘贴\行\开始=1'您希望从哪一行开始粘贴?
paste_col_start=2'要从哪个列开始粘贴?
对于db_first_row=1到db_last_row
"检查条件"
带图纸(数据表)
今天=.Range(“L3”)'您的“今天”状况如何?
endate=.Range(“L4”)'您的条件“endate”在哪里?
name=.Range(“L5”)'您的条件“name”在哪里?
cond(1)=.单元(第一行,7)>=今天

cond(2)=.Cells(db_first_row,8)很难理解您想要什么。我编写了以下代码,希望能有所帮助

Sub New_List()

Dim cond(1 To 3), created As Boolean
Dim db_first_row, db_last_row As Integer
Dim today, endate, name As Integer
Dim paste_row_start, paste_col_start As Integer
Dim data_sheet, target_sheet As String

'CONFIG (MODIFY THIS VARIABLES)

data_sheet = "worksheet2"    'sheet where the conditions and data are
target_sheet = "worksheet1"  'in what sheet do you want to paste?
db_first_row = 1             'in what row dose main table starts?
db_last_row = 10             'in what row dose main table ends?
paste_row_start = 1          'from what ROW would you like to start pasting?
paste_col_start = 2          'from what COL would you like to start pasting?

For db_first_row = 1 To db_last_row
    
    'check conditions
    With Sheets(data_sheet)
    today = .Range("L3")    'where is your condition "today"?
    endate = .Range("L4")   'where is your condition "endate"?
    name = .Range("L5")     'where is your condition "name"?

    cond(1) = .Cells(db_first_row, 7) >= today
    cond(2) = .Cells(db_first_row, 8) <= endate
    cond(3) = .Cells(db_first_row, 6) = name
    End With
    
    'if conditions are met, proceed
    If cond(1) = True And cond(2) = True And cond(3) = True Then
        
        'if you need to create a new worksheet
        'activate this CODE:
        
        'If created = False Then
            'Sheets.Add.name = "NAME OF SHEET"
            'created = True
        'End If
        
        With Sheets(data_sheet)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start) = _
        .Range("B" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 1) = _
        .Range("D" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 2) = _
        .Range("F" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 3) = _
        .Range("G" & db_first_row)
        Sheets(target_sheet).Cells(paste_row_start, paste_col_start + 4) = _
        .Range("H" & db_first_row)
        End With
    
    paste_row_start = paste_row_start + 1
    
    End If
    
Next db_first_row

End Sub
Sub New_List()
Dim cond(1到3),创建为布尔值
Dim db_第一行,db_最后一行为整数
Dim today,endate,名称为整数
Dim粘贴\u行\u开始,粘贴\u列\u开始为整数
尺寸数据表,目标数据表为字符串
'配置(修改