+ Reply to Thread
Results 1 to 6 of 6

Isolate the last 3 used area of the column

Hybrid View

emina002 Isolate the last 3 used area... 03-05-2014, 09:07 PM
Kaper Re: Isolate the last 3 used... 03-06-2014, 07:55 AM
Kaper Re: Isolate the last 3 used... 03-06-2014, 07:59 AM
emina002 Re: Isolate the last 3 used... 03-06-2014, 09:39 PM
Kaper Re: Isolate the last 3 used... 03-07-2014, 02:55 AM
emina002 Re: Isolate the last 3 used... 03-07-2014, 03:49 AM
  1. #1
    Forum Contributor
    Join Date
    05-16-2011
    Location
    Philippines
    MS-Off Ver
    Excel 365/Excel 2016
    Posts
    315

    Isolate the last 3 used area of the column

    Hello, few years back. I do have problem finding last empty cell on a column and inserting, copy paste the last non empty column. Now, I do have same problem again but this time I have to copy the last 3 non empty columns and paste it on 3 inserted column. Here is the piece of successful code with my previous problem. Hope you can modify it. Thanks!

    Sub AddCol()
        Dim rng As Range, c As Range
        Set rng = Intersect(ActiveSheet.UsedRange, Rows(2)) ' Isolate row 2
        If rng Is Nothing Then Exit Sub ' Assuming there is more than one row
        Set c = rng.Cells(rng.Cells.Count) ' Isolate the last cell in row 2
        If IsEmpty(c) Then Set c = c.End(xlToLeft) ' Find the last non-empty cell
        If Not IsEmpty(c) Then
            Set rng = Intersect(c.EntireColumn, ActiveSheet.UsedRange) ' Isolate the used area of the column
            rng.Copy ' Copy to clipboard
            rng.Insert Shift:=xlToRight ' Duplicate
            c.Offset(0, -1).Select ' Select the "marker" in the duplicate column
            Application.CutCopyMode = False ' Switch off the CutCopy marquee
        End If
    End Sub
    Last edited by emina002; 03-07-2014 at 03:51 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Isolate the last 3 used area of the column

    How about (try it):

    Sub AddCol()
      Dim rng As Range, c As Range, i as long
      for i = 1 to 3
        Set rng = Intersect(ActiveSheet.UsedRange, Rows(2)) ' Isolate row 2
        If rng Is Nothing Then Exit Sub ' Assuming there is more than one row
        Set c = rng.Cells(rng.Cells.Count) ' Isolate the last cell in row 2
        If IsEmpty(c) Then Set c = c.End(xlToLeft) ' Find the last non-empty cell
        If Not IsEmpty(c) Then
            Set rng = Intersect(c.EntireColumn, ActiveSheet.UsedRange) ' Isolate the used area of the column
            rng.Copy ' Copy to clipboard
            rng.Insert Shift:=xlToRight ' Duplicate
            c.Offset(0, -1).Select ' Select the "marker" in the duplicate column
            Application.CutCopyMode = False ' Switch off the CutCopy marquee
        End If
      next i
    End Sub
    I just added two lines and one declaration of variable. Looking at the structure - it shall work. Or if I understand wrong your concept - try another mod:

    Sub AddCol()
      Dim rng As Range, c As Range, i as long
        Set rng = Intersect(ActiveSheet.UsedRange, Rows(2)) ' Isolate row 2
        If rng Is Nothing Then Exit Sub ' Assuming there is more than one row
        Set c = rng.Cells(rng.Cells.Count) ' Isolate the last cell in row 2
      for i = 1 to 3
        If IsEmpty(c) Then Set c = c.End(xlToLeft) ' Find the last non-empty cell
        If Not IsEmpty(c) Then
            Set rng = Intersect(c.EntireColumn, ActiveSheet.UsedRange) ' Isolate the used area of the column
            rng.Copy ' Copy to clipboard
            rng.Insert Shift:=xlToRight ' Duplicate
            c.Offset(0, -1).Select ' Select the "marker" in the duplicate column
            Application.CutCopyMode = False ' Switch off the CutCopy marquee
            Set c = c.offset(0,-2)   'not sute about it: may be -1 - experiment with it
        End If
      next i
    End Sub
    just a small tip:

    Next time post a tiny sample workbook with few input data and desired (manualy "calculated") results.
    It makes much easier to test few concepts and choose the best one for those who try to help.
    Best Regards,

    Kaper

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Isolate the last 3 used area of the column

    PS. depending on data layout and filled/empty cells it could be wise to add a security measure to the final lines:

            Application.CutCopyMode = False ' Switch off the CutCopy marquee
            if c.column <3 then 
              exit for
            else
              Set c = c.offset(0,-2)   'not sute about it: may be -1 - experiment with it
            end if
        End If

  4. #4
    Forum Contributor
    Join Date
    05-16-2011
    Location
    Philippines
    MS-Off Ver
    Excel 365/Excel 2016
    Posts
    315

    Re: Isolate the last 3 used area of the column

    Hi sorry if I didn't attach sample workbook. Kindly see below sample workbook. Since this is a monthly report,let assume that on row 2,it will find an empty cell which is on Column E. I have to copy the last 3 previous column(A,the merge column/row B,C and D,may be 4 columns since B and C are merge),and insert copied cells as the macro select Column E(which the macro found empty cell on row 2). See the must be result tab. If you notice Column E to H is populated already, with the details on last 4 columns. For the next month, I have to open the workbook and create another report for current month(eg: February report), if the code will identify the last empty cell on row 2 and it would be on Column I now. I just embed your modify code and it just copy the last non empty column 3x. Let me know if the example is not yet clear. Thanks.
    Attached Files Attached Files

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Isolate the last 3 used area of the column

    OK. Then yet another approach (and you are right - there are 4 columns indeed, just 3 headers):
    Sub AddCol3()
        Dim rng As Range, c As Range
        Set rng = Intersect(ActiveSheet.UsedRange, Rows(2)) ' Isolate row 2
        If rng Is Nothing Then Exit Sub ' Assuming there is more than one row
        Set c = rng.Cells(rng.Cells.Count) ' Isolate the last cell in row 2
        If IsEmpty(c) Then Set c = c.End(xlToLeft) ' Find the last non-empty cell
        If Not IsEmpty(c) Then
            Set rng = Intersect(c.Offset(0, -3).Resize(1, 4).EntireColumn, ActiveSheet.UsedRange) ' Isolate the used area of the 4 columns
            rng.Copy ' Copy to clipboard
            rng.Insert Shift:=xlToRight ' Duplicate
            c.Offset(0, -1).Select ' Select the "marker" in the duplicate column may be here you would like to change -1 to -3?
            Application.CutCopyMode = False ' Switch off the CutCopy marquee
        End If
    End Sub
    As you see the only change (marked blue) from the original code, is to move c cell left 3 columns from original and made c 4 columns wide before setting rng Range (to be copied and inserted).

  6. #6
    Forum Contributor
    Join Date
    05-16-2011
    Location
    Philippines
    MS-Off Ver
    Excel 365/Excel 2016
    Posts
    315

    Re: Isolate the last 3 used area of the column

    Wow it works. I tried to modify the code but when I run it, it getting error. Now my problem solved now. Thanks @Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Isolate sum from cumulative sums in a column
    By nrrrrb in forum Excel General
    Replies: 4
    Last Post: 07-29-2013, 10:44 AM
  2. Isolate rows based on first column
    By justinsmyth1 in forum Excel General
    Replies: 5
    Last Post: 06-22-2012, 06:46 PM
  3. Isolate Duplicates in Column B if Column A also Duplicate
    By Ocean Zhang in forum Excel General
    Replies: 4
    Last Post: 03-13-2012, 04:26 PM
  4. Tuning column chart to get area-column effect
    By goels in forum Excel General
    Replies: 2
    Last Post: 06-07-2011, 01:02 PM
  5. Isolate 1st 2 letters in a column
    By Curalice in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-06-2007, 04:49 AM

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