Consulting

Results 1 to 17 of 17

Thread: Moving & copying between two workbooks

  1. #1

    Moving & copying between two workbooks

    Hello!

    I am trying to copy some cells from one workbook to another. Here is the peace of code:

    For i = 1 to 100
    If Cells(i, "A").Value = "Start" Then
    Cells(i, "A").Select
    Cells(i, "A").Copy_ Destination:=Workbooks(2).Sheets(1).Range("A1").End(xlDown)
    End If
    Next i

    I want to copy entire row of finded cell. Any help with this?

    My other questions are:

    1. is there some simple way to copy to closed workbook?
    2. After founding "Start" value, I want to search for End value and copy whole range between them. How to do that????

    Thank you for any help!
    Last edited by Sephir; 06-27-2007 at 02:36 PM.

  2. #2
    Any help with this?

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    To be clear on what you're trying to do....you want to search column A from top to bottom and copy all rows that have data in column A to a different sheet?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Quote Originally Posted by lucas
    To be clear on what you're trying to do....you want to search column A from top to bottom and copy all rows that have data in column A to a different sheet?
    i want to seach column A, starting at "Start" to bottom, copy all rows and finish on row, with "End" in column A.

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Not tested. [vba]Sub StartEnd()
    Dim rng As Range
    Dim result, result2
    Dim firstaddress As String
    Dim vmessage As String
    Dim wb As Workbook
    Dim current As Workbook
    Dim sht As Worksheet
    Dim rng2 As Range

    Set current = ActiveWorkbook
    Set wb = Workbooks.Add
    Set sht = wb.Sheets(1)
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    current.Activate
    Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    With rng
    Set result = .Find("Start", LookIn:=xlValues)
    If Not result Is Nothing Then
    firstaddress = result.Address
    Do
    vmessage = vmessage & "- start : " & result.Row & vbCrLf
    Set result = .FindNext(result)
    Loop While Not result Is Nothing And result.Address <> firstaddress
    End If
    Set result2 = .Find("End", LookIn:=xlValues)
    If Not result2 Is Nothing Then
    firstaddress = result2.Address
    Do
    vmessage = vmessage & "- End : " & result2.Row & vbCrLf
    Set result2 = .FindNext(result)
    Loop While Not result2 Is Nothing And result2.Address <> firstaddress
    End If
    End With
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Copy rng2
    End Sub[/vba]vmessage ins't really necessary. Dimming and building of vmessage can be deleted.

  6. #6
    VBAX Regular
    Joined
    Mar 2007
    Posts
    47
    Location
    hey. thanx for the code, but it is not working... it starts and then just sits there forever... any help? Sephir did you managed to get it working?
    Last edited by CicoMico; 07-12-2007 at 09:43 AM.

  7. #7
    Quote Originally Posted by CicoMico
    hey. thanx for the code, but it is not working... it starts and then just sits there forever... any help? Sephir did you managed to get it working?
    No, I did not. Any help, somebody? Please...

  8. #8
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Works fine for me. In column A I have in A3 the word start and in A10 the word end. Now, everything in between those two words is copied to a new created workbook. Instead of creating one, you could easily change the coding to open an existing workbook.

  9. #9
    Quote Originally Posted by Charlize
    Works fine for me. In column A I have in A3 the word start and in A10 the word end. Now, everything in between those two words is copied to a new created workbook. Instead of creating one, you could easily change the coding to open an existing workbook.
    Hmmm... That is strange! I've created new wb with these datas: A1 - Start, A10 - End; A20 - Start, A25 - End; inserted Your code into new module and run. Excel stopped responding and freezes... Any suggestions?

  10. #10
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    The previous codes weren't for multiple copies of data between start and end. Try this suggestion instead.[VBA]Sub StartEnd()
    Dim rng As Range
    Dim result, result2
    Dim wb As Workbook
    Dim current As Workbook
    Dim sht As Worksheet
    Dim rng2 As Range

    Set current = ActiveWorkbook
    Set wb = Workbooks.Add
    Set sht = wb.Sheets(1)
    current.Activate
    Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    Do
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    With rng
    Set result = .Find("Start", LookIn:=xlValues)
    If Not result Is Nothing Then
    Set result2 = .Find("End", LookIn:=xlValues)
    Else
    Exit Do
    End If
    End With
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Copy rng2
    Set rng = Range(Cells(result2.Row, result2.Column), _
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
    Loop While result2.Row < Range("A" & Rows.Count).End(xlUp).Row
    End Sub[/VBA]

  11. #11
    hey! thanx for helping and for your patience. but unfortunately it is still not working for me. I am posting sample wb with source and results... please help!

  12. #12
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    This will do it. It's because you putted a Start in A1. First time looking when using Find will skip A1.[VBA]Option Explicit
    Option Compare Text
    Sub StartEnd()
    'starting range
    Dim rng As Range
    'store lookup results
    Dim result, result2
    'workbook that we use
    Dim current As Workbook
    'sheet to copy to
    Dim sht As Worksheet
    'range to copy to
    Dim rng2 As Range

    Set current = ActiveWorkbook
    Set sht = current.Worksheets("Result")
    current.Activate
    Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    'if destination A1 <> empty
    If sht.Range("A1") <> "" Then
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    Else
    Set rng2 = sht.Range("A1")
    End If
    'Because find function will skip A1 when starting
    'we have to check it by using an if construction
    If Range("A1").Value = "Start" Then
    Set result = Range("A1")
    Set result2 = rng.Find("End", LookIn:=xlValues)
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 3).Copy rng2
    Set rng = Range(Cells(result2.Row, result2.Column), _
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
    End If
    Do
    'if destination A1 <> empty
    If sht.Range("A1") <> "" Then
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    Else
    Set rng2 = sht.Range("A1")
    End If
    With rng
    Set result = .Find("Start", LookIn:=xlValues)
    'if start is found, we look for End
    'else exit the loop to avoid errors when searching for End
    If Not result Is Nothing Then
    Set result2 = .Find("End", LookIn:=xlValues)
    Else
    Exit Do
    End If
    End With
    'resize the range to be copied from 1 to 3 columns
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 3).Copy rng2
    Set rng = Range(Cells(result2.Row, result2.Column), _
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
    'if rowno of result2 (End) < total number of rows, we check again on
    'start value
    Loop While result2.Row < Range("A" & Rows.Count).End(xlUp).Row
    End Sub
    [/VBA]

  13. #13
    Quote Originally Posted by Charlize
    This will do it. It's because you putted a Start in A1. First time looking when using Find will skip A1.[vba]Option Explicit
    Option Compare Text
    Sub StartEnd()
    'starting range
    Dim rng As Range
    'store lookup results
    Dim result, result2
    'workbook that we use
    Dim current As Workbook
    'sheet to copy to
    Dim sht As Worksheet
    'range to copy to
    Dim rng2 As Range

    Set current = ActiveWorkbook
    Set sht = current.Worksheets("Result")
    current.Activate
    Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    'if destination A1 <> empty
    If sht.Range("A1") <> "" Then
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    Else
    Set rng2 = sht.Range("A1")
    End If
    'Because find function will skip A1 when starting
    'we have to check it by using an if construction
    If Range("A1").Value = "Start" Then
    Set result = Range("A1")
    Set result2 = rng.Find("End", LookIn:=xlValues)
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 3).Copy rng2
    Set rng = Range(Cells(result2.Row, result2.Column), _
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
    End If
    Do
    'if destination A1 <> empty
    If sht.Range("A1") <> "" Then
    Set rng2 = sht.Range("A" & sht.Range("A" & Rows.Count).End(xlUp).Row + 1)
    Else
    Set rng2 = sht.Range("A1")
    End If
    With rng
    Set result = .Find("Start", LookIn:=xlValues)
    'if start is found, we look for End
    'else exit the loop to avoid errors when searching for End
    If Not result Is Nothing Then
    Set result2 = .Find("End", LookIn:=xlValues)
    Else
    Exit Do
    End If
    End With
    'resize the range to be copied from 1 to 3 columns
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 3).Copy rng2
    Set rng = Range(Cells(result2.Row, result2.Column), _
    Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
    'if rowno of result2 (End) < total number of rows, we check again on
    'start value
    Loop While result2.Row < Range("A" & Rows.Count).End(xlUp).Row
    End Sub
    [/vba]
    perfect! thanx! just one question... i have added some colums and it gives me: Run-time error: '1004'. The information cannot be pasted becouse the copy area and the paste area are not the same size and shape...
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 3).Copy rng2

    i changed 3 to 5, and same error...
    Range(Cells(result.Row, 1), Cells(result2.Row, 1)).Resize(, 5).Copy rng2

    any help with this? thanx

  14. #14
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Post your workbook exactly formatted as you would use it. Probably something to do with merged cells.

  15. #15
    Quote Originally Posted by Charlize
    Post your workbook exactly formatted as you would use it. Probably something to do with merged cells.
    unfortunately i can't. it's part of unreleased project... anyway, i've searched for merged cells, and no merged cells in my wb... it has 1323 rows and 5 colums.

  16. #16
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location

  17. #17
    Quote Originally Posted by Charlize
    hmmm... still the same error...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •