+ Reply to Thread
Results 1 to 6 of 6

Copy Part Of Row Instead Of Whole Row

  1. #1
    Registered User
    Join Date
    11-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    8

    Copy Part Of Row Instead Of Whole Row

    I have a macro which matches a value in CV1 with every row in column cu if a match is found the whole row is copied to Sheet 5. This part works perfectly. Unfortunately I only want to copy the range(a:CS) so as not to overwrite existing information in sheet 5 after column cs. I have been unable to figure out how to do this and it is driving me crazy. I hope someone can help keep my sanity.

    Thank you

    Ted


    Macro

    Sub test()



    Dim LSearchRow As Integer
    Dim LCopyToRow As Integer
    Dim LSearchValue As String
    Dim LSearchValue1 As String




    LSearchValue1 = Range("CV1").Value





    'Start search in row 2
    LSearchRow = 2

    'Start copying data to row 2 in First (row counter variable)
    LCopyToRow = 2


    Sheets("All_Data").Activate

    LSearchValue1 = Range("CV1").Value



    '
    '
    ' 5
    '
    '


    Worksheets("5").Range("A2:cs1093").ClearContents


    'LSearchValue = concatenate(LSearchValue & "1")




    While Len(Range("A" & CStr(LSearchRow)).Value) > 0

    'If value in column C = LSearchValue, copy entire row First

    If Range("CU" & CStr(LSearchRow)).Value = LSearchValue1 Then
    'Select row in All1to4 to copy

    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
    Selection.Copy

    'Paste row into First in next row
    Sheets("5").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste


    Sheets("5").Select
    Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
    ActiveSheet.Paste

    'Move counter to next row
    LCopyToRow = LCopyToRow + 1

    'Go back to All_Data to continue searching
    Sheets("All_Data").Select


    End If

    LSearchRow = LSearchRow + 1

    Wend
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Copy Part Of Row Instead Of Whole Row

    Why do you not simply use Autofilter:Test2.xlsm
    Last edited by kaasplankje; 12-08-2023 at 06:24 PM.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,541

    Re: Copy Part Of Row Instead Of Whole Row

    You don't need that many lines in your code to get this done but first explain in a concise way what you want to achieve and secondly, get used to using code tags because it makes code so much easier to read.
    If I understand it right, you want to check Column CU for a match of the value in Range CV1. If a match is found, copy that row from the first column to column CS to the first available (free) row in Sheet 5. Is that right?
    You can do what kaasplankje suggested or do it with a loop which with todays computers is not all that slow anymore. It obviously will depend on the amount of data to be checked.
    Please Login or Register  to view this content.
    Change references where required.

    If it is only values that need to be copied/pasted, this is more efficient.
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    Check all references because we have no way of checking.
    Last edited by jolivanes; 12-08-2023 at 06:46 PM.
    Experience trumps academics every day of the week and twice on Sunday.

  4. #4
    Registered User
    Join Date
    11-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Copy Part Of Row Instead Of Whole Row

    I am not a VBA expert. This is actual data from my spreadsheet/database. The information is actual. Column CU is Column CU which is column 99. If the information in column CU matches the information in CV1 the information in that row from A:CS should be copied to sheet 5 starting with Row 2. As it stands now test works but it copies the whole row and I do not want the whole row. I have no doubt autofilter would work; I have not figured out how to make it work. The For Loop as a standalone did not transfer any rows. Is the for Loop meant to be standalone or is it to replace something I had in test?

    Thank you both

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,541

    Re: Copy Part Of Row Instead Of Whole Row

    Please Login or Register  to view this content.
    Works on your attachment. As mentioned previously, you always have to change references if not all info is available.

    Part of the fives ("5") are numerical while others are text.
    The right aligned are numbers while the left aligned are text.

    However, like kaasplankje mentioned, filtering is the best way. Fixing the text/numerical makes things easier.
    I am sure he/she will be showing up soon to give you a suggestion.

    And this works also. Probably as fast or faster then filtering.
    Please Login or Register  to view this content.
    Last edited by jolivanes; 12-10-2023 at 03:32 AM.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,299

    Re: Copy Part Of Row Instead Of Whole Row

    Please Login or Register  to view this content.
    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 star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

+ 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] Copy only part of a cell
    By captbluefin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-17-2022, 12:02 PM
  2. [SOLVED] Copy part of a column to part of a row?
    By terrypin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-03-2020, 03:48 PM
  3. Copy Part of Worksheet to *.jpg
    By Vladamir in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2012, 10:19 AM
  4. Copy data from one part# on sheet 2 to the same Part# on sheet 1
    By sald in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2009, 10:24 PM
  5. Copy Part of String
    By Desi_bhai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-16-2007, 03:03 PM
  6. Transpose copy: Second Part
    By Mika in forum Excel General
    Replies: 1
    Last Post: 05-31-2006, 07:45 AM
  7. Copy Cell Help Part 2!!
    By rhani111 in forum Excel General
    Replies: 0
    Last Post: 02-22-2005, 08:34 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