EXCEL VBA错误424

时间:2014-08-15 13:41:20

标签: excel vba excel-vba

我正在尝试根据列中的一些单元格,从用户选择的excel文件(req)复制到另一个固定文件(rfqq)。下面是我的代码,它带来了错误424,需要对象。 任何人都可以帮忙吗?。我是一个新手,期待一个非常简单的答案!

Sub rfqo()

Dim rfq As Workbook
Dim req as variant
Dim rowcount, rfqc, reqab, i As Integer
req = Application.GetOpenFilename
If req = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
    Exit Sub
Else
Workbooks.Open Filename:=req
    End If
      rowcount = ActiveSheet.UsedRange.Rows.Count
Set rfq = Workbooks.Open("c:\users\mostafa\desktop\rfqq.xlsx")
For i = 1 To rowcount + 12
rfqc = 12 + i
reqab = 2 + i
rfq.Sheets("Sheet1").Range("C12:C" & rfqc).Value = _
req.Sheets("Sheet1").Range("AB2:AB" & reqab).Value
rfq.Sheets("sheet1").Range("e12:e" & rfqc).Value = _
req.Sheets("sheet1").Range("ac2:ac" & reqab).Value
rfq.Sheets("sheet1").Range("f12:f" & rfqc).Value = _ 
req.Sheets("sheet1").Range("af2:af" & reqab).Value
rfq.Sheets("sheet1").Range("e12:e" & rfqc).Value = _
req.Sheets("sheet1").Range("ac2:ac" & reqab).Value
rfq.Sheets("sheet1").Range("g12:g" & rfqc).Value = __
req.Sheets("sheet1").Range("ag2:ag" & reqab).Value
Next i

End Sub

1 个答案:

答案 0 :(得分:2)

您需要将变量req定义为工作簿对象,并使用不同的字符串作为要打开的工作簿的名称。

所以改变你的代码如下:

Dim rfq As Workbook, req as Workbook '~~>Change 'req' to Workbook type
Dim vBookName as variant '~~>Add new variant to capture the name
Dim rowcount As Integer, rfqc As Integer, reqab As Integer, i As Integer
'~~>without 'As Integer' statements, all but the last one of these was a variant

vBookName = Application.GetOpenFilename '~~>replace 'req' w 'vBookName' in this section
If vBookName = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
    Exit Sub
Else
    Set req = Workbooks.Open(Filename:=vBookName) '~~> Use 'Set' as 'req' is an object
End If

rowcount = ActiveSheet.UsedRange.Rows.Count
Set rfq = Workbooks.Open("c:\users\mostafa\desktop\rfqq.xlsx")