非常新的編碼和第一篇文章在這裏。我查遍了所有地方,但無法弄清楚我的代碼有什麼問題。基本上我試圖從另一個工作簿上的數據計算一些平均值。我運行以下代碼時出現的錯誤「運行時錯誤1004,應用程序定義的錯誤或方法定義的錯誤」似乎是從我參考其他工作簿以執行Match方法的行中生成的。它看起來像它應該工作,但對我來說否則報錯說...感謝您的幫助爲什麼我得到這個運行時錯誤?
Private Sub CommandButton1_Click()
Dim wbk As Workbook
Set wbk = Workbooks.Open("X:\Data Analysis\Process & Wall Loss Data Analysis\***** CT 12 HR AVG rev2.xlsm")
Dim nrow As Integer, conv_start As Double, conv_end As Double, avg1 As Double, avg2 As Double
nrow = Cells(9, 12)
For i = 1 To nrow
conv_start = Application.VLookup(Cells(14 + i, 12), Range(Cells(3, 2), Cells(300, 3)), 2, True)
conv_end = Application.VLookup(Cells(14 + i, 13), Range(Cells(3, 2), Cells(300, 3)), 2, True)
avg1 = Application.Match(conv_start, wbk.Worksheets("PC & Wear").Range(Cells(1, 1), Cells(626, 1)), 1)
avg2 = Application.Match(conv_end, wbk.Worksheets("PC & Wear").Range(Cells(1, 1), Cells(626, 1)), 1)
For j = 1 To 40
Cells(42 + j, 11 + i) = Application.Average(Range(Cells(avg1, 1 + j), Cells(avg2, 1 + j)))
Next j
Next i
末次
你需要限定所有的對象範圍; 'range'和'cells'到工作表。例如'nrow = Cells(9,12)'應該是這樣的:'nrow = wbk.sheets(「Sheet1」)。Cells(9,12)' –