+ Reply to Thread
Results 1 to 5 of 5

Last Used Column in Loop Data Copy

Hybrid View

gsurge Last Used Column in Loop Data... 05-31-2011, 11:49 AM
Leith Ross Re: Last Used Column in Loop... 05-31-2011, 12:15 PM
gsurge Re: Last Used Column in Loop... 05-31-2011, 12:23 PM
Leith Ross Re: Last Used Column in Loop... 05-31-2011, 12:33 PM
gsurge [SOLVED] Last Used Column in... 05-31-2011, 01:29 PM
  1. #1
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Exclamation Last Used Column in Loop Data Copy

    Hello,

    Can someone please help me adjust the following code to pull data from the Last Used Column in the source workbooks, thus replacing the "B" column designation? I do not want the code to require adjusting the column value, and instead, simply search for the last column with data on the source workbook to copy to the destination workbook.

    Thanks

    Sub datacollect()
      
    Dim C As Long
      Dim DstWks1 As Worksheet
      Dim LastRow As Long
      Dim R As Long
      Dim SrcWkb As Workbook
      Dim StartRow As Long
      Dim wkbname As Variant
      Dim xlsFiles As Variant
      
       'Starting column and row for the destination workbook
        C = 2
        R = 2
       'Set references to destination workbook worksheet objects
        Set DstWks1 = ThisWorkbook.Worksheets("Data")
        
       'Starting row on source worksheet
        StartRow = 1
        
       'Get the workbooks to open
        xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls), *.xls", MultiSelect:=True)
        Application.AskToUpdateLinks = False
          If VarType(xlsFiles) = vbBoolean Then Exit Sub
          
         'Loop through each workbook and copy the data to this workbook
          For Each wkbname In xlsFiles
            Set SrcWkb = Workbooks.Open(Filename:=wkbname, ReadOnly:=True)
              LastRow = SrcWkb.Worksheets("Equity").Cells(Rows.Count, "B").End(xlUp).Row
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets("Equity")
                    DstWks1.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, "B"), .Cells(LastRow, "B")).Value
                  End With
                End If
            C = C + 1
            SrcWkb.Close savechanges:=False
          Next wkbname
          
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Last Used Column in Loop Data Copy

    Hello gsurge,

    Here is the corrected macro...
    Sub datacollect()
      
      Dim C As Long
      Dim DstWks1 As Worksheet
      Dim LastCol As Variant
      Dim LastRow As Long
      Dim R As Long
      Dim SrcWkb As Workbook
      Dim StartRow As Long
      Dim wkbname As Variant
      Dim xlsFiles As Variant
      
       'Starting column and row for the destination workbook
        C = 2
        R = 2
       'Set references to destination workbook worksheet objects
        Set DstWks1 = ThisWorkbook.Worksheets("Data")
        
       'Starting row on source worksheet
        StartRow = 1
        
       'Get the workbooks to open
        xlsFiles = Application.GetOpenFilename(FileFilter:="Excel files (*.xls), *.xls", MultiSelect:=True)
        Application.AskToUpdateLinks = False
          If VarType(xlsFiles) = vbBoolean Then Exit Sub
          
         'Loop through each workbook and copy the data to this workbook
          For Each wkbname In xlsFiles
            Set SrcWkb = Workbooks.Open(Filename:=wkbname, ReadOnly:=True)
              Set LastCol = SrcWkb.Worksheets("Equity").Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious, False)
              If Not LastCol Is Nothing Then LastCol = LastCol.Column Else Continue
              LastRow = SrcWkb.Worksheets("Equity").Cells(Rows.Count, LastCol).End(xlUp).Row
                If LastRow >= StartRow Then
                  With SrcWkb.Worksheets("Equity")
                    DstWks1.Cells(R, C).Resize(LastRow - StartRow + 1, 1).Value = _
                    .Range(.Cells(StartRow, LastCol), .Cells(LastRow, LastCol)).Value
                  End With
                End If
            C = C + 1
    Continue:
            SrcWkb.Close savechanges:=False
          Next wkbname
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Last Used Column in Loop Data Copy

    Thank you so much Leith!

    Just so I can understand the added code better, can you briefly explain the syntax you entered to determine LastCol?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Last Used Column in Loop Data Copy

    Hello gsurge,

    The code used to find the last column searches the worksheet cells column by column starting with the last column and moving left until it finds a cell in a column or it returns back to cell "A1". The "*" is a wildcard meaning any value in the cell.

    By checking if the result is "Nothing", the decision is made to either extract the column of the last cell or skip to the next workbook. If the resulting Range object isn't check to be "Nothing", a error will occur.

  5. #5
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    [SOLVED] Last Used Column in Loop Data Copy

    thank you so much for the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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