Consulting

Results 1 to 3 of 3

Thread: Solved: Moving cell data

  1. #1
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location

    Solved: Moving cell data

    Hello

    This bit of code works well to move some data from column A one down from the word DONOR to column b one across and one up.
    Only part is shown
    [vba]
    Option Explicit
    Sub GetthemDONORS1()
    Range("B:B").Insert Shift:=xlToLeft
    Dim Rng As Range
    Dim c As Range
    Dim testvalue1

    testvalue1 = "DONOR"
    Set Rng = Range("A1:A" & Range("A10000").End(xlUp).Row)
    For Each c In Rng
    If c.Value = testvalue1 Then
    c.Offset(1, 0).Cut c.Offset(0, 1)
    End If
    Next c
    [/vba]

    If I try to add another search like this

    [vba]Option Explicit
    Sub GetthemDONORS2()
    Range("B:B").Insert Shift:=xlToLeft
    Dim Rng As Range
    Dim c As Range
    Dim testvalue1
    Dim testvalue2
    testvalue1 = "DONOR"
    testvalue2 = "NEW"
    Set Rng = Range("A1:A" & Range("A10000").End(xlUp).Row)
    For Each c In Rng
    If c.Value = testvalue1 Then
    c.Offset(1, 0).Cut c.Offset(0, 1)
    End If
    Next c
    For Each c In Rng
    If c.Value = testvalue2 Then
    c.Offset(0, 5).Copy c.Offset(0, -2)
    End If
    Next c
    [/vba]

    The code runs the first part ok but stops on the next search at
    c.Offset(0, 5).Copy c.Offset(0, -2)
    with an error message of ' Runtime error 1004 Application defined or object defined error.
    I dont know if I am trying to add this second search correctly so any help would be appreciated.
    I will want to add another search to the finished job if my code so far is going in the right direction.

    Cheers
    Gil

  2. #2
    This line
    [vba]c.Offset(0, 5).Copy c.Offset(0, -2) [/vba] will copy the cell's contents 2 columns to the left (because column index is -2).
    As there is nothing to the left of column A, this leads to an error.
    Perhaps you wanted to use
    [vba]c.Offset(0, 5).Copy c.Offset(0, 2) [/vba]
    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  3. #3
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Jimmy
    I must have had my brain to the left of Column A. All is working as I require with my addition inserted as well.
    Many thanks
    Gil

Posting Permissions

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