Consulting

Results 1 to 10 of 10

Thread: Solved: With-End With problem

  1. #1

    Solved: With-End With problem

    I changed this
    [VBA]
    wsTarget.Range("A4:T" & lrTarget).Delete
    wsTarget.Range("A4:T" & lrTarget).Value = wsSource.Range("A4:T" & lrSource).Value
    wsTarget.Range("A4:T" & lrTarget)..Font.Bold = False
    [/VBA]

    To this
    [VBA]
    With wsTarget.Range("A4:T" & lrTarget)
    .Delete
    .Value = wsSource.Range("A4:T" & lrSource).Value
    .Font.Bold = False
    End With
    [/VBA]

    But this line
    [VBA]
    .Value = wsSource.Range("A4:T" & lrSource).Value
    [/VBA]

    is giving me an error-
    run time 424
    Object required

    Any ideas why this is not a valid block?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Doug

    You're deleting the range object immediately before you try and use it's value property.

  3. #3
    Norie,
    I dont think so. But I say this cautiously....
    Let me repost.
    I am deleting the Target, then telling it to use the Source sheet.

    This works
    [VBA]With wsTarget
    .Range("A4:T" & lrTarget).Delete
    .Range("A4:T" & lrSource).Value = wsSource.Range("A4:T" & lrSource).Value
    .Range("A4:T" & lrSource).Font.Bold = False
    End With[/VBA]

    This does not work
    [VBA]
    With wsTarget.Range("A4:T" & lrTarget)
    .Delete
    .Value = wsSource.Range("A4:T" & lrSource).Value
    .Font.Bold = False
    End With
    [/VBA]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    No you are deleting the range object.

    That might not be technically the right phrase but I suggest you try stepping throughh this code and add a watch to rng.
    [vba]
    Set rng = wsTarget.Range("A4:T" & lrTarget)
    With rng
    .Delete
    .Value = wsSource.Range("A4:T" & lrSource).Value
    .Font.Bold = False
    End With[/vba]

  5. #5
    I will try that, thanks Norie
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    When you delete a cell, if you are pointing at that cell your reference is lost.

    This codes resets the pointer to the new cell that now occupies the position of the deleted cell

    [vba]

    With wsTarget
    .Range("A4:T" & lrTarget).Delete
    .Range("A4:T" & lrSource).Value = wsSource.Range("A4:T" & lrSource).Value
    .Range("A4:T" & lrSource).Font.Bold = False
    End With
    [/vba]

    whereas this code has not been reset (because the cell is included in the With), so itsi trying to pibt at the cell the was deleted, which is invalid

    [vba]

    With wsTarget .Range("A4:T" & lrTarget).Delete
    .Range("A4:T" & lrSource).Value = wsSource.Range("A4:T" & lrSource).Value
    .Range("A4:T" & lrSource).Font.Bold = False
    End With
    [/vba]
    Last edited by Bob Phillips; 10-07-2007 at 11:33 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Bob,
    Possible you pasted the same line of code in the two examples? I see no difference between the two lines of code.
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It got screwed up, look at it now I have corrected it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    ok.... got it

    gracias
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Instead of Delete, use ClearContents
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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