Results 1 to 7 of 7

Thread: VBA to Copy Data From Other Workbooks Suddenly Fails

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Question VBA to Copy Data From Other Workbooks Suddenly Fails

    Hello all,

    I found a solution on this site to a problem I was having in excel. A few small changes were needed, but then it worked flawlessly. After several times of running the code, I tried again today, but now it doesn't add any new information. It runs the script, no errors, and then doesn't paste any data. I have no idea why it has randomly stopped working.

    Code:
    Sub BulkImport()
    
       Dim InFileNames As Variant
       Dim fCtr As Long
       Dim tempWkbk As Workbook
       Dim consWks As Worksheet
       Set consWks = ThisWorkbook.Sheets(1)
       InFileNames = Application.GetOpenFilename _
       (FileFilter:="Excel Files, *.*", MultiSelect:=True)
       Application.ScreenUpdating = False
       If IsArray(InFileNames) Then
          For fCtr = LBound(InFileNames) To UBound(InFileNames)
            With Workbooks.Open(Filename:=InFileNames(fCtr))
                .Sheets(1).Range("A2:I" & .Sheets(1).Range("A" & .Sheets(1).Rows.Count).End(xlUp).Row).Copy consWks.Range("A" & consWks.Rows.Count).End(xlUp)(2)
                .Close 0
            End With
          Next fCtr
       Else
          MsgBox "No file selected"
       End If
       With Application
          .StatusBar = True
          .ScreenUpdating = True
       End With
       
    End Sub
    Any ideas?

    Thanks,

    Amit

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    It's working fine.

    can you add this line after With Workbooks.Open...

    Code:
    MsgBox "Number of cells that have data: " & Application.WorksheetFunction.CountA(.Sheets(1).Range("A2:I" & .Sheets(1).Range("A" & .Sheets(1).Rows.Count).End(xlUp).Row))
    and let us know what the msgbox returns.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0
    Hello,

    I added the line you provided and the output was "Number of cells that have data: 27326"

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    The code looks fine. Can you please attach the workbooks here ?
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0
    Interestingly enough, I copied the module from the personal workbook (all workbooks) to the specific workbook that will be using this exclusively, and it worked again. I'm not sure why though.

  6. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    It's got to do with the difference between ActiveWorkbook and ThisWorkbook.

    ThisWorkbook always refers to the workbook file wherein the code is running from. If you change ThisWorkbook, to ActiveWorkbook, the code should run from the personal.xlsb file too.

    ThisWorkBook object refers to the workbook that the code is contained in. ActiveWorkBook object refers to the workbook that is currently active.

    Most times, they will refer to the same workbook. But if the workbook running the code is not the active workbook then they will point to different objects.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0
    Thank you very much guys. Your prompt and helpful responses helped immensely. I'm still new to all this and I appreciate all your help.

    I'm not sure how to set this thread as [SOLVED], unless that's not needed.

    - Amit

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2013, 05:32 PM
  2. Replies: 3
    Last Post: 05-14-2013, 03:25 PM
  3. Form suddenly disappears
    By tfurnivall in forum Access Help
    Replies: 3
    Last Post: 12-20-2012, 06:47 PM
  4. Replies: 2
    Last Post: 11-08-2012, 01:15 PM
  5. VBA Code to Open Workbook and copy data
    By Howardc in forum Excel Help
    Replies: 16
    Last Post: 08-15-2012, 06:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •