Results 1 to 8 of 8

Edit looping code to copy range from one workbook to another

Threaded View

  1. #1
    Forum Contributor
    Join Date
    05-12-2014
    MS-Off Ver
    Excel 2013
    Posts
    131

    Edit looping code to copy range from one workbook to another

    Heya!
    Private Sub test()
    Dim x As Workbook, y As Workbook
    Dim sdt As String, wbNam As String, dt As String, ldt As String, i As Long
    
    wbNam = "Productivity "
    dt = Sheet1.Range("B1").Value
    sdt = Format(CStr(dt), "m.d.yy") & ".xlsx"
    ldt = Format(CStr(dt), "yyyy") & "\" & Format(CStr(dt), "mm") & "_" & MonthName(Month(dt)) & "_" & Year(dt)
    
    With Sheets("Team One")
        sn = Cells(1).CurrentRegion.Value
    End With
    
    Set y = Workbooks.Open("S:\" & ldt & "\" & wbNam & sdt)
    
    For i = 2 To UBound(sn)
        If sn(i, 1) <> vbNullString Then
    Set x = ThisWorkbook
    
    'Now, copy what you want from x:
            x.Activate
            ActiveSheet.Cells.Find(sn(i, 2)).Resize(10, 8).Offset(1, 0).Copy y.Sheets("MSP WPS").Cells.Find(sn(i, 2)).Offset(1, 0)
            ActiveSheet.Cells.Find(sn(i, 2)).Resize(2, 8).Offset(12, 0).Copy y.Sheets("MSP WPS").Cells.Find(sn(i, 2)).Offset(12, 0)
    
    'Close x
            x.Close SaveChanges:=True
        End If
    
    Next
    
    End Sub
    So I had a thread on this yesterday and I didn't want to dredge it up again, so I'm posting a new thread with the code I've compiled thus far.

    Attached is an example workbook. Sheet1 and Sheet2 on this workbook would be separate workbooks in practice, but are just sheets for consolidation purposes.

    Basically, what I'm trying to do is have Excel use the value in Sheet1, A2 as the lookup option, copy the specified range, then find the value on Sheet 2 and paste it. Then I'd like it to move on to the next name on Sheet 1 which is in A18, and repeat the exact same process. Then do that over and over until it hits a blank, then stop.

    The concept of looping makes sense, but the declaration of i or x as Long is wildly confusing to me, so I'm not sure how to tell the code to execute what I have above starting at A2 then A18, then A34, etc etc.

    I spent a majority of yesterday evening trying to google it, but this is the best I've come up with so far and it isn't working. If anyone could identify the flaw in my code/logic and help me out, I'd appreciate it!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA code to copy range of table from Excel 2010 to (existing) ppt (looping through a list)
    By megaheinz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2016, 04:29 AM
  2. code to enter hyperlinked workbook and copy range
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-24-2014, 10:32 AM
  3. VBA code to copy a range from open workbook to another workbook
    By Frank Harrison in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-01-2014, 09:21 AM
  4. [SOLVED] Code to copy data from a closed workbook and paste in active workbook using named range.
    By paullie1912 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2014, 02:38 AM
  5. Code looping somewhere & freezing up workbook?
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-29-2012, 05:16 PM
  6. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 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