Results 1 to 5 of 5

Copy several columns into a specific range (new workbook)

Threaded View

Barrigudo Copy several columns into a... 02-09-2016, 12:28 PM
joe31623 Re: Copy several columns into... 02-09-2016, 01:20 PM
Barrigudo Re: Copy several columns into... 02-09-2016, 03:21 PM
MarvinP Re: Copy several columns into... 02-09-2016, 01:55 PM
Barrigudo Re: Copy several columns into... 02-10-2016, 05:19 AM
  1. #1
    Registered User
    Join Date
    02-09-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Copy several columns into a specific range (new workbook)

    Hi there,

    I have been lately working with VBA and I got stuck, selecting the data I am interested in and copy it into a new workbook. I have seen examples and youtube videos but I have clearly missed something.

    I have posted part of the code, as the rest is working fine:


    Private Sub Obtain_Data_Click()
    
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim SelectedFiles() As Variant
        Dim NRow As Long
        Dim FileName As String
        Dim NFile As Long
        Dim ThisWorkbook As Workbook
        Dim SourceRange As Range
        Dim DestRange As Range
        Dim i As Integer
        
        Dim lastrow As Long, lastcolumn As Long
        
        
        
        ' Create a new workbook and set a variable to the first sheet.
        Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        
        ' Modify this folder path to point to the files you want to use.
        FolderPath = "C:\Users\Mark\Desktop\PV Data"
        
        
                  
                  
        ' Set the current directory to the the folder path.
        ChDrive FolderPath
        ChDir FolderPath
        
        ' Open the file dialog box and filter on Excel files, allowing multiple files
        ' to be selected.
        SelectedFiles = Application.GetOpenFilename( _
            filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
        
        ' NRow keeps track of where to insert new rows in the destination workbook.
        NRow = 1
        
        ' Loop through the list of returned file names
        For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
            ' Set FileName to be the current workbook file name to open.
            FileName = SelectedFiles(NFile)
            
            ' Open the current workbook.
            Set ThisWorkbook = Workbooks.Open(FileName)
            
            'Copy column A
            
            Dim sh1 As Worksheet, sh2 As Worksheet, p As Long
        Dim j As Long, N As Long, r1 As Range
    
        Set sh1 = ThisWorkbook.Sheets("Sheet1")
        Set sh2 = SummarySheet
        N = sh1.Cells(Rows.Count, "A").End(xlUp).Row
        j = p
    
        For p = 1 To N
            Set r1 = sh1.Cells(p, "A")
            If r1.Value <> "" Then
                r1.Copy sh2.Cells(j, "A")
                j = j + p
            End If
        Next p
    
     'Copy column A
            
            Dim sh1 As Worksheet, sh2 As Worksheet, p As Long
        Dim j As Long, N As Long, r1 As Range
    
        Set sh1 = ThisWorkbook.Sheets("Sheet1")
        Set sh2 = SummarySheet
        N = sh1.Cells(Rows.Count, "A").End(xlUp).Row
        j = p
    
        For p = 1 To N
            Set r1 = sh1.Cells(p, "A")
            If r1.Value <> "" Then
                r1.Copy sh2.Cells(j, "A")
                j = j + p
            End If
        Next p
    
    
    
    ' Set the cell in column F2 to be the file name.
            SummarySheet.Range("A2").Value = ActiveWorkbook.Name
            
            
          
              SummarySheet.Range("A1").Value = "Name of the Site"


    Basically I want to copy several (but not all) columns from the opened file, into specific columns inside the new workbook.

    Any help is highly appreciated

    Many Thanks

    Ricardo
    Last edited by Barrigudo; 02-09-2016 at 12:47 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 07-24-2014, 09:11 AM
  2. [SOLVED] Copy Two Specific Columns from One Spreadsheet to Another in the Same Workbook
    By Deathwreaker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-29-2014, 05:04 PM
  3. [SOLVED] Copy a specific range from one workbook to another
    By ArnolddG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2014, 08:07 AM
  4. how to copy specific columns form one workbook to another
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2014, 11:53 PM
  5. how to copy specific columns form one workbook to another
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2014, 10:49 PM
  6. Copy specific columns from one workbook to another workbook
    By Tegpreet in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2013, 07:55 AM
  7. Copy specific columns and save into another workbook
    By stoey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2008, 04:02 PM

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