+ Reply to Thread
Results 1 to 4 of 4

Data not copied

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,765

    Data not copied

    I have the following macro below to open several files and copy data


    The data is not being copied


    It would be appreciated if someone can kindly check & amend my code

     Sub Open_Workbook()
    Clear_Data
    A:
    Dim A     As Variant
    Dim LR As Long
         
        Dim file As Variant
        
        Dim FileAry As Variant, Fle As Variant
       With Application.FileDialog(3)
          .InitialFileName = "C:\Sales\"
          .AllowMultiSelect = True
          .InitialFileName = "C:\Sales\*BIGGS*.csv*"
          If .Show Then Set FileAry = .SelectedItems()
       End With
       'For Each file In FileAry
       If TypeName(A) = "Boolean" Then Exit Sub
    
      
        
        Application.ScreenUpdating = False
            
    
         
            With Sheets(1)
                .Range("a1", .Range("Z" & Rows.Count).End(xlUp)).Copy _
                    Destination:=ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1)
            
             
            End With
      
       
     Application.ScreenUpdating = True
    End Sub
    Sub Clear_Data()
    Dim LR As Long
    With Sheets(1)
    LR = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("A1:Z" & LR).ClearContents
    End With
    
    End Sub

    Have posted a similar post on https://www.mrexcel.com/board/thread...files.1148019/
    Last edited by Howardc1001; 10-13-2020 at 11:01 PM.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,544

    Re: Data not copied

    Found this Howard. Select your folder and sail on home.
    Change references where required first.
    Sub LoopAllCSVFilesInFolder()
    
    'SOURCE: www.TheSpreadsheetGuru.com
    
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    
    'Optimize Macro Speed
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    
    'Retrieve Target Folder Path From User
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    'In Case of Cancel
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
    'Target File Extension (must include wildcard "*")
      myExtension = "*.csv*"
    
    'Target Path with Ending Extention
      myFile = Dir(myPath & "*" & "BTMK" & "*" & myExtension)    '<----- Copies from all files that have "BTMK" in the file name
    
    'Loop through each Excel file in folder
      Do While myFile <> ""
        'Set variable equal to opened workbook
          Set wb = Workbooks.Open(Filename:=myPath & myFile)
        
        'Ensure Workbook has opened before moving on to next line of code
          DoEvents
        
        'Copy and Paste Data
          wb.Worksheets(1).Range("A1:I40").Copy ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)    '<---- Change as required
        
        'Save and Close Workbook
          wb.Close SaveChanges:=False
          
        'Ensure Workbook has closed before moving on to next line of code
          DoEvents
    
        'Get next file name
          myFile = Dir
      Loop
    
    'Message Box when tasks are completed
      MsgBox "Task Complete!"
    
    ResetSettings:
      'Reset Macro Optimization Settings
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,765

    Re: Data not copied

    Thanks for the help, much appreciated

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,544

    Re: Data not copied

    Thanks for the update and good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] When data is copied to another workbook, hyperlinks as not copied or they don't work
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-17-2014, 06:46 AM
  2. Trying to filter data and display it on another sheet, however the data isn't fully copied
    By patrickvdveen88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2013, 12:54 PM
  3. [SOLVED] VBA copied chart keeps on pointing to original data, VBA relinking charts to nesource data
    By dastr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-25-2013, 06:58 AM
  4. Copying multiple worksheet so that copied hyperlinks link to new copied sheets
    By chrisjames25 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2013, 02:37 PM
  5. [SOLVED] Copying data adds carriage return to data copied
    By extrapulp in forum Excel General
    Replies: 2
    Last Post: 10-09-2012, 01:08 PM
  6. copy data inserting a prefix to copied data
    By CPGLH in forum Excel General
    Replies: 2
    Last Post: 02-23-2012, 01:15 AM
  7. [SOLVED] New data will not sort with existing data from copied file
    By Carolyn in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2006, 10:15 AM
  8. can i convert data copied as an image into data in excel ?
    By prabhat in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-01-2005, 09:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1