+ Reply to Thread
Results 1 to 4 of 4

Copy and paste a cell into another cell according to its content

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2007
    Location
    London, UK
    Posts
    10

    Cool Copy and paste a cell into another cell according to its content

    Hi

    I need help with this.

    I need to copy contents of cells ( by rows) from one excel sheet to another. But the destination column of the cell will depend on the value of the cell being copied.

    Say the "recipient" sheet will have columns for products P1234EN, P1234ES, P1443EN, P1443FR etc and may be they are in columns H,I,J,K and in the next row a corresponding quantity

    The "donor" sheet will have a number of columns , each column predefined for one product as heading. I need to copy across from donor sheet to recipient sheet the quantity data from row by row placing each quantity under the correct product code column.
    I want to go down rows 1 to X until empty row is reached and look at value of B1 (which is P1234EN) move along to C1 and copy this number and paste it in the recipient sheets Column which matches B1. I will know in advance what the columns in the recipient sheet will be.

    Can this be done? is there a way to automate this?

    I have a similar VBA code which copies from one sheet to another row by row. But with that one I can specify in the code where each data will be copied to. Difference here is that where the data ends up is determined by the value of another cell.

    Thanks for any advice.

    Talāt

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Copy and paste a cell into another cell according to its content

    Hi Talat,

    Try this:

    Option Explicit
    Sub Macro1()
    
        Dim lngEndRow As Long, _
            lngMyRow As Long
        
        lngEndRow = Sheets("recipient").Cells(Rows.Count, "B").End(xlUp).Row
        
        Application.ScreenUpdating = False
        
        For lngMyRow = 1 To lngEndRow
            'If the uppcase value of the entry in row 'lngMyRow' in Col. B of the 'recipient' tab is P1234EN, then...
            If StrConv(Sheets("recipient").Range("B" & lngMyRow), vbUpperCase) = "P1234EN" Then
                '...copy the value in Col. C of that row to the same row number in Col. B of the 'donor' tab.
                Sheets("recipient").Range("C" & lngMyRow).Copy Destination:=Sheets("donor").Range("B" & lngMyRow)
            End If
        
        Next lngMyRow
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    06-20-2007
    Location
    London, UK
    Posts
    10

    Re: Copy and paste a cell into another cell according to its content

    Thanks for the response Robert.

    I have only just seen this. I was waiting to receive an email alert saying that there was a reply to my post, but for some reason I never received it.
    I will try your suggestion and come back to you. I am very grateful to you for taking the time to respond and make this suggestion.

    Talk later.

    Talāt

  4. #4
    Registered User
    Join Date
    06-20-2007
    Location
    London, UK
    Posts
    10

    Re: Copy and paste a cell into another cell according to its content

    Hi Robert and anyone else who is willing to help out,

    I tried Robert's suggestion above, but I guess due to my lack of VBA knowledge I did ot get very far. So I decided to have a different approach but this time I got stuck with the If/Elseif Statements. If anyone can sort me out with this, I think I am 90% there. I may get stuck again in the last part, but I will cry for help then.

    Here is my Code which I think it will work once I sort out the if statements ( and add many more of them)

    Thanks for your help and suggestions.

    Talat



    
    Sub PrintRoom()
         
       Application.ScreenUpdating = False
       
       Dim myRange      As String
       Dim InvNum        As Long     'Despatch Note Number
       Dim InvSheet      As Worksheet
       Dim PrintRmSheet  As Worksheet
       Dim NextRow       As Long     'the next available invoice row on the PrintRmSheet
       Dim oRow          As Long     'row number on PrintRmSheet
       Dim iRow          As Long     'row number on InvSheet
       Dim DestCol      As String    'Column Name on PrintRmSheet
       
       
       Set InvSheet = ThisWorkbook.Worksheets("INVOICE TEMPLATE")
       
       
        Workbooks.Open Filename:="G:\PUBS\PP-MS\INVOICES\PrintRmData.xlsx"
            
        Set PrintRmSheet = ActiveWorkbook.Worksheets("Sheet1")
       
       oRow = PrintRmSheet.UsedRange.Rows.Count + 1
       iRow = 20
     
          
        Do
         
        
            InvSheet.Range("K2").Copy  'DN Number
            PrintRmSheet.Cells(oRow, "A").PasteSpecial xlPasteValues
            InvSheet.Range("B6").Copy  'Section Name
            PrintRmSheet.Cells(oRow, "B").PasteSpecial xlPasteValues
           
            
          
          If InvSheet.Cells(iRow, "D") = "P4068EN" Then DestCol = "K"
          ElseIf InvSheet.Cells(iRow, "D") = "P4063EN" Then DestCol = "H"
          ElseIf InvSheet.Cells(iRow, "D") = "P4069MU" Then DestCol = "J"
     
          'Need to add more Else if conditions here
          
          End If
          InvSheet.Cells(iRow, "E").Copy
          PrintRmSheet.Cells(oRow, DestCol).PasteSpecial xlPasteValues
              
          
                   
        
       
          iRow = iRow + 1
          'oRow = oRow + 1
       
       
       
       
       Loop Until IsEmpty(InvSheet.Cells(iRow, "D")) Or InvSheet.Cells(iRow, "D") = F
       
      
       
       
       Application.CutCopyMode = False
       ActiveWorkbook.Close True           'save changes and close
       
       Application.ScreenUpdating = True
         
    End Sub

+ 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] [Solved] Conditional copy/paste cell content from one sheet to another
    By chng87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2014, 06:01 AM
  2. Copy cell content to clipboard to paste into a pdf
    By kevinm3u in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2014, 03:30 PM
  3. [SOLVED] Copy cell A1 and paste value into B1 if B1 has content, paste to B2...etc
    By mhopke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2012, 09:39 PM
  4. [SOLVED] Copy and paste row based on cell content
    By BDan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2006, 11:20 AM
  5. [SOLVED] How do I copy a formula/paste w/o losing the cell content?
    By CShannon in forum Excel General
    Replies: 6
    Last Post: 06-25-2005, 08:05 AM

Tags for this Thread

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