1

閉じたブックから特定のシート、開いたブックに特定のシートをコピーしようとしていますが、実行時エラーが発生します。

コードは以下のとおりです。

Option Explicit

Sub START()


'Defining variables as a worksheet or workbook
Dim shBrandPivot As Worksheet
Dim shCurrentWeek As Worksheet
Dim shPriorWeek As Worksheet
Dim shPivot As Worksheet
Dim wkb As Workbook
Dim wkbFrom As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim path As String, FilePart As String
Dim TheFile
Dim loc As String

'Setting sheets to active or to recognize where to pull information
Set shBrandPivot = ActiveWorkbook.Sheets("Brand Pivot")
Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week")
Set shPriorWeek = ActiveWorkbook.Sheets("Prior Week")
Set shPivot = ActiveWorkbook.Sheets("Pivot")
Set wkb = ThisWorkbook
loc = shPivot.Range("E11").Value
path = shPivot.Range("E12").Value
FilePart = Trim(shPivot.Range("E13").Value)
TheFile = Dir(path & "*" & FilePart & ".xls")
Set wkbFrom = Workbooks.Open(loc & path & TheFile)
Set wks = wkbFrom.Sheets("SUPPLIER_01_00028257_KIK CUSTOM")
Set rng = wks.Range("A2:N500")

'Copy and pastes This week number to last week number
shPivot.Range("E22:E23").Copy shPivot.Range("F22")

'Deletes necessary rows in PriorWeek tab
shPriorWeek.Range("A4:AC1000").Delete

'Copies necessary rows in CurrentWeek tab to PriorWeek tab
shCurrentWeek.Range("A4:AC1000").Copy shPriorWeek.Range("A4")

'Copies range from report generated to share drive and pastes into the current week tab of open order report
rng.Copy wkb.Sheets("Current Week").Range("A4")

'Closes the workbook in the shared drive
wkbFrom.Close False

End Sub

これは、それぞれセルE11、E12、およびE13にあるものです。

E11-S:_Supply Chain \ Weekly Rpts Supplier and Buyer \ E12-102112 E13-SUPPLIER_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls

閉じたワークブックを開きたいディレクトリは、S:_Supply Chain \ Weekly Rpts Supplier and Buyer \ 102112 \ Supplier_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls

4

2 に答える 2

2

これを読むことをお勧めします

原因

この問題は、ブックに定義された名前を付けてから、最初にブックを保存して閉じずにワークシートを数回コピーした場合に発生する可能性があります。

解決する方法

この問題を解決するには、コピープロセスの実行中に、ブックを定期的に保存して閉じます。

チャットでのディスカッションの概要について

にブレークポイントを設定するSet wkbFrom = Workbooks.Open(loc & path & TheFile)

ファイル名が欠落しているため、ファイル名Dir(path & "*" & FilePart & ".xls") のないディレクトリが返されることがわかりました。

したがって、解決策は、セルE13にあるファイル名を追加することです。

于 2012-11-05T18:34:12.583 に答える
1

私は以前にあなたに役立つかもしれない同様のことをしました。鉱山は範囲をコピーするだけですが、範囲のサイズをコピーしたいシートの使用済みスペースの量に簡単に変更できます。

Dim source as Workbook
Dim dest As Workbook
Dim originalWorkBook As Workbook
Set originalWorkBook = ThisWorkbook
Dim ws As Worksheet
Dim copyRange As String
Dim myDir As String
'myDir is found via a function I wrote. The user enters a name, month and year and
'from there it knows where the file will be

copyRange = "A1:D80"

source.Worksheets("The name of your worksheet here").Range(copyRange).Copy
originalWorkBook.Activate
originalWorkBook.Worksheets("Sheet1").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
source.Close False
Application.ScreenUpdating = True

お役に立てれば。

于 2012-11-06T06:05:25.380 に答える