Results 1 to 9 of 9

Thread: suggest the mistake in this macro part

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by Safal Shrestha View Post
    This part of the macro is for pasting in a different sheet. I have the heading in row b3 of sheet named "i".. there is error in the line after the else command.
    Code:
    Sheets("i").Activate
    Range("B3").Select
    If Range("b3").Offset(1, 0) = "" Then
        Selection.Offset(1, 0).Select
    Else
        Selection.Range(xlDown).Offset(1, 0).Select
    End If
    ActiveSheet.paste
    I am guessing that you meant the code line I highlighted in red to be this instead...

    Selection.End(xlDown).Offset(1, 0).Select

    Since you didn't post all your code, I cannot rewrite it for you because it ends by selecting a cell, but I would point out that it is rarely necessary to select cells in order to work with them. Whenever you see yourself writing something like this...

    Range(..).Select
    Selection.something


    you can write it like this instead...

    Range(..).something

    Think about it... what does Selection.something mean? It means do "something" with the object you selected. But what is that object... it is the range you selected in the code line above... so you might as well work directly with the original object instead of working with a different object that references it. In effect, you already knew this because in your code, after you did this...

    Range("B3").Select

    you wrote this...

    If Range("B3").Offset(1, 0) = "" Then

    instead of writing this...

    If Selection.Offset(1, 0) = "" Then
    Last edited by Rick Rothstein; 04-02-2013 at 12:27 PM.

Similar Threads

  1. Suggest the mistake in the formula
    By Safal Shrestha in forum Excel Help
    Replies: 2
    Last Post: 04-21-2013, 09:42 AM
  2. Replies: 2
    Last Post: 04-16-2013, 01:36 PM

Posting Permissions

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