+ Reply to Thread
Results 1 to 11 of 11

Thread: Macro to pull data from sheet1 to sheet2

  1. #1
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Macro to pull data from sheet1 to sheet2

    Hi freinds,

    Attached is excel file.

    Sheet1 contains raw data (which needs to be arrange in sheet2)
    Sheet2 contains final output (Output will reflect correspondent data from sheet1)

    Any macro or formula will be very help full.

    Thanks for your views.....and time.
    Attached Files Attached Files
    Last edited by rupss01; 12-11-2011 at 01:28 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Macro to pull data from sheet1 to sheet2

    hi rupss01, please check attachment, run code "test"

    Option Explicit
    Sub test()
    
    Dim data, result, n As Long, j As Long, i As Long, icounter As Long, addr As String, x As Long
    
    
    With Sheets("Sheet1")
        If .Range("a1") = "" Then Exit Sub
        data = .Range("a1", .Cells(Rows.Count, "a").End(xlUp))
    End With
    
    ReDim result(1 To UBound(data) / 12, 1 To 7)
    
    For n = 1 To UBound(data) Step 12
        
        j = j + 1
        
        i = n
        
        icounter = icounter + 1
        result(j, 1) = icounter
        
        result(j, 2) = data(i, 1)
        
        addr = data(i + 1, 1) & ", "
        
        i = i + 2
        
        Do
            
             addr = addr & data(i, 1)
            
            i = i + 1
            
            If data(i, 1) = "" Then Exit Do
            
        Loop Until InStr(1, data(i, 1), "-") > 0
        
        x = InStrRev(addr, ",")
        
        result(j, 3) = Mid(addr, 1, x - 1)
        
        result(j, 4) = Mid(addr, x + 2, Len(addr))
        
        result(j, 5) = data(i, 1)
        
        result(j, 6) = CInt(Split(data(i + 6, 1), " ")(0))
        
        result(j, 7) = CInt(Split(data(i + 7, 1), " ")(0))
        
        addr = ""
        
    Next
    
    If j > 0 Then Sheets("Sheet2").Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(j, 7) = result
    
    End Sub
    Attached Files Attached Files

  3. #3
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Macro to pull data from sheet1 to sheet2

    if assume that three row address will look like rows 1-12 sample of the attachment, it might be done like this

    Option Explicit
    Sub test()
    
    Dim data, result, n As Long, j As Long, i As Long, icounter As Long, addr As String, x As Long, zctrl As Integer, corr As Integer
    
    
    With Sheets("Sheet1")
        If .Range("a1") = "" Then Exit Sub
        data = .Range("a1", .Cells(Rows.Count, "a").End(xlUp))
    End With
    
    ReDim result(1 To UBound(data) / 12, 1 To 7)
    
    For n = 1 To UBound(data) Step 12
        
        j = j + 1
        
        i = n
        
        icounter = icounter + 1
        result(j, 1) = icounter
        
        result(j, 2) = data(i, 1)
        
        addr = data(i + 1, 1)
        
        i = i + 2
        
        zctrl = 1
        
        Do
            
            addr = addr & ", " & data(i, 1)
            
            zctrl = zctrl + 1
            
            i = i + 1
            
            If data(i, 1) = "" Then Exit Do
            
        Loop Until InStr(1, data(i, 1), "-") > 0
        
        If zctrl = 3 Then corr = 1 Else corr = 0
        
        x = InStrRev(addr, ",")
        
        result(j, 3) = Mid(addr, 1, x - 1)
        
        result(j, 4) = Mid(addr, x + 2, Len(addr))
        
        result(j, 5) = data(i, 1)
        
        result(j, 6) = CInt(Split(data(i + 6 - corr, 1), " ")(0))
        
        result(j, 7) = CInt(Split(data(i + 7 - corr, 1), " ")(0))
        
        addr = ""
        
    Next
    
    If j > 0 Then Sheets("Sheet2").Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(j, 7) = result
    
    End Sub
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Macro to pull data from sheet1 to sheet2

    @watersev...the code works perfects...thanks for your prompt response.

    One query can i pull more than 100 data @ time

  5. #5
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Macro to pull data from sheet1 to sheet2

    the code has no such limitation provided the layout will be the same

  6. #6
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Macro to pull data from sheet1 to sheet2

    if you are happy with the solution provided, please mark the thread as Solved under Forum rules:

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  7. #7
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Macro to pull data from sheet1 to sheet2

    @watersev

    Bro can you please check attached file as well, while running macro getting Runtime Error 13 - Type mismatch

    Unable to pull more than 12 data...

    Surely will follow your's instruction.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Macro to pull data from sheet1 to sheet2

    @watersev

    Also some instance Runtime Error 9- Subscript out of Range

  9. #9
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Macro to pull data from sheet1 to sheet2

    Also some instance Runtime Error 9- Subscript out of Range

  10. #10
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Macro to pull data from sheet1 to sheet2

    The example provided originally has not been representative. You have cases without address at all, without end part etc. Try this option, press Start on Sheet 2
    Attached Files Attached Files
    Last edited by watersev; 12-10-2011 at 08:29 PM.

  11. #11
    Registered User
    Join Date
    03-25-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Macro to pull data from sheet1 to sheet2

    @watersev

    thanx a lot for your time and efforts ...instruction followed

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0