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
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.