+ Reply to Thread
Results 1 to 9 of 9

Error when selecting a range

  1. #1
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562

    Exclamation Error when selecting a range

    I have a program that creates and prints a form. A certain portion of this form has borders around the cells. After joining this forum I am breaking out of "selecting" things, but I am having trouble on this on.


    The code below is giving me a 1004 error.

    HTML Code: 
    What am I doing wrong? I have just started trying to use ranges so I am not real clear yet on the coding.

    Any help will certainly be appreciated.

    Jeff

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Well, I have answered my own question.

    Changed

    HTML Code: 
    to

    HTML Code: 
    and now it works.

    This was a small section of a huge sub and I am in the middle of converting it over to not use "select" . Apparently, wsLetter did not have focus and this was causing the problem.

    Jeff

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    boylejob

    You need to be careful using code like
    Please Login or Register  to view this content.
    as Excel can take the value of the cell and not the cell address, which is what you need for the range part of the command

    I recommend you use
    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Mudraker,

    Thanks for the additional help. I would not have even thought about the Address thing. You never know what new things you are going to learn when you post something.

    I have one more question on this subject. Like I mentioned, the bit of code I posted is a small portion of a rather large subroutine. The statement I am working on has actually ended up looking like this:

    Please Login or Register  to view this content.
    Is there any problem with writing the Set statement as I have it listed above. Basically, incorporating it within a With statement.

    I have learned a great deal from this forum so I am trying things I have not tried in the past. The speed at which my programs are now running has greatly improved as a result of the stuff I am learning on the forum.

    Thanks!

    Jeff

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Jeff

    I have never used set within a With something command util your previous posting

    I tried this code.
    It set the range as coded
    So I guess the answer is no there is no problem
    I will ask another more experienced person to have a look at this thread
    Please Login or Register  to view this content.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Jeff,

    The With statement is a very efficient method of writing code. It also makes your code much easier to read.This VBA statement is identical to the "C" language statement. It speeds compiliation of the code by the interpreter. When you use a statement like Range("A10").Select the interpreter must find the object parent first. The With statement provides the interpreter with a short cut. The interpreter looks up the parent object only once. It finds the parent of the object following With and retains it until the End With is encountered.

    You can even nest With statements. A good example is setting Font properties and Border properties.

    Please Login or Register  to view this content.
    Hope this helps you to better understand the With statement.

    Sincerely,
    Leith Ross

  7. #7
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Leith,

    Thank you for the explanation. That helps me understand what exactly is going on.

    I have been programming in VBA for about 6-7 years now. Pretty much self taught by picking up things from here and there. After joining this forum I have found I have done a lot of things the hard way. I have used what I understood and worked around that which I did not. I am learning new things everyday from this forum.

    Yesterday, I put the "with" statement to use to a greater extent than I have in the past and I wanted to make sure there is no problem with the way I have this code. The program I am working on is a pretty important program in my office and I want to be sure I haven't fouled things up.

    My concern was with my Set rletter statement that is buried deep within a With statement. As you can see by Mudraker's post he set up a bit of code similar to mine and ran it. It worked fine on his end and it worked fine on my end. The code I have below is an expansion of Mudraker's code, but I have it set exactly like it is in my program (just a shortened version).

    Please Login or Register  to view this content.
    This is the statement I was concerned with

    Please Login or Register  to view this content.
    Do you see any problems with this statement? It just had a lot more periods than I am accustom to seeing so I wanted to make sure it is correct and will not cause me a problem later.

    Thanks!

    Jeff

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Jeff,

    The Set statement simply assigns an object variable to an existing object. In your case rLetter is now tha same object as ...
    Please Login or Register  to view this content.
    Since this is what you intended it to be, no problem. I grew up in Asheville, N.C.

    Sincerely,
    Leith Ross

  9. #9
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Leith,

    Thanks for the help! This forum is a fantastic resource that I sure wish I had found years ago. My code is running so much faster it is absolutely incredible.

    I am a transplant from Owensboro, KY and have lived in Forest City for about 5 years now which is about an hour from Ashville. Small world and the internet seems to be making it even smaller.

    Jeff

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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