这是我第一次在这里发帖,所以我为我犯的任何错误道歉。正如您将看到的,我对VBA缺乏经验。 我的工作是将现有宏从一台机器复制到另一台机器。因此,我将整个模块复制到一个文档中,然后在新机器上打开,将模块添加到个人宏工作簿中。我从代码中改变的只是路径,因为在新机器上显然改变了目的地。我从文件资源管理器中复制了文件位置。当我点击运行时,我收到以下错误:
运行时错误'1004': Microsoft Excel无法访问该文件 'C:\ Users \ bookk \ Onedrive \ Documents \ Financials \ Payroll Reports \ 2017-2018 2017-2018 \ Payroll 17-18 \ Payroll Reports 17-18 \ 51A35100'
直接从文件资源管理器复制粗体部分之前的路径。我不知道大胆部分的来源或原因。我的猜测是下面的“if,else,end”语句。转到该文件位置会提示“不存在”消息,因为实际文件位置在粗体部分之前结束。
点击“debug”会以黄色显示以下行。宏比下面的长,但我不希望它比它需要的更长(我希望)。 我查看了本网站上的其他几篇文章以及其他选项,但尚未找到解决方案。这可能已经回答了,我只是不知道我在找什么。
任何帮助都将不胜感激。
Sub CreatePayrollReport()
Dim strAnswer As String, intFlag As Integer, strPath As String, intWSWCol As Integer, intWSWRow As Integer
Dim intWSRCol As Integer, intWSRRow As Integer, intWSWLRow, intNetPayRow, I As Integer
Dim intWSWSCol As Integer, intWSNPRow, strYearR As String, strYearER As String
Dim WBWage, WBRep, WSWage, WSRep, WSNetPay
' Set up Parameters
'intWSWCol = 7 ' 2015.9.15 - old column when QuickBook reports were narrower
intWSWCol = 9 ' Wage data working column, "Pay" for each person
intWSWRow = 1
intWSRCol = 1
intWSWSCol = 4 ' Wage data gross pay line item description column
intWSRRow = 5
intWSNPRow = 5
' Set up Path for Workbooks
strPath = "C:\Users\bookk\Onedrive\Documents\Financials\Payroll Reports\2017-2018"
The old strPath was "C:\Users\Protected - Bookkeepe\Documents\Bookkeeper\Bookkeeper\2017-2018"
If Now() > DateSerial(Year(Now()), 9, 5) And Now() <= DateSerial(Year(Now()), 12, 31) Then
strYearR = Right(Year(Now()), 2) & "-" & Right(Year(Now()) + 1, 2)
strYearER = Year(Now()) & " - " & Year(Now()) + 1
Else
strYearR = Right(Year(Now()) - 1, 2) & "-" & Right(Year(Now()), 2)
strYearER = Year(Now()) - 1 & " - " & Year(Now())
End If
strPath = strPath & strYearER & "\Payroll " & strYearR & "\Payroll Reports " & strYearR & "\"
' Get name of workbook containing wage data
Do
strAnswer = InputBox("Please input the name of the workbook that contains the Wage data")
intFlag = MsgBox("Press OK if this is the correct name - " & strAnswer, vbOKCancel)
Loop Until intFlag = 1
Workbooks(strAnswer).Activate
Set WBWage = ActiveWorkbook
Set WSWage = Worksheets("Sheet1")
' Get last row of wage data to check
Columns("B:B").Select
intWSWLRow = Selection.Find(What:="Adjusted", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
' Get Net Pay Row
Columns("A:A").Select
intNetPayRow = Selection.Find(What:="Net Pay", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
' Get Pay Period Ending Date
Do
strAnswer = InputBox("Please the title for this Payroll Report (PP End)")
intFlag = MsgBox("Press OK if this is the correct name - " & strAnswer, vbOKCancel)
Loop Until intFlag = 1
Workbooks.Add
> ActiveWorkbook.SaveAs Filename:=
strPath & "Pay Period End " & strAnswer & ".xlsx"
Set WBRep = ActiveWorkbook
ActiveSheet.Name = strAnswer
Set WSRep = ActiveSheet
Set WSNetPay = Worksheets("Sheet2")
Worksheets("Sheet2").Name = "Net Pays"