+ Reply to Thread
Results 1 to 7 of 7

Code to copy data from another workbook where offset cell equals "none"

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Code to copy data from another workbook where offset cell equals "none"

    My workbook stores data related to items that are all tracked by barcodes. Im using the following code (as part of a larger set of code) to copy data from rows 6 on in column D, E & G to K from one workbook to another. The data in F (Item Names) is found using a formula, by matching the barcode data in the corresponding G cell (on the same row) to another sheet (where the barcodes and Item names are stored). When the data has finished copying over and EnableEvents gets turned back on the F column mostly automatically fills itself in from the formula. The issue I have however is in certain circumstances there are some items that either dont have a barcode, or the barcode is missing. In this case the user types "none" (not case sensitive), which opens a userform allowing the user to type in an Item name that gets entered into the F cell, overwriting the formula. I need to copy this data over separately to the new workbook. Im looking for code to find these records from row 6 on (either by searching for rows where G equals "none", or rows where F has no formula, and then copy the data in F over to the other workbook to the corresponding row number but offset by the number of records already present in the active workbook (currently being calculated by wbReturnDataLastRow).

        Dim wb As Workbook
        Dim mybook As Workbook
        
        
        Set wb = ActiveWorkbook
        Set mybook = Workbooks("ERL.xlsm")
                            Dim mybookReturnDataLastRow As Long
                            Dim wbReturnDataLastRow As Long
                        
                            mybookReturnDataLastRow = mybook.Worksheets("ReturnData").Range("G6").End(xlDown).Row
                            wbReturnDataLastRow = wb.Worksheets("ReturnData").Cells(Rows.Count, "G").End(xlUp).Offset(1).Row
                              
                                With mybook.Worksheets("ReturnData")
                                   .Range("D6", .Range("E" & mybookReturnDataLastRow)).Copy
                                   wb.Worksheets("ReturnData").Range("D" & wbReturnDataLastRow).PasteSpecial xlPasteValues
                                   .Range("G6", .Range("K" & mybookReturnDataLastRow)).Copy
                                   wb.Worksheets("ReturnData").Range("G" & wbReturnDataLastRow).PasteSpecial xlPasteValues
                                End With
    wb is the ActiveWorkbook that the data is being copied to
    mybook is the workbook that the data is being copied from

    Thanks,
    James

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,704

    Re: Code to copy data from another workbook where offset cell equals "none"

    You can use HasFormula property and check all cells in range

    like:
    dim mycell as range
    for each mycell in mybook.sheets("sheet with data").Range("your column with some non-formulas")
      if not mycell.hasformula then mycell.copy activesheet.range("here destination")
    next mycell
    application.cutcopymode=false
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to copy data from another workbook where offset cell equals "none"

    Thanks Kaper. I tried the following, and while it proceeded with no errors, it didn't copy anything. It just went to the first available cell in the F column and removed the formula:

                                Dim mycell As Range
                                
                                With mybook.Worksheets("ReturnData")
                                For Each mycell In .Range("F6" & mybookReturnDataLastRow)
                                    If Not mycell.HasFormula Then mycell.Copy
                                    wb.Worksheets("ReturnData").Range("F" & wbReturnDataLastRow).PasteSpecial xlPasteValues
                                Next mycell
                                End With
                                Application.CutCopyMode = False
    If this code was working would it copy to the correct corresponding cells on the active workbook? The data should be copied to the same row number on the Active workbook, offset by the number of rows currently already present. So for example if active workbook (wb) contains 4 records then if data is copied from row 20 (on my book) then it should be pasted into row 24.
    Thanks,
    James

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,704

    Re: Code to copy data from another workbook where offset cell equals "none"

    Hi James,

    1) I think Range was not correct:
    Range("F6:F" & mybookReturnDataLastRow)
    2) you copied only if .Hasformula but pasted always
    3) pasted all the time to the same location Range("F" & wbReturnDataLastRow) I added increasing of the row number. Try such code

    With mybook.Worksheets("ReturnData")
      if mybookReturnDataLastRow < 6 Then Msgbox "mybookReturnDataLastRow = " & mybookReturnDataLastRow , vbcritical
       For Each mycell In .Range("F6:F" & mybookReturnDataLastRow)
          If Not mycell.HasFormula Then 
            mycell.Copy
            wb.Worksheets("ReturnData").Range("F" & wbReturnDataLastRow).PasteSpecial xlPasteValues
            wbReturnDataLastRow = wbReturnDataLastRow +1      
          end if  
       Next mycell
    End With

  5. #5
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to copy data from another workbook where offset cell equals "none"

    Thanks Kaper, that correctly copies all the cells in range that dont contain a formula, however its just pasting them into the first available F cell, all in a line together in that column, rather than pasting them in the corresponding row numbers (offset by number of existing rows).

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,704

    Re: Code to copy data from another workbook where offset cell equals "none"

    As quite often, attached sample workbook with some input data and shown expected "manually created" result would help a lot. If the below does not help, I honestly suggest you post one.

    With mybook.Worksheets("ReturnData")
       For Each mycell In .Range("F6:F" & mybookReturnDataLastRow)
          If Not mycell.HasFormula Then 
            mycell.Copy
            wb.Worksheets("ReturnData").Range("F" & wbReturnDataLastRow + mycell.row).PasteSpecial xlPasteValues
            end if  
       Next mycell
    End With
    I removed
      if mybookReturnDataLastRow < 6 Then Msgbox "mybookReturnDataLastRow = " & mybookReturnDataLastRow , vbcritical
    because seeing just a part of the code I was not sure if mybookReturnDataLastRow is set at all, which could lead to strange behaviour.
    So it would raise alert if there is a problem.

  7. #7
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to copy data from another workbook where offset cell equals "none"

    Sorry I should have posted a sample workbook from the beginning.
    That worked great. I just had to tweak the calculation to find the row number to paste to, as both of those items (wbReturnLastRow and mycell) were factoring in the title rows, so I just added -6 and then it lined up perfectly.
    Thanks,
    James

+ 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. VBA code for copy data from closed workbook to "Data " sheet of current workbook
    By satputenandkumar0 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-13-2014, 02:23 AM
  2. Replies: 1
    Last Post: 05-23-2013, 02:04 PM
  3. [SOLVED] Delete row if cell in Column N equal "Different" or Column P equals "Blank"
    By Cyberpawz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2012, 08:25 AM
  4. Replies: 11
    Last Post: 05-15-2012, 01:22 PM
  5. Code to not print row if cell equals "x"
    By DarthMinogue in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2011, 07:44 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