EXCEL VBA ERROR 424

后端 未结 1 1227
闹比i
闹比i 2021-01-23 20:00

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,

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 20:05

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

    0 讨论(0)
提交回复
热议问题