当我使用以下代码时,我收到运行时1004错误“应用程序定义或对象定义的错误”:
Sub GetPCData()
'Get PC response ratios
PCanalytes = Array("Furosemide", "Caffeine", "Ketoprofen", "Phenylbutazone", "Flunixin")
PCanalytePositions = Array("J20", "K20", "L20", "M20", "N20")
Set SQWorkbook = Application.ActiveWorkbook
Dim sourceSheet, targetSheet As Worksheet
Dim copyFromRange, copyToRange As Range
Dim Y As Range
Set targetSheet = ThisWorkbook.Sheets("QC data")
For i = 0 To SQWorkbook.Worksheets.Count
Set sourceSheet = SQWorkbook.Worksheets(PCanalytes(i))
Set Y = sourceSheet.Range("H8").End(xlDown)
Set copyToRange = targetSheet.Range(PCanalytePositions(i))
Set copyFromRange = sourceSheet.Range("H8", Y)
copyToRange.Value = Application.WorksheetFunction.Average(copyFromRange)
copyToRange.PasteSpecial xlPasteValues
Next i
End Sub
我需要使用两个单元格H8中的值和定义的数组中定义为Y的变量来计算平均值。所以我需要两个独立单元的平均值,其中一个是由该宏处理的数据集定义的变量。很抱歉,如果这是一个简单的修复,我无法找到解决方法。
答案 0 :(得分:0)
看看这是否有效,因为我已经假设你想要实现的目标。
Sub GetPCData()
Dim PCanalytes, PCanalytePositions
'Get PC response ratios
PCanalytes = Array("Furosemide", "Caffeine", "Ketoprofen", "Phenylbutazone", "Flunixin")
PCanalytePositions = Array("J20", "K20", "L20", "M20", "N20")
Set SQWorkbook = Application.ActiveWorkbook
Dim sourceSheet As Worksheet, targetSheet As Worksheet 'note you need to add the type for each declaration
Dim copyFromRange As Range, copyToRange As Range 'ditto
Dim Y As Range
Set targetSheet = ThisWorkbook.Sheets("QC data")
For i = 0 To SQWorkbook.Worksheets.Count
Set sourceSheet = SQWorkbook.Worksheets(PCanalytes(i))
Set copyToRange = targetSheet.Range(PCanalytePositions(i))
Set copyFromRange = sourceSheet.Range("H8", sourceSheet.Range("H8").End(xlDown))
With copyFromRange
copyToRange.Value = Application.WorksheetFunction.Average(.Cells(1), .Cells(.Cells.Count))
End With
Next i
End Sub