+ Reply to Thread
Results 1 to 11 of 11

assistance required copy macro not giving expected results to new sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    assistance required copy macro not giving expected results to new sheet

    Hi All

    Trying to copy specific column to another sheet but some columns need not to be copied but still being copied by the macro.

    Should start at A1 of the new sheet

    See attached of expected results

    Can anyone help

    
    
    Sub M1()
    
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        
        Dim arr()   As Variant
        Dim var     As Variant
        Dim x       As Long
        
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        
        With ws1
            For Each var In Array("A", "B", "C", "D", "H", "I", "K", "L", " M", "Q", "R", "S", "T", "W")
                x = .Range(CStr(var) & .Rows.Count).End(xlUp).Row
                arr = .Range(CStr(var) & 1).Resize(x).Value
                
                ws2.Range(CStr(var) & 1).Resize(UBound(arr, 1)).Value = arr
                Erase arr
            Next var
        End With
        
        Set ws1 = Nothing
        Set ws2 = Nothing
    
    End Sub
    Attached Files Attached Files
    Last edited by JEAN1972; 01-14-2017 at 11:30 AM.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: assistance required copy macro not giving expected results to new sheet

    This should work

    Sub M1()
    
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        
        Dim arr()   As Variant
        Dim var     As Variant
        Dim x       As Long
        Dim icol    As Long
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        
        With ws1
            x = .Range("a" & .Rows.Count).End(xlUp).Row: icol = 1
            For Each var In Array("A", "B", "C", "D", "H", "I", "K", "L", " M", "Q", "R", "S", "T", "W")
                
                arr = .Range(CStr(var) & 1).Resize(x).Value
                
                ws2.Cells(1, icol).Resize(UBound(arr, 1)).Value = arr
                Erase arr: icol = icol + 1
            Next var
        End With
        
        Set ws1 = Nothing
        Set ws2 = Nothing
    
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: assistance required copy macro not giving expected results to new sheet

    Quote Originally Posted by mike7952 View Post
    This should work

    Sub M1()
    
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        
        Dim arr()   As Variant
        Dim var     As Variant
        Dim x       As Long
        Dim icol    As Long
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        
        With ws1
            x = .Range("a" & .Rows.Count).End(xlUp).Row: icol = 1
            For Each var In Array("A", "B", "C", "D", "H", "I", "K", "L", " M", "Q", "R", "S", "T", "W")
                
                arr = .Range(CStr(var) & 5).Resize(x).Value
                
                ws2.Cells(1, icol).Resize(UBound(arr, 1)).Value = arr
                Erase arr: icol = icol + 1
            Next var
        End With
        
        Set ws1 = Nothing
        Set ws2 = Nothing
    
    End Sub

    Thanks mike working a little adjustment in bold

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: assistance required copy macro not giving expected results to new sheet

    You left a blank column in G.

    Sub M1()
    
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        Dim NextCol  As Long
        Dim arr()   As Variant
        Dim var     As Variant
        Dim x       As Long
        
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        NextCol = 1
        With ws1
            For Each var In Array("A", "B", "C", "D", "H", "I", "K", "L", " M", "Q", "R", "S", "T", "W")
                x = .Range(CStr(var) & .Rows.Count).End(xlUp).Row
                arr = .Range(CStr(var) & 5).Resize(x).Value
                
                ws2.Cells(1, NextCol).Resize(UBound(arr, 1)).Value = arr
                NextCol = NextCol + 1
                Erase arr
            Next var
        End With
        
        Set ws1 = Nothing
        Set ws2 = Nothing
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: assistance required copy macro not giving expected results to new sheet

    Thank you for assistance working now

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: assistance required copy macro not giving expected results to new sheet

    I don't understand? What in Bold?

  7. #7
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: assistance required copy macro not giving expected results to new sheet

    Quote Originally Posted by mike7952 View Post
    I don't understand? What in Bold?
    
    arr = .Range(CStr(var) & 5).Resize(x).Value
    I was meaning that it was 1 instead of 5 as it copies the first 4 rows which was not required as per expected csv example.

  8. #8
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,493

    Re: assistance required copy macro not giving expected results to new sheet

    Sub M1()
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        Dim COL, Ncol As String: COL = "ABCDHIKLMQRSTW"
        Dim I&
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        With ws1
           For I = 1 To Len(COL)
               Ncol = Mid(COL, I, 1)
                  Set Rng = Intersect(.UsedRange.Offset(5, 0), .Columns(Ncol).SpecialCells(xlCellTypeConstants))
                  Rng.Copy ws2.Range(Ncol & 5)
             Next
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: assistance required copy macro not giving expected results to new sheet

    Quote Originally Posted by daboho View Post
    Sub M1()
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        Dim COL, Ncol As String: COL = "ABCDHIKLMQRSTW"
        Dim I&
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        With ws1
           For I = 1 To Len(COL)
               Ncol = Mid(COL, I, 1)
                  Set Rng = Intersect(.UsedRange.Offset(5, 0), .Columns(Ncol).SpecialCells(xlCellTypeConstants))
                  Rng.Copy ws2.Range(Ncol & 5)
             Next
        End With
    End Sub
    Hi Daboho

    I tested your code it copies the blanks column which I do not need

    I have also modified the code so that it starts the headers , however I have no idea why it copies the blank column (I d'ont need these columns
    Check the csv expected results , however I leave column G blank, to ignore G as I do not need this columns


    
    Sub dabohoM1()
        Dim ws1     As Worksheet
        Dim ws2     As Worksheet
        Dim COL, Ncol As String: COL = "ABCDHIKLMQRSTW"
        Dim I&
        Set ws1 = Sheets("corps")
        Set ws2 = Sheets("csv")
        With ws1
           For I = 1 To Len(COL)
               Ncol = Mid(COL, I, 1)
                  Set Rng = Intersect(.UsedRange.Offset(4, 0), .Columns(Ncol).SpecialCells(xlCellTypeConstants))
                  Rng.Copy ws2.Range(Ncol & 1)
             Next
        End With
    End Sub

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: assistance required copy macro not giving expected results to new sheet

    sorry my bad

  11. #11
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: assistance required copy macro not giving expected results to new sheet

    Quote Originally Posted by mike7952 View Post
    sorry my bad
    No problem, we are here to help each other

    The climate is very hot where I am, my head gets dumbed .

    Thanks again for your help

    Regards

    Jean

+ 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] Create Table Style Macro Not Producing Expected Results
    By mcclanat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2015, 10:47 AM
  2. "IF" statement not giving expected results
    By khank in forum Excel General
    Replies: 6
    Last Post: 11-17-2010, 04:14 PM
  3. IF Formula not giving expected results.
    By fungus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-17-2010, 02:10 PM
  4. Macro is not generating expected results.
    By Foxcan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-10-2009, 04:16 PM
  5. Sumproduct formula not giving expected results
    By Shocked in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 09-30-2008, 03:12 PM
  6. [SOLVED] Why isn't this macro giving me proper results?
    By Girish in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2005, 02:05 PM
  7. MACRO Assistance Required
    By Andrew Fletcher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-31-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