寻找将数据从一个工作簿复制到另一个工作簿的自定义解决方案

时间:2017-10-16 20:13:30

标签: excel vba excel-vba

我正在努力简化现场员工的工作流程。基线情况是,员工全年在单个Excel工作簿中填写每日详细报告,其中包括他们当天工作的时间,项目#和阶段代码,他们的人工成本将达到,以及他们当天工作的小时数特别的项目。一天中相同的项目编号和阶段代码被多次使用是很常见的(即多个行条目需要根据具有相同项目编号和相位代码的条件一起添加的总小时数。天)。参见附件"详细报告WB"图像。

然后,我们的员工必须在单独的工作簿中以不同的格式输入相同的数据(即每个项目编号和相位代码对每天只允许一行)。参见附件"时间表导入WB"图片。时间表工作簿基于该周(即星期日)的周结束日期来驱动。然而,每日详细报告工作簿是基于周开始日期驱动的,周日不包括在每日详细报告中。为了使事情变得更复杂,“每日详细信息”报告中列出的日期基于公式,并且不在单元格中保留实际日期值。

此解决方案的目标是将输入到每日详细报告工作簿中的信息通过基于ActiveX命令按钮单击事件的子功能放入Timesheet工作簿。请参阅随附的“最终目标”图片。

到目前为止,我编译了以下代码,允许用户单击导入按钮,提示用户找到他们要从中导入数据的每日详细报告工作簿。此代码还允许用户输入Timesheet工作簿所需的周末日期。我试图使用周结束日期在每日详细报告工作簿中查找所需数据,但这证明很难。任何帮助将不胜感激。自从我在大学回到VBA进行计算机编程以来已经有好几年了。

图片位于此处:https://drive.google.com/drive/folders/0B7BjXxM59FFyQlM5eThvc0dDWUU?usp=sharing

谢谢!

Private Sub CommandButton1_Click()
'Define All Variables
Dim GCell As Range
Dim fDialog As FileDialog, result As Integer
Dim MyDetailReport As String
Dim MyTimeSheet As String
Dim MySheet As String
Dim ProjNum As String
Dim PhaseCode As String
Dim Hours As String
Dim WkEndDate As String

'Find source file
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fDialog.AllowMultiSelect = False
    fDialog.Title = "Select Daily Report File to Import"
    fDialog.InitialFileName = "W:\PDX - Mechanical Construction\Operations\Detailing\Daily Tracking Logs\"
    fDialog.Filters.Clear
    fDialog.Filters.Add "Excel files", "*.xlsx"
    If fDialog.Show = -1 Then
        MyDetailReport = fDialog.SelectedItems(1)
    End If

'Enter date to look for in workbook
  WkEndDate = InputBox("Insert Week End Date in format mm/dd/yyyy", "User date", Format(Now(), "mm/dd/yyyy"))
  If IsDate(WkEndDate) Then
    WkEndDate = Format(CDate(WkEndDate), "mm/dd/yyyy")
    'Place Week End Date into Timesheet workbook
    With ThisWorkbook.ActiveSheet.Range("AE5")
        .Value = WkEndDate
    End With
  Else
    MsgBox "Wrong date format"
  End If

'Use the current sheet to store found data
MySheet = ActiveSheet.Name

'Use Error Handling routine in case of errors
On Error GoTo ErrorHandler

'Turn off screen updating to run macro faster
Application.ScreenUpdating = False
Workbooks.Open Filename:=MyDetailReport & MyTimeSheet

'Search Detail Report "Daily Report Log" sheet for the selected Week End Date
Set GCell = ActiveSheet.Cells.Find(WkEndDate, LookIn:=xlValues)
GCell = GCell.Offset(2, 1)
    If GCell.Value = "" Then
        GCell = GCell.Offset(1, 0)
    Else
        'copy data formulas to correct format for Timesheet workbook
    End If

'Error Handling
ErrorHandler:
Select Case Err.Number

        'Common error #2: the specified data wasn't in the target workbook.
        Case 9, 91
            Application.ScreenUpdating = True
            MsgBox "The value " & WkEndDate & " was not found."
            Exit Sub
        'General case: turn screenupdating back on, and exit.
        Case Else
            Application.ScreenUpdating = True
        Exit Sub
End Select

End Sub

0 个答案:

没有答案