Consulting

Results 1 to 11 of 11

Thread: Copy range to blank cell

  1. #1

    Copy range to blank cell

    I want Vba to copy the column of data that is found under the name "Value" until it reaches a blank space and then copy that data in another worksheet. this is what I have so far.

    Sub select_cellsCounty()
         Set cell = Cells.Find(What:="Value")      
         cell.Select
         Set Start= ActiveCell.Offset(1, 0).Select  
     Set ans = Application.InputBox(Start, Start, Type:=8)
            
            Range(ans, Cells(Rows.Count, ans.Column).End(xlUp)).Select
             
    Selection.Copy
    Sheets("State").Select
            Range("A2:A").Select
            ActiveSheet.Paste
    End Sub
    I want to get rid of the input box and have it do it automatically, but I dont know how to set the beginning of the range as the cell one below the name "Value". I'm using Excel 2013.


    Thanks,
    Last edited by Bob Phillips; 08-10-2017 at 10:25 AM. Reason: Added code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is the Inputbox for, you are finding the column.
    ____________________________________________
    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

  3. #3
    The inputbox was used initially to select the first cell in the range, but i want to get rid of that.
    I want something like this

    Range(cells.Find(What:="Value").Offset(1, 0), cells(Rows.Count, ans.Column).End(xlUp)).Select

    Where the cell below "Value" is the first cell in the range, and the range is selected until the first empty cell. I am trying this but it doesnt allow me to do it.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Dim Found As Range
    Set Found = Rows(1).Find(What:="Value").Offset(1, 0)
    Range(Found, Cells(Rows.Count, Found.Column).End(xlUp)).Copy
    Possible Alternate, If you can guarantee that the column contains no empty cells above the bottom most used cell
    Range(Found, Found.End(xlDown)).Copy
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Quote Originally Posted by SamT View Post
    Dim Found As Range
    Set Found = Rows(1).Find(What:="Value").Offset(1, 0)
    Range(Found, Cells(Rows.Count, Found.Column).End(xlUp)).Copy
    Possible Alternate, If you can guarantee that the column contains no empty cells above the bottom most used cell
    Range(Found, Found.End(xlDown)).Copy

    I tried what you said, but I keep getting an "object variable or With block variable not set" error.

    Is there anyway to avoid the error

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Dim Found As Range
    Set Found = Cells.Find(What:="Value").Offset(1, 0)
    Range(Found, Found.End(xlDown)).Copy Sheets("State").Range("A2")
    End Sub
    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'

  7. #7
    Thank You, that worked perfectly

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Happy to help. For the future, please use meaningful titles rather than "Please Help" and the like. I'll change this title.
    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'

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    JSMH
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by SamT View Post
    JSMH
    ?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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'

Tags for this Thread

Posting Permissions

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