Results 1 to 9 of 9

Thread: Simple macro, yet trouble

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    1

    Simple macro, yet trouble

    Hello all

    I want to make a very simple macro, but its causing me lots of trouble. Might be that my VBA-skills aren't updated

    I have a long list of data in column A in Sheet1. I want to create a macro that copies data from cell B1 in Sheet2 to the first empty cell in column A in Sheet1.

    I have no problem creating a macro that copies to the last cell with data, but I want the first EMPTY cell.

    Can anyone help?

    Thanks in advance

    Fred

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple macro, yet trouble

    This is the VB.NET forum, for questions on VB.NET. VBA questions belong in the VBA forum. I have asked the mods to move this thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Simple macro, yet trouble

    Moved. Thanks for the report jmcilhinney

    Gary

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple macro, yet trouble

    We must have both moved this at the same time.

    I have an additional question. You mention Sheet1 so it is presumed that you are using Excel VBA.

    It is important for anyone wishing to assist you to know what version of Excel you are using.

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Simple macro, yet trouble

    Another thing to note is what do you consider the first empty cell? Do you start in the first column and keep going down? Or start in the first row and keep going across? Or a more difficult square pattern? Rectangle? So you see, the first cell can be many different things to many different people and I presume only one way will work for you.

    The first step is to CLEARLY and COMPLETELY define the problem AND solution.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Simple macro, yet trouble

    try
    vb Code:
    1. range("a65535").end(xlup).offset(1) = range("b1")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    19

    Re: Simple macro, yet trouble

    I think westconn way works best, but if you need a number instead of a address this works as well:

    vb Code:
    1. for i = 1 to 256 ' ' 256 is the maximum number of cols excel contains.
    2.   If isEmpty(Cells(0,i)) then Exit For ' ' No endless loops for me, right
    3. next i
    4. Cells(0,i).paste ' ' This is for pasting, not sure if this works like this...

    That will give you the column number. Swap the "0" and the "i" in the "isEmpty" statement to get the row number. Make sure you change the 256 to 65536 when doing rows.
    Last edited by unleash; Apr 27th, 2011 at 04:48 PM. Reason: Did it the wrong way round... thanks westconn1.
    "For all men are equal before fish."
    ~ Herbert Hoover ~

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Simple macro, yet trouble

    Swap the "0" and the "i" in the "isEmpty" statement to get the column number.
    other way round, the example is doing columns
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    Junior Member
    Join Date
    Feb 2010
    Posts
    19

    Re: Simple macro, yet trouble

    Sorry, my bad.

    I changed the original post, so nobody gets confused
    "For all men are equal before fish."
    ~ Herbert Hoover ~

Posting Permissions

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



Click Here to Expand Forum to Full Width