尝试使用Rows.Count
时,我一直收到运行时错误1004。它通常在我第一次运行下面的代码时发生,但是如果我重置并再次运行它就可以工作。
这条线路失败了:
LastRow = Cells(Rows.Count, 4).End(xlUp).Row
任何有助于让这些代码可靠运行的帮助都将非常感谢!
整个代码如下:
Private Sub ImportAPRData_Click()
'Declare variables for columns in "Projects" spreadsheet in Approved Reliability Projects Workbook (Excel)
Dim orgSheetCol(13) As String
orgSheetCol(0) = "$E$" 'Project Title
orgSheetCol(1) = "$D$" 'Circuit Tag
orgSheetCol(2) = "$F$" 'District
orgSheetCol(3) = "$G$" 'State
orgSheetCol(4) = "$M$" 'Date recieved
orgSheetCol(5) = "$J$" 'Planned Capital Cost
orgSheetCol(6) = "$X$" 'Actual Capital Cost
orgSheetCol(7) = "$U$" 'Capital work completed date
orgSheetCol(8) = "$K$" 'Planned O&M Cost
orgSheetCol(9) = "$Y$" 'Actual O&M Cost
orgSheetCol(10) = "$V$" 'O&M work completed date
orgSheetCol(11) = "$AD$" 'Path to RWP file
orgSheetCol(12) = "I" 'Investment Reason
'Declare variables for cell values attained from APR spreadsheet
Dim orgSheetvalues(13) As Variant
orgSheetvalues(0) = "" 'Project Title
orgSheetvalues(1) = "" 'Circuit Tag
orgSheetvalues(2) = "" 'District
orgSheetvalues(3) = "" 'State
orgSheetvalues(4) = "" 'Date recieved
orgSheetvalues(5) = "" 'Planned Capital Cost
orgSheetvalues(6) = "" 'Actual Capital Cost
orgSheetvalues(7) = "" 'Capital work completed date
orgSheetvalues(8) = "" 'Planned O&M Cost
orgSheetvalues(9) = "" 'Actual O&M Cost
orgSheetvalues(10) = "" 'O&M work completed date
orgSheetvalues(11) = "" 'RWP File Path
orgSheetvalues(12) = "" 'Investment Reason
'Declare & Set Variables for opening & working with Excel Wrokbook / worksheet (Approved Relaibility Projects/Projects)
Dim xls As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set xls = New Excel.Application
'Delcare & set variables for loops, excel row, cell numbers, etc.
Dim rownumber As Integer
rownumber = 3
Dim rowstring As String
Dim cellstring As String
Dim i As Integer
'Declare & set variable to see if RWP already exists in table
Dim tablecheck As Integer
tablecheck = 0
'Declare variable for Capital and O&M Costs / completion dates conditions
Dim Condition1 As Boolean
Dim Condition2 As Boolean
Dim Condition3 As Boolean
Dim Condition4 As Boolean
Dim Condition5 As Boolean
Dim Condition6 As Boolean
Dim Condition7 As Boolean
Dim Condition8 As Boolean
Dim LastRow As Integer
'Open Approved Reliability Projects Workbook & set worksheet to "Projects"
xls.Visible = True
xls.UserControl = True
Set wkb = xls.Workbooks.Open("\\pacificorp.us\dfs\SLCCO\SHR02\PD\POWER\AreaSystemFiles\UT\Park_City_Office\Reliability\RWP_Goal_Tracking\Approved Reliability Projects v5.xlsm", ReadOnly:=True, UpdateLinks:=False)
Set wks = wkb.Worksheets("Projects")
'Find row # for last populated row
LastRow = Cells(Rows.Count, 4).End(xlUp).Row 'For some reason it keeps giving me an error here!!!!
'For each row in APR spreadsheet get info, then make sure all criteria are met, then check to see if it already exists in table, if not insert into table
For rownumber = 3 To LastRow
rowstring = CStr(rownumber)
'Pull information from specified row in APR Spreadsheet
For i = 0 To 12
cellstring = orgSheetCol(i) & rowstring
orgSheetvalues(i) = wks.Range(cellstring).Value
If IsError(orgSheetvalues(i)) Then
orgSheetvalues(i) = wks.Range(cellstring).Text
End If
Next i
'Check to make sure that there are planned costs and completion dates before inserting into rwpT Table
Condition1 = orgSheetvalues(5) <> "" And (orgSheetvalues(7) <> "" And orgSheetvalues(7) <> "#") And orgSheetvalues(11) Like "\\*"
Condition2 = orgSheetvalues(5) = "" And orgSheetvalues(7) = "" And orgSheetvalues(11) Like "\\*"
Condition3 = orgSheetvalues(8) <> "" And orgSheetvalues(10) <> "" And orgSheetvalues(10) <> "N/A"
Condition4 = orgSheetvalues(8) = "" And orgSheetvalues(10) = ""
Condition5 = Condition1 And Condition3
Condition6 = Condition1 And Condition4
Condition7 = Condition1 And Condition3
Condition8 = (Condition5 Or Condition6) Or Condition7
If Condition8 Then
tablecheck = DCount("PlanTitle", "rwpT", "PlanTitle = '" & orgSheetvalues(0) & "'") 'check rwp table to see if plan is already there
'If plan is not there insert into rwpT Table
If tablecheck = 0 Then
CurrentDb.Execute "INSERT INTO rwpT (PlanTitle, Circuit, OpArea, State, InvestmentReason, ApprovalDate, PlanCapitalCost, ActualCapitalCost, CapitalWorkCompDate, PlanOMCost, ActualOMCost, OMWorkCompDate, File) Values ('" & orgSheetvalues(0) & "', '" & orgSheetvalues(1) & "', '" & orgSheetvalues(2) & "', '" & orgSheetvalues(3) & "','" & orgSheetvalues(12) & "', '" & orgSheetvalues(4) & "', '" & orgSheetvalues(5) & "', '" & orgSheetvalues(6) & "', '" & orgSheetvalues(7) & "', '" & orgSheetvalues(8) & "', '" & orgSheetvalues(9) & "', '" & orgSheetvalues(10) & "','" & orgSheetvalues(11) & "')"
End If
End If
Next rownumber
'Close Approved Reliability Projects Workbook & remove all handles to it
wkb.Close False 'Close workbook. False is so that it doesn't save
Set wks = Nothing
Set wkb = Nothing
xls.Quit
Set xls = Nothing
End Sub
答案 0 :(得分:0)
将Dim LastRow As Integer
更改为Dim LastRow As Long
,它会没问题,有太多行可以作为整数。
在VBA中,最好总是使用Long而不是Integer,因为它们都存储为long,然后在运行时转换整数,在这里搜索更多信息。
答案 1 :(得分:0)
MS Access没有默认的Rows
属性(如果属性,则不是您想要使用的属性)。您希望在其Excel意义上使用Rows
,如果在Excel应用程序中运行,则默认为Application.ActiveWorkbook.ActiveSheet.Rows
。
由于Access不知道Rows
的含义,因此它使用Excel Application对象的默认实例中的属性(与您的xls
对象不同)。默认实例中没有打开工作簿或工作表,因此无法确定Application.ActiveWorkbook.ActiveSheet.Rows
(或Application.ActiveWorkbook.ActiveSheet.Cells
)的含义。
更改行
LastRow = Cells(Rows.Count, 4).End(xlUp).Row
以便您完全符合您的方法/属性,即
LastRow = wks.Cells(wks.Rows.Count, 4).End(xlUp).Row