我遇到的问题是我将一个已知范围从一个工作簿复制到另一个工作簿,但是在最后一行之后 - 但作为一个偏移量(3,0)。这是整个代码,你真的可以忽略它的前3/4 ......并从我设置范围的地方开始。我认为设置范围并从那里做一行偏移,但显然我没有正确地做到这一点。
这条线给我一个错误:设置rngFinalRange ...我知道这是错的,但我被卡住了。思考?谢谢!
Option Explicit
Sub TEST()
Dim auditfolder As String
Dim dumpfile As String
Dim FSO As Object
Dim fldstart As Object
Dim wbkAudit As Workbook
Dim wbkdump As Workbook
Dim rngDumpCols As Range
Dim rngDumpFullRange As Range
Dim strAuditName As String
Dim strDumpName As String
Dim fl As Object
Dim rngAuditFileRows As Range
Dim rngAuditCols As Range
Dim rngFinalRange As Range
Dim rngauditrows As Range
Dim lastrow as range
Set FSO = CreateObject("scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "\\networkpath"
.AllowMultiSelect = False
If .Show <> -1 Then
MsgBox "No file selected. Exiting."
Exit Sub
Else
auditfolder = .SelectedItems(1)
Set fldstart = FSO.getfolder(auditfolder)
End If
End With
For Each fl In fldstart.Files
If Right(fl.name, 3) = "xls" Then
If InStr(fl.name, "5ESS") Then
strAuditName = fl.name
ElseIf InStr(fl.name, "SelectDataDump") Then
strDumpName = fl.name
Else
MsgBox "Missing either the audit or selectdatadump file"
End If
ElseIf Right(fl.name, 4) = "xlsx" Then
If InStr(fl.name, "5ESS") Then
strAuditName = fl.name
ElseIf InStr(fl.name, "SelectDataDump") Then
strDumpName = fl.name
Else
MsgBox "Missing either the audit or selectdatadump file"
End If
End If
Next fl
Application.ScreenUpdating = False
Set wbkdump = Workbooks.Open(strDumpName)
Set rngDumpCols = wbkdump.Sheets(1).Cells.Find(what:="*", After:=wbkdump.Sheets(1).Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Set rngDumpFullRange = wbkdump.Sheets(1).Range("A1", rngDumpCols.Address)
Set wbkAudit = Workbooks.Open(strAuditName)
Set rngAuditCols = wbkAudit.Sheets(1).Cells.Find(what:="*", After:=wbkAudit.Sheets(1).Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Set rngauditrows = wbkAudit.Sheets(1).Range("A1", rngAuditCols.Offset(0, -4).Address)
lastrow = wbkAudit.Sheets(1).Cells(65536, rngAuditCols.Column).End(xlUp).Row
Set rngFinalRange = wbkAudit.Sheets(1).Range(lastrow.Offset(3, 0).Row)
rngFinalRange.Value = rngDumpFullRange.Value
wbkAudit.Sheets(1).Columns.AutoFit
wbkAudit.save
MsgBox "Processing Complete!"
End Sub
答案 0 :(得分:1)
lastrow
在您的代码中声明为Range
对象,但您尝试将其设置为Long
值:
http://msdn.microsoft.com/en-us/library/office/ff196952.aspx
尝试更改此内容......
lastrow = wbkAudit.Sheets(1).Cells(65536, rngAuditCols.Column).End(xlUp).Row
Set rngFinalRange = wbkAudit.Sheets(1).Range(lastrow.Offset(3, 0).Row)
......对此...
Set lastrow = wbkAudit.Sheets(1).Cells(65536, rngAuditCols.Column).End(xlUp)
Set rngFinalRange = wbkAudit.Sheets(1).Range(lastrow.Offset(3, 0).Address)