简体   繁体   English

VBA将相应的数据复制并粘贴到新工作表上

[英]VBA copy and paste corresponding data onto new sheet

I am trying to use VBA to ask the user to enter a date. 我正在尝试使用VBA要求用户输入日期。 For that date, copy all appointments and paste them on sheet “Daily Appts”, one-by-one. 在该日期,复制所有约会并将其逐一粘贴到“ Daily Appts”表中。 My reference sheet that contains all the data corresponding to given dates is named "Appts". 我的参考表包含了与给定日期相对应的所有数据,被称为“ Appts”。 I attached a picture for reference. 我附上一张图片供参考。 Leading up to this, I created worksheet "Daily Sheet" and copy and pasted the headers from "Appts" onto it. 为此,我创建了工作表“ Daily Sheet”,并将标题从“ Appts”复制并粘贴到其上。 I am trying to get every value for the date entered to copy and paste onto the new sheet but I am stuck. 我正在尝试获取输入的日期的每个值,以将其复制并粘贴到新表上,但是我被卡住了。 For example, if user enters 10/01/2018, it will have multiple sets of data that needs to be copied over. 例如,如果用户输入10/01/2018,它将有多组数据需要复制。 Here is what I have so far. 这是我到目前为止所拥有的。 Step 6 is where I need help to complete the task. 第6步是我需要帮助才能完成任务的地方。 1 : https://i.stack.imgur.com/vEtUd.png 1https//i.stack.imgur.com/vEtUd.png

'Step 1:
Sub Part2()
Dim sheet As Variant
'Step 2: Add code to delete sheet "Daily Appts", if exist.
    For Each sheet In ActiveWorkbook.Worksheets
        If sheet.Name = "Daily Appts" Then
            Application.DisplayAlerts = False
            Worksheets("Daily Appts").Delete
            Application.DisplayAlerts = True
        End If
    Next sheet 
'Step 3: Add code to add a new sheet, name it "Daily Appts"
    Sheets("Main").Select
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Select
    ActiveSheet.Name = "Daily Appts"
    Sheets("main").Select
'Step 4: Add code to select the worksheet "Appts". Name the range that holds             
'the title (first row), the range that contains the data, and the range 
'contains the schedule.
Sheets("Appts").Select
Dim Title As Range, Data As Range, Schedule As Range
    Set Title = Range("A1", Range("A1").End(xlToRight))
    Title.Name = "Title"

    Set Data = Range("A2", Range("A2").End(xlDown).End(xlToRight))
    Data.Name = "Data"

    Set Schedule = Range("J2", Range("J2").End(xlDown))
    Schedule.Name = "Schedule"


'Step 5: Add code to copy and paste the title into the new sheet, "Daily         
'Appts".
    Sheets("Appts").Range("Title").Copy 'Copy the data
    Sheets("Daily Appts").Activate 'Activate the destination worksheet
    Range("A1").Select 'Select the target range
    ActiveSheet.Paste 'Paste in the target destination

    Application.CutCopyMode = False
'Step 6: Ask the user to enter a date.  For that date, copy all appointments     
'and paste them on sheet "Daily Appts", one-by-one.
Dim result As String, i As Long, mydate As Date
Sheets("Appts").Select
    result = InputBox("Enter a date")

    For i = 2 To 360
        mydate = Cells(i, 10)
        If mydate = result Then
        Sheets("Appts").Range("J2").End(xlToLeft).Copy
        Sheets("Daily Appts").Activate
        Range("A2").End(xlDown).Select
        ActiveSheet.Paste
        End If
    Next
End Sub

There is not reason to loop to test whether a single sheet exists a simple test if a cell reference is valid will do it: 没有理由循环测试单个工作表是否存在,如果单元格引用有效,则进行简单测试即可:

If Not IsError(Application.Evaluate("'Daily Appts'!A1")) Then Worksheets("Daily Appts").Delete

Also by declaring a worksheet variable on the addition it make it easier to work with the sheet later: 同样,通过在添加项上声明一个工作表变量,它也使得以后处理工作表更容易:

Dim ws As Worksheet
Set ws = Worksheets.Add(After:=Worksheets("Main"))
ws.Name = "Daily Appts"

Then there is no need for the range.name as you created the ranges as variables just refer to them. 然后就不需要range.name因为您创建的范围只是作为变量的引用。

Then in the loop you need to iterate the copy ranges. 然后在循环中,您需要迭代复制范围。

I also cleaned up the .Activate and .Select which should be avoided. 我还清理了应该避免的.Activate.Select

Sub Part2()

'Step 2: Add code to delete sheet "Daily Appts", if exist.
    If Not IsError(Application.Evaluate("'Daily Appts'!A1")) Then Worksheets("Daily Appts").Delete
'Step 3: Add code to add a new sheet, name it "Daily Appts"
    Dim ws As Worksheet
    Set ws = Worksheets.Add(After:=Worksheets("Main"))
    ws.Name = "Daily Appts"
'Step 4: Add code to select the worksheet "Appts". Name the range that holds
'the title (first row), the range that contains the data, and the range
'contains the schedule.

    With Worksheets("Appt")
        Dim lCol As Long
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Dim lRow As Long
        lRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        Dim Title As Range
        Set Title = .Range(.Cells(1, 1), .Cells(1, lCol))

'Step 5: Add code to copy and paste the title into the new sheet, "Daily
'Appts".
        Title.Copy ws.Range("A1") 'Paste in the target destination

'Step 6: Ask the user to enter a date.  For that date, copy all appointments
'and paste them on sheet "Daily Appts", one-by-one.
        Do
            Dim result As String
            result = InputBox("Enter a date")
            If Not IsDate(result) Then MsgBox ("must be date")
        Loop Until IsDate(result)

        For i = 2 To lRow
            If .Cells(i, 10).Value2 = CDate(result) Then
                .Range(.Cells(i, 1), .Cells(i, lCol)).Copy ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1)
            End If
        Next
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM