+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Copy/paste code

  1. #1
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Copy/paste code

    Hello,

    So I have two files and need to copy the information from columns B,C,F,H rows 5 true 29, and past it in the other file in Columns B,D,G,J; rows 3 true 35.

    The code that I have works fine, but have one problem - it past the info in the rows starting from 37 instead from row 3. Here is where I need your help. The code is triggered when the condition Arr ( arrived) is chosen in the column AH ( file Delivery Status) and then the specific warehouse is chosen in the column AI. To make it easy I have left only one warehouse as a choice.

    Please advise.

    Attached you can find sample files ( I have delete the info from the heading cells as it was in another lenguage)
    Attached Files Attached Files

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    re: Copy/paste code

    Hi,

    It seems to work fine for me with the two files you attached. The only reason I can see that would make the macro start at row 37 is if B36 of the relevant Warehouse sheet was non blank.

    As an aside, it may be more appropriate to use Data Filter Advanced and filter the Delivery Status workbook using a Criteria where Status = "Arr" and Destination not blank, and have the data output fields in the same order as the Warehouse sheets. Then you could simply copy and paste the whole block in one go.

    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    re: Copy/paste code

    Are you sure that the info is copied where I need it. Becouse when I try
    the code paste the info from B5 to B3, that is where I need it, but the rest of the cells are pasted below my table, starting from row 37.

    What you suggest about the auto filter does not suit my needs. The information that you see in the Delivery status file is an example, I am entering the data every month starting from B5 and i need once i fill the row the information to be copied to the other file, I am not coping a block of information, but singel rows every day, becouse based on that I am calculating other things on a daily basis.

    Please refer to the files that I am attaching now and you will se just the first row filled in and then check the Warehouse file to see where the code paste the data.

    Any other suggestion?

    Please i need you hep guys!
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    re: Copy/paste code

    Acctualy I found what is cousing that problem, but do not know what the resolution would be.

    Because my table has formula at the end, Row 36, summing the quantities from rows 3:35, the code paste the data after that row 36 because detaches that row is nonblank.
    If I remove the formula and row 36 is empty, then the code works fine and paste the data in the right place, starting from row 3.

    So, what can I do to tell the code to start from row 3 even if I have something in the row 36?

    Please Help!!

  5. #5
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    re: Copy/paste code

    Please Help

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    re: Copy/paste code

    Hello Bob@Sun,

    I revised your macro and it has been added to the attached workbook. You will need to change the source folder back to your own. Here is the macro code...
    
      Dim DataRng As Range
      Dim DstPath As String
      Dim DstRng As Range
      Dim DstWkb As Workbook
      Dim DstWks As Worksheet
      Dim NR As Long
      Dim R As Long
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = ThisWorkbook.ActiveSheet
        Set DataRng = SrcWks.Range("B5:AF29")
        
         'DstPath = "C:\Documents and Settings\bkostadinov\My Documents\project"
         DstPath = "C:\Documents and Settings\Admin.ADMINS\My Documents\Excel Forum Folders\Bob@Sun"
          
         'Make sure the folder path ends with a backslash
          DstPath = IIf(Right(DstPath, 1) <> "\", DstPath & "\", DstPath)
          
          On Error Resume Next
           'ignore potential error and resume execution on the next line of code
            Set DstWkb = Workbooks("Warehouse.xls")
           'this makes sure that if the workbook, worksheet does not exist, error 9 -Subscript Out of Range, it will creat it
           'the code below will creat the workbook if does not exist
            If Err = 9 Then
               Set DstWkb = Workbooks.Open(DstPath & "Warehouse.xls")
               Err.Clear
            End If
          On Error GoTo 0
        
          R = Target.Row - DataRng.Row + 1
         
          Set DstWks = DstWkb.Worksheets(Target.Value)
          
         'Find the next empty row in the range "B3:B35"
          Set DstRng = DstWks.Range("B3:B34")
          Set RngEnd = DstRng.Cells(DstRng.Rows.Count, 1).End(xlUp)
          NR = IIf(RngEnd.Row < DstRng.Row, DstRng.Row, RngEnd.Row + 1)
          
          With DstWks
            .Cells(NR, "B").Value = DataRng.Cells(R, 1).Value
            .Cells(NR, "D").Value = DataRng.Cells(R, 2).Value
            .Cells(NR, "G").Value = DataRng.Cells(R, 5).Value
            .Cells(NR, "J").Value = DataRng.Cells(R, 7).Value
          End With
          
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    re: Copy/paste code

    Hi Bob@Sun
    I've revised the definition of "LR" in your macro from this
     LR = DstWks.UsedRange.Rows.Count + 1
    to this
    LR = DstWks.Cells(1, 4).End(xlDown).Row + 1
    such that it pastes data on the first blank row from the top of the range. However, in my mind, it appears to be pasting incorrect information in the wrong cells. But, again in my mind, your original procedure appears to do the same. So, you need to figure out this anomaly or let me know what should go where.
    The line of code I changed assumes there is some entry in your merged cells "DEF", GHI", "JKLM" and NO" in the Warehouse worksheet. You'll see that I've added data there. If your "Real" worksheets have data in these cells, you should be good to go.
    Another issue that probably needs addressed is that LR is based on column D of the warehouse you provided. This will probably not work in all warehouses. LR needs to know the last occupied row in each warehouse. Perhaps column A can be used for this purpose. I'd need to know more about your project to address this issue.
    Again, the ONLY changes I made is the one line of code and populating the merged cells mentioned above.
    Let me know if what's been provided works as expected. John
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    re: Copy/paste code

    Thanks guys !

    Combining your suggestions I ended up with the following and that works perfect for me. It paste the information within the range wanted.

    R = Target.Row - DataRng.Row + 1
         
          Set DstWks = DstWkb.Worksheets(Target.Value)
          Set DstRng = DstWks.Range("B3:O35")
                       LR = DstRng.Rows.Count + 1
           DataRng.Cells(R, 1).Resize(1, 1).Copy DstRng.Cells(LR, 1).End(xlUp).Offset(1, 0)
           DataRng.Cells(R, 2).Resize(1, 1).Copy DstRng.Cells(LR, 16).End(xlUp).Offset(1, 0)
           DataRng.Cells(R, 5).Resize(1, 1).Copy DstRng.Cells(LR, 3).End(xlUp).Offset(1, 0)
          DataRng.Cells(R, 7).Resize(1, 1).Copy DstRng.Cells(LR, 4).End(xlUp).Offset(1, 0)
          DataRng.Cells(R, 9).Resize(1, 1).Copy DstRng.Cells(LR, 9).End(xlUp).Offset(1, 0)

    But now another problem came up.

    Let's consider the following example: ( see the attached files )

    On the 16-Nov I am filling in information the Columns "F" and "H"
    On the 17-Nov I am filling in information in the Column "F" and "J", file Delivery Status, and because the Code is looking for the first blank cell in the column the data from the column J goes in the cell J3 in the file Warehouse instead of in the cell J4.

    And Second Problem is that I need the information from the Delivery Status File to be pasted in the other file without the formating, without the colour.


    I will apreciate any suggestion!!
    Thanks!
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    re: Copy/paste code

    Please Help!!!

  10. #10
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    re: Copy/paste code

    Quote Originally Posted by Bob@Sun View Post
    Please Help!!!
    Where's the bug. The only problem I can see is that the start of the xlup copy is row 45 which puts it below the last row of the warehouse sheet. Is that the bug to which you refer?

    Rgds
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  11. #11
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Copy/paste code

    Where's the bug. The only problem I can see is that the start of the xlup copy is row 45 which puts it below the last row of the warehouse sheet. Is that the bug to which you refer?
    That was the problem before. Now the problem is the following:

    Let's consider the following example: ( see the attached files )

    On the 16-Nov I am filling in information the Columns "F" and "H"
    On the 17-Nov I am filling in information in the Column "F" and "J", file Delivery Status, and because the Code is looking for the first blank cell in the column the data from the column J goes in the cell J3 in the file Warehouse instead of in the cell J4.

    And Second Problem is that I need the information from the Delivery Status File to be pasted in the other file without the formating, without the colour.
    I will be happy if you can help, and sorry if I did not name the post right.


    Cheers!

  12. #12
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Copy/paste code

    Hi Bob@Sun
    Try this code
    Option Explicit
    Sub CopyDeliveryData(Target As Range)
    
        Dim DataRng As Range
        Dim DstPath As String
        Dim DstRng As Range
        Dim DstWkb As Workbook
        Dim DstWks As Worksheet
        Dim NR As Long
        Dim R As Long
        Dim RngEnd As Range
        Dim SrcWks As Worksheet
        Dim LR As Long
    
        Application.ScreenUpdating = False
        Set SrcWks = ThisWorkbook.ActiveSheet
        Set DataRng = SrcWks.Range("B5:AF29")
    
        'DstPath = "C:\Documents and Settings\bkostadinov\My Documents\project"
        DstPath = "C:\Documents and Settings\Bob@Sun\Desktop\New Folder\TEST"
    
        'Make sure the folder path ends with a backslash
        DstPath = IIf(Right(DstPath, 1) <> "\", DstPath & "\", DstPath)
    
        On Error Resume Next
        'ignore potential error and resume execution on the next line of code
        Set DstWkb = Workbooks("Warehouse.xls")
        'this makes sure that if the workbook, worksheet does not exist, error 9 -Subscript Out of Range, it will creat it
        'the code below will creat the workbook if does not exist
        If Err = 9 Then
            Set DstWkb = Workbooks.Open(DstPath & "Warehouse.xls")
            Err.Clear
        End If
        On Error GoTo 0
    
        R = Target.Row - DataRng.Row + 1
    
        Set DstWks = DstWkb.Worksheets(Target.Value)
        Set DstRng = DstWks.Range("B:B")
        LR = DstWks.Range("B" & Rows.Count).End(xlUp).Row + 1
    
    
        DataRng.Cells(R, 1).Resize(1, 1).Copy
        DstRng.Cells(LR, 1).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 2).Resize(1, 1).Copy
        DstRng.Cells(LR, 16).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 5).Resize(1, 1).Copy
        DstRng.Cells(LR, 3).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 7).Resize(1, 1).Copy
        DstRng.Cells(LR, 4).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 9).Resize(1, 1).Copy
        DstRng.Cells(LR, 9).PasteSpecial Paste:=xlPasteValues
        Application.ScreenUpdating = True
    
    End Sub
    and see if it does what you're looking for.
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  13. #13
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Copy/paste code

    This works fine now.

    There is only one thing. when the code copies the informaiton the whole row in the Delivery status file is marked like is being copied and the whole role in the other file is selected. I am attaching the two files to see what is happening if I am not explaining it well.

    Besides that, although I have this line included

    Application.ScreenUpdating = True
    the screen is blinking on coping.

    I like though the thing that the code focuses on the sheet where the information has been added. I my previous versio of the code this did not happened.

    John,

    I realy like what you did. It's doing the job very well. I am thanking you for your work!!!



    If we can clear those little problems now, that i mentioned above, it will be just perfect.
    Attached Files Attached Files

  14. #14
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Copy/paste code

    Hi Bob@Sun
    In looking at your code, it appears to be copying data from where it should to where it should. So, I can't help you with this issue
    the code copies the informaiton the whole row in the Delivery status file is marked like is being copied and the whole role in the other file is selected
    Regarding this issue
    the screen is blinking on coping.
    this code will fix that
    Sub CopyDeliveryData(Target As Range)
    
        Dim DataRng As Range
        Dim DstPath As String
        Dim DstRng As Range
        Dim DstWkb As Workbook
        Dim DstWks As Worksheet
        Dim NR As Long
        Dim R As Long
        Dim RngEnd As Range
        Dim SrcWks As Worksheet
        Dim DstWkbNm As String
    
    '******************************
    ' Add This Line
    
        Application.ScreenUpdating = False
    '******************************
    
        Set SrcWks = ThisWorkbook.ActiveSheet
        Set DataRng = SrcWks.Range("B6:BQ58")
    
    
        DstPath = "C:\Documents and Settings\Bob@Sun\Desktop\New Folder"
    
        'Make sure the folder path ends with a backslash
        DstPath = IIf(Right(DstPath, 1) <> "\", DstPath & "\", DstPath)
    
        On Error Resume Next
        'ignore potential error and resume execution on the next line of code
    
        Select Case Target.Value
        Case "Ester-Income"
            DstWkbNm = "Sklad-September09.xls"
        Case "Sofia-Income"
            DstWkbNm = "Sklad-September09.xls"
        Case "Kapelen-Income"
            DstWkbNm = "Sklad-September09.xls"
        Case "Drujba-Income"
            DstWkbNm = "Sklad-September09.xls"
        Case "Varna-Income"
            DstWkbNm = "Sklad-September09.xls"
    
        Case "IPK"
            DstWkbNm = "Zaiavka-September09.xls"
        Case "Standart"
            DstWkbNm = "Zaiavka-September09"
        Case "7 Dni"
            DstWkbNm = "Zaiavka-September09"
        Case "Drujba-Client"
            DstWkbNm = "Zaiavka-September09"
        Case "Ilinden 2000"
            DstWkbNm = "Zaiavka-September09"
        Case "IPK-Star Print"
            DstWkbNm = "Zaiavka-September09"
        Case "Maritsa"
            DstWkbNm = "Zaiavka-September09"
        Case "Kapelen-BG"
            DstWkbNm = "Zaiavka-September09"
        Case "Multiprint"
            DstWkbNm = "Zaiavka-September09"
        Case "Sega"
            DstWkbNm = "Zaiavka-September09"
        Case "Iconomedia"
            DstWkbNm = "Zaiavka-September09"
        Case "M Match"
            DstWkbNm = "Zaiavka-September09"
    
        End Select
    
    
        Set DstWkb = Workbooks(DstWkbNm)
        'this makes sure that if the workbook, worksheet does not exist, error 9 -Subscript Out of Range, it will creat it
    
        'the code below will creat the workbook if does not exist
        If Err = 9 Then
            Set DstWkb = Workbooks.Open(DstPath & DstWkbNm)
            Err.Clear
        End If
    
        On Error GoTo 0
    
        R = Target.Row - DataRng.Row + 1
    
        Set DstWks = DstWkb.Worksheets(Target.Value)
        Set DstRng = DstWks.Range("D:D")
        Set RngEnd = DstRng.Cells(DstRng.Rows.Count, 1).End(xlUp)
        NR = DstWks.Range("D" & Rows.Count).End(xlUp).Row + 1
    
        DataRng.Cells(R, 1).Resize(1, 1).Copy
        DstRng.Cells(NR, 1).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 2).Resize(1, 1).Copy
        DstRng.Cells(NR, 67).PasteSpecial Paste:=xlPasteValues
        DataRng.Cells(R, 5).Resize(1, 64).Copy
        DstRng.Cells(NR, 2).PasteSpecial Paste:=xlPasteValues
    
    '*************************************
    'Change this line
        Application.ScreenUpdating = True
    '*************************************
    
    
    End Sub
    Of course, you may loose this
    I like though the thing that the code focuses on the sheet where the information has been added.
    unless you select the target sheet in the macro.
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  15. #15
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Copy/paste code

    The Screen updating problem is solved. Thanks.

    About the other issue, probably you did not understand me or I did not explain it well. The data is copied where is neede, but the thing is that after the code copies it, the row that has been copied is highlited in both files. If I am not explaining it well, you can see the screenshots that I am attaching.

    One more thing, can you please tell me how to Select the target sheet
    Of course, you may loose this
    Code:
    I like though the thing that the code focuses on the sheet where the information has been added.

    unless you select the target sheet in the macro.
    John
    Thanks!

    Bobby

+ 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