遍历Excel工作表中的列,直到找到要粘贴值的日期匹配项

时间:2019-05-02 03:07:41

标签: excel vba

我目前正在尝试使工作同事的每日报告工作簿自动化。

我最终从公司的数据库中下载了数据,并将其转换为Excel Workbook(1)。

然后,我在单独的每日报告工作簿(2)中有一个宏。我使用For ... Next循环遍历各列以查找空白,然后将我复制的值粘贴到“天”列之一(例如星期一)

我想找到一种更好的方法来找到合适的星期几来粘贴我的值。根据我从公司复制的数据,我想到了将日期作为参考,以在“每日报告工作簿”中找到合适的日子。

Dim x As Workbook, y As Workbook
OFile = "Automation_Example_Tanya.xlsm"
MsgBox "Choose the file for this day's report"
FileName = Application.GetOpenFilename
Do While FileName = False
    MsgBox ("Choose this week's report")
    FileName = Application.GetOpenFilename
Loop
Set x = Workbooks.Open(FileName)
Windows(OFile).Activate
Sheets(1).Activate
Cells.Select
Selection.Clear

    x.Activate
    x.Sheets(1).Cells.Select
    Application.CutCopyMode = False
    Selection.Copy

    Windows(OFile).Activate
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

    x.Close SaveChanges:=False

End Sub
Sub Auto_Update_Report()
    Dim NCR As Variant
    Dim x As Variant
    Application.ScreenUpdating = False
    Call OpenFilePaste
    Sheets(2).Activate
    Range("I2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    Range("B2").Activate
    NCR = Range("C2:G2").Cells.Count
    For x = 1 To NCR
    ActiveCell.Offset(0, 1).Select
    If IsEmpty(ActiveCell) Then
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
           :=False, Transpose:=False
           Range("A1").Activate
           Exit Sub
            Else
                If Range("G2") <> "" Then
                Range("C:F").ClearContents
                Range("A1").Activate
                Call Auto_Paste
                Exit Sub
            End If
    End If
Next x
End Sub ```

0 个答案:

没有答案