私は、Excel 2010でVBAを使って簡単な作業をしようとしていますが、私は専門家が助けてくれることを願っています。私はVBAを初め、正式なトレーニングを受けていないので、おそらく私はどこかでばかげたミスを犯しているようです。ブック(Source.xlsx)を開き、そのブック(Sheet1!A1:E1)からデータを取り出し、現在のワークブック(Target.xlsm)Sheet1にコピーします。A5:A9。私はソースとターゲットのワークブックを作成してみましたが、これを試してみることはできますが、実際の「ターゲット」ワークブックは複数のワークブックからデータを取得します。私は関数にファイル名を渡し、 "Source.xlsx"のような名前でハードコーディングするのではなく、そのファイル名を使うことを意図していました。これは、これまで私が持っているコードです:ワークブックを開いてデータを転記してワークブックを閉じるWorkbooks.Open does does not work work
Public Function TranscribeFromClosedWb(SourcePath As String, SourceWb As String, SourceWs As String)
0 'Exits Function if an Error is thrown
1 On Error GoTo ErrHandler
2
3 'Stops Excel from updating the screen so that the Function runs faster and is more aesthetic to the User
4 'Application.ScreenUpdating = False
5
6 'Dimensionalizes the Arrays and counting variables to be used in the function
7 Dim SourceCell(1 To 5) As String
8 Dim cellData(1 To 5) As Variant
9 Dim TargetCell(1 To 5) As String
10 Dim i As Integer
11
12 'Establishes the cell references for each item in the Arrays. 5 shown here for example. Arbitrary values in SourceCells and no values in TargetCells
13 SourceCell(1) = "A1"
14 SourceCell(2) = "B1"
15 SourceCell(3) = "C1"
16 SourceCell(4) = "D1"
17 SourceCell(5) = "E1"
18 cellData(1) = 42
19 cellData(2) = 42
20 cellData(3) = 42
21 cellData(4) = 42
22 cellData(5) = 42
23 TargetCell(1) = "A5"
24 TargetCell(1) = "A6"
25 TargetCell(1) = "A7"
26 TargetCell(1) = "A8"
27 TargetCell(1) = "A9"
28
29 'Dimensionalizes an Excel Object as a Workbook to hold the Source Workbook open while data gathering and function codes are proceeding
30 Dim SourceWbObject As Workbooks
31
32 'Simply checks to ensure Source file actually exists, and exits if it does not
33 If Dir(SourcePath & SourceWb) = "" Then
34 MsgBox ("File " & Chr(34) & SourcePath & SourceWb & Chr(34) & " was not found.")
35 Exit Function
36 End If
37
38 'Dimensionalizes a string variable to hold the complete filepath, and sets the Excel Workbook Object to the Source Workbook
39 Dim SourceWbPath As String
40 SourceWbPath = SourcePath & SourceWb
41 MsgBox ShiftPressed()
42
43 Set SourceWbObject = Workbooks.Open(SourceWbPath, True, True)
44 'MsgBox ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
45 'SourceWbObject
46 MsgBox ThisWorkbook.Worksheets("Sheet1").Range("A1").Value & Chr(13) & Workbooks.Count
47
48 'Sets up a For loop that is supposed to cycle through the cells in the SourceCell Array and set the values gathered to items in the cellData Array
49 i = 1
50 For i = 1 To 5
51 cellData(i) = ThisWorkbook.Worksheets("Sheet1").Range(SourceCell(i)).Value
52 'cellData(i) = ThisWorkbook.Worksheets("Sheet1").Range(SourceCell(i)).Value
53 i = i + 1
54 Next
55
56
57 'Sets up a For loop that transcribes the values gathered in the above For loop to the items in the TargetCell Array
58 i = 1
59 Workbooks("Target.xlsm").Activate
60 With ThisWorkbook.Worksheets("Sheet1").Activate
61 For i = 1 To 5
62 ActiveCell.Offset(0, i).Activate
63 ActiveCell.Value = cellData(i)
64 i = i + 1
65 Next
66 End With
67
68 'Basic Housekeeping: Closes the Source Workbook, and clears the Excel Object that held it.
69 'SourceWbObject.Close False
70 'Set SourceWbObject = Nothing
71
72
73 'Error Handling stub: Simply restores EnableEvents properties (which may have been disabled by the Error), and restores screen updating prior to exiting function
74 ErrHandler:
75 Dim Msg As String
76 Msg = "Error # " & Str(Err.Number) & " was generated by " & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
77 MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
78 Application.EnableEvents = True
79 Application.ScreenUpdating = True
End Function
私はWorkbooks.Open多くの異なる繰り返しを試してみましたが、何も動作していないようにみえます。私がこれを始めたとき、私はMsgBoxのエラー情報を取得する方法を知らなかったので、私はMsxBoxを使って、何が起こっているのかを試してみるための情報を私に与えていました。 43行目以降のMsgBoxは、Excelがアクティブなブックとして正しいブックを持っていると言います.Countの1から2に示すようにソースブックを開くことに成功しました。それもやってください)。私は、私が犯したエラーの数を最小限に抑えるために何かを試していた時とは違ったものを試してみました。ここでは、ここで助けを求める前に試した最後の繰り返しです。私はScreenUpdatingをチェックアウトしたので、視覚的に何が行われているかを視覚的に確認することができたので、いったんコードを実行すれば元の状態に戻すことができます。変数またはWithブロック変数が設定されていませんが、Withブロックの前にコードが停止し、Line 43にSetステートメントがあるため、これを理解できません。429も取得しました:ActiveXコンポーネントは同じ行にオブジェクトを作成できません。ここにあるスレッドは、これはShiftキーが押されているとExcelに起因するバグである可能性があると述べているので、Shiftキーが押されてMsgBoxがOpenラインの直前に表示され、私はこれが問題だとは思わない。複数のソース、Excelのヒント、ミスターExcel、MSDN、そしてここではすべて、Workbooks.Openは私が望むものを正確に実行すべきだと述べていますが、なぜそれが私にとってうまくいかないのか理解できません... !あなたの時間と配慮していただきありがとうございます!
'ThisWorkBook'は常にコードを含むものに適用されるので、代わりに' SourceWbObject'を使用する必要があります – avb
行46および51のこのワークブックをSourceWbObjectに変更すると、コンパイルエラー、エラー461メソッドまたはデータメンバーが見つかりません。私はこれを試してみたのを忘れてしまったことを忘れていました...私はTarget Workbookに影響を与えたいのでThisWorkbookを60行から66行まで使用しましたが、コードがそこに到達する前にエラーが発生しています。 .. – Valiant
'msgbox SourceWbObject.path'それは何を表示しますか? – avb