【问题标题】:Copy rows from one workbook to another if condition is fulfilled如果满足条件,将行从一个工作簿复制到另一个工作簿
【发布时间】:2020-05-14 21:41:26
【问题描述】:

如果满足以下条件,我如何将行从一个工作簿复制/粘贴到另一个工作簿: 如果源工作簿(wb.Source,sheet1)中的所有行来自实际月份(包含真实日期的第 8 列),则复制它们并将它们粘贴到工作表 3 中的我的主工作簿(Wb)。仅复制具有第一个的行在第 8 列中作为日期的月份日期。

示例:

假设今天是 14.05.2020。

触发宏将从源工作簿中复制日期为 01.05.2020(第 8 列)的所有行,并将它们粘贴到工作表 3 中的 wb。

因此,宏需要参考 Today 函数才能说出现在实际上是哪个月份,然后在下一步中复制同一月份的行,但仅从该月份的第一天开始.

不胜感激!

Private Sub CommandButton3_Click()





Dim fname As String, wbSource As Workbook, wsSource As Worksheet
    fname = Me.TextBox1.Text

    If Len(fname) = 0 Then
       MsgBox "No file selected", vbCritical, "Error"
       Exit Sub
    End If

    Set wbSource = Workbooks.Open(fname, False, True) ' no link update, read only
    Set wsSource = wbSource.Sheets("Sheet1") ' change to suit

    Dim wb As Workbook, ws As Worksheet
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Table 3")



    ' Set your source and destination worksheets as objects here

    Set wsSource = wbSource.Sheets("Sheet1")


    Dim i As Long, destination_row As Long
    Dim source_rng As Range, destination_rng As Range
    destination_row = 1

    For i = 1 To 10 ' See note below regarding for vs while loop for this
        ' Check dates only against cells that contain date values (in case there is a non-date value in one of the cells)
        If VarType(wsSource.Cells(i, 8)) = vbDate Then

            ' Condition checks that the date is today
            If Format(Now, "yyyy/mm/dd") = Format(wsSource.Cells(i, 8).Value2, "yyyy/mm/dd") Then

                ' Set source and destination ranges
                Set source_rng = wsSource.Range(wsSource.Cells(i, 1), wsSource.Cells(i, 10000))
                Set destination_rng = ws.Range(ws.Cells(destination_row, 1), destination_sheet.Cells(destination_row, 10000))

                source_rng.Copy destination_rng ' Alternatively use destination_rng.Value2 = source_rng.Value2
                destination_row = destination_row + 1 ' Iterate the destination row so that the next copy outputs to the next row

            End If
        End If
    Next i



     ' close source worbook no save
    wbSource.Close False


End Sub

【问题讨论】:

  • 总是有助于发布您当前的代码 - 这将使回答变得更加容易,否则我们可以将您推荐给您已经在这里得到的答案:stackoverflow.com/questions/61800272/…
  • Dateserial(year(date),month(date),1) 为您提供当月的第一天

标签: excel vba


【解决方案1】:

问题对于工作表的格式有点模糊(哪些列包含数据,您要从哪里开始打印,如果您知道源数据有多少行,是否填充了数据范围内的所有单元格,等),但我在这里编写了一个示例,可以对其进行调整以适合您正在使用的工作表。

Option Explicit

Sub CopyRows()

    ' Set your source and destination worksheets as objects here
    Dim source_sheet As Worksheet, destination_sheet As Worksheet
    Set source_sheet = ActiveWorkbook.Sheets("A")
    Set destination_sheet = ActiveWorkbook.Sheets("B")

    Dim i As Long, destination_row As Long
    Dim source_rng As Range, destination_rng As Range
    destination_row = 1

    For i = 1 To 10 ' See note below regarding for vs while loop for this
        ' Check dates only against cells that contain date values (in case there is a non-date value in one of the cells)
        If VarType(source_sheet.Cells(i, 8)) = vbDate Then

            ' Condition checks that the date is today
            If Format(Now, "yyyy/mm/dd") = Format(source_sheet.Cells(i, 8).Value2, "yyyy/mm/dd") Then

                ' Set source and destination ranges
                Set source_rng = source_sheet.Range(source_sheet.Cells(i, 1), source_sheet.Cells(i, 10))
                Set destination_rng = destination_sheet.Range(destination_sheet.Cells(destination_row, 1), destination_sheet.Cells(destination_row, 10))
                source_rng.Copy destination_rng ' Alternatively use destination_rng.Value2 = source_rng.Value2
                destination_row = destination_row + 1 ' Iterate the destination row so that the next copy outputs to the next row

            End If
        End If
    Next i

End Sub

我使用工作表“A”和“B”作为源工作表和目标工作表的代理。您需要在代码中将这些对象设置为您的实际源工作表和目标工作表。

我还将destination_row 初始化为1,假设您希望从目标工作表的第一行开始打印复制的行,但这可以设置为2(如果有标题)或您喜欢的任何行输出开始。如果您希望在工作表中已存在的任何数据下方添加新行,您可能需要添加额外的代码来查找下一个空行。

for 循环是作为一个基本示例编写的,假设您知道有多少行数据。如果您希望源范围的大小发生变化(即经常添加和/或删除行),您可能希望在开始循环之前确定最后一行(通过使用 xlUp/xlDown 或类似方法),或者使用一段时间循环而不是 for 循环(如果有一个始终填充的列,您可以将其用作 while 条件的一部分)。

在设置source_rng和destination_rng的部分,将数字1和10替换为数据的第一列和最后一列(例如,如果数据从第3列开始,到第15列结束,则应更改这些分别为 3 和 15)。

【讨论】:

  • 谢谢!我已经调整了您的代码以适合我的工作表。经过几个错误后,我让它工作了。唯一的问题是代码不返回任何内容。知道为什么吗?请参阅初始帖子中的代码。
  • 嗨 - 不太确定在这种情况下您所说的“返回”是什么意思?另请注意我所说的带有 for 循环和范围选择列的行 - 这些将需要根据您的数据特征进行调整。
  • 对不起,我的意思是没有行被复制和粘贴。我的目标表是空的。我甚至将日期格式调整为 dd.mm.yyyy 以适合我的工作表,但仍然没有结果我的源工作表有超过 14k 行,这就是为什么我将范围调整为 1 - 15000
  • 日期格式无关紧要,因为它将两个日期转换为相同的格式 - 确切的格式无关紧要,因为它只是为了比较,只要它们都被转换为相同的格式。 Excel 将日期存储为一个数值,因此无论您如何呈现它们,工作表上的基本日期值都是相同的。
  • 我认为您可能调整了错误的数字。应该根据数据的行数调整 for 循环(我仍然看到它读取“for i = 1 to 10”),并且应该根据 COLUMNS 的数量调整目标范围设置(您已调整为 10000 ,这似乎不正确)。
猜你喜欢
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2019-07-29
  • 1970-01-01
相关资源
最近更新 更多