+ Reply to Thread
Results 1 to 4 of 4

Macro range issue

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-28-2003
    Location
    Happyville, USA
    MS-Off Ver
    365
    Posts
    300

    Macro range issue

    Hi,

    The code below has been working great for me but I need to add something to it. The code is currently looking at cell I2 to see if there is a value in it. If there is anything in it the formula will then select down to the end of the data, cutting it, and placing it in column H. It works great.

    The problem I'm running into is if there is data only in cell I2. The formula is then copying all the way down to the end of the sheet and poplulating column H with a bunch of blanks. How can I get the formula to know that if there is data only in cell I2 to copy that cell but not copy down any further? I want to keep the code and functions that it performs but add that piece to the puzzel.

    Any thoughts are most welcome.

    Thanks

    Eddie.


    PHP Code: 
    'If anything is in column I it will cut all data and place it in column H.  If there is nothing in that column it will not perform the function.
            
            
                If Range("I2").Value <> "" Then
    Range("I2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Cut
    Range("H2").Select
    ActiveSheet.Paste
    End If 

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Dynamic Range VBA

    Try something like this.....


        ActiveWorkbook.Names.Add Name:="YourRange", RefersTo:= _
                                 "=OFFSET(Sheet1!$I$2,0,0,COUNTA(Sheet1!I:I),1)"
        If Range("I2").Value <> "" Then
            Range("YourRange").Cut Destination:=Range("H2")
        End If

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565
    Or this:

    Range("I65536").End(xlUp).Select
    
        If ActiveCell.Address(False, False) = "I2" Then
                Range("I2").Select
                Selection.Cut
                Range("H2").Select
                ActiveSheet.Paste
        Else
        
            Range("I2").Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Cut
            Range("H2").Select
            ActiveSheet.Paste
        
        End If
    HTH

    Robert

  4. #4
    Forum Contributor
    Join Date
    10-28-2003
    Location
    Happyville, USA
    MS-Off Ver
    365
    Posts
    300
    Outstanding!!!!! Those both work great.

    Sorry I couldn't reply until now. I had some meeting this morning and just got back to my computer.

    Thanks

    Eddie.

+ 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