EXCEL VBA错误424 [英] EXCEL VBA ERROR 424

查看:580
本文介绍了EXCEL VBA错误424的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据其列将vba中的某些单元格从用户选择的Excel文件(req)复制到另一个固定文件(rfqq).下面是我的代码,它带来错误424,对象为必需. 有人可以帮忙吗?.我是新手,希望对此有一个非常简单的答案!

I am trying to copy some cells in vba based on their column, from an excel file chosen by the user (req), to another fixed file (rfqq). below is my code which brings error 424, object required. could anyone help?.I am a newbie and expect a very simple answer to this!

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

推荐答案

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

you need to define the variable req as a workbook object, and use a different string for the name of the workbook you want to open.

因此,请如下更改代码:

so change your code as below:

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")

这篇关于EXCEL VBA错误424的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆