+ Reply to Thread
Results 1 to 16 of 16

Macro for copying not specific columns

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Macro for copying not specific columns

    Hi, I had created a macro is looping for some values in one sheet and then copy valid rows to other sheets. However, I would like to amend macro only to copy entire rows to selected column, like A-I. Is there any way to do that? Pasting my macro below:


    Sub CopyRows()
    
    Dim bottomL As Long
    Dim x As Long
    
        
    bottomL = Sheets("Sales").Range("h" & Rows.Count).End(xlUp).Row: x = 1
         
     Dim c As Range
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculateManual
        
        
        For Each c In Sheets("Sales").Range("h2:h" & bottomL)
            If c.Value = "89" Then
                 c.EntireRow.Copy Worksheets("RL").Range("A" & x)
                x = x + 1
            End If
        Next c
        
        bottomL = Sheets("Sales").Range("h" & Rows.Count).End(xlUp).Row: x = 1
        For Each c In Sheets("Sales").Range("h2:h" & bottomL)
            If c.Value = "31" Then
                c.EntireRow.Copy Worksheets("OC").Range("A" & x)
                x = x + 1
            End If
        Next c
    
    End Sub
    Many thanks for help.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    Hi bizzarofen

    What range do you want to copy to Range("A:I")...Not sure to copy entire row to only 9 columns
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    Hello sintek,

    I would like the macro to copy the values from column A:I to separate sheet instead of copying an entire row as I have some formulas in column J, which I would like to preserve.

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    yes, but to what range in other worksheet must it copy to

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    Something like this:
    Sheet1.Range("A1:I1").Copy Sheet2.Range("A1:I1")
    Sheet1.Range("A1:A3").Copy Sheet2.Range("D1")

  6. #6
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    For instance if the value in column C in sheets Sales is 89, the macro should copy values from columns A:I into the tab's RL columns A:I, similarly to what you have just provided. Many thanks for your help.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    If Sheets("Sales").Range("C2") = "89" Then
       Sheets("Sales").Range("A2:I2") .Copy Sheets("RL").Range("A2")
    End If

  8. #8
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    That's precisely what I tried to do. Is there just a way to nest it into the loop:
    For Each c In Sheets("Sales").Range("h2:h" & bottomL)
            If c.Value = "89" Then
                 c.EntireRow.Copy Worksheets("RL").Range("A" & x)
                x = x + 1
            End If
        Next c
    Rather than use fixed cells?

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    Try this adaptation to your code:
    Option Explicit
    Sub CopyRows()
    Dim lRow As Long, RLnRow As Long, OCnRow As Long, x As Long
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("Sales")
    Set ws2 = Sheets("RL")
    Set ws3 = Sheets("OC")
    lRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    
    
    With ws1
        For x = 1 To lRow 'Change 1 to the row no your data starts in Sheets("sales")
            RLnRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
            OCnRow = ws3.Range("A" & Rows.Count).End(xlUp).Row + 1
            If .Range("C" & x) = "89" Then 'Change C to the column your value of 89 is in
                .Range("A" & x & ":I" & x).Copy ws2.Range("A" & RLnRow)
            ElseIf Range("C" & x) = "31" Then 'Change C to the column your value of 39 is in
                .Range("A" & x & ":I" & x).Copy ws3.Range("A" & OCnRow)
            End If
        Next x
    End With
    
    Application.ScreenUpdating = True
    End Sub

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    That's precisely what I tried to do.
    Your code is using entirerow.copy

  11. #11
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    Thank you sintek, it worked like a charm.

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    Glad I could help....Please feel free to add to Reputation...thanks

  13. #13
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    Just one last thing - it seems that the macro is copying values for RL sheets only without tab OC. Wonder what might be causing that.

  14. #14
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    What do you mean...it only copies to Tab OC if value = 31....Can you upload a sample workbook

  15. #15
    Registered User
    Join Date
    02-24-2017
    Location
    France
    MS-Off Ver
    2007
    Posts
    86

    Re: Macro for copying not specific columns

    I am so sorry, must've made some manual mistake, macro is working flawlessly. Much appreciated.

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,505

    Re: Macro for copying not specific columns

    macro is working flawlessly.
    Do i deserve a rep point

+ 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. Copying a pasting specific columns
    By Throughstream in forum Excel General
    Replies: 1
    Last Post: 01-21-2017, 07:16 PM
  2. [SOLVED] Macro copying specific columns by date range.
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-14-2015, 08:43 PM
  3. Need help with specific macro script for data copying in a specific format
    By Amit1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-08-2014, 12:36 AM
  4. Copying specific data to a specific field in a worksheet macro
    By bradpeh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2013, 01:52 AM
  5. Replies: 8
    Last Post: 04-04-2013, 08:02 PM
  6. Copying specific columns
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-20-2010, 10:15 AM
  7. Copying data to specific columns
    By chalky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-06-2005, 08:05 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