+ Reply to Thread
Results 1 to 11 of 11

Last row starting from row A

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Last row starting from row A

    Hello. I'd like to paste into A and THEN offset by 1 row. This code starts in A2 unfortunately. How could I modify it? Also, why do I have to write PasteSpecial xl PasteAll? Couldn't I just write Paste? Thanks.


    Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteAll

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Last row starting from row A

    It's starting in A2 because you have Offset(1)

    Generally, you will need to do that to not overwrite the value in a table's last row.

    You could check to see if you are at the top of the sheet, or if the value is blank,
    IF Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).row =1 THEN 'This tells you that you have nothing in the A column below the first row - the first row may or may not be blank.  
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).PasteSpecial xlPasteAll
    ELSE
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteAll 'You did not make it to the first row - so there is data in the A column.  The offset(1) prevents over-writing the last row.
    END IF
    For the pasteSpecial versus paste, just try it both ways and see what happens. Generally, you want to clean your code of this kind of statement anyway.

    If you are trying to copy over some values, there are better ways to do that.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    I tried this with my code but it doesn't seem to work. The first part copies into A1 but then the rest of the line items don't copy anything.

    Sub men()
    For i = 1 To 12
        If Sheets("Sheet1").Cells(i, 1).Value = "Copy Me" Then
            Sheets("Sheet1").Cells(i, 1).Copy
        If Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row = 1 Then 'This tells you that you have nothing in the A column below the first row - the first row may or may not be blank.
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).PasteSpecial xlPasteAll
    Else
        Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteAll 'You did not make it to the first row - so there is data in the A column.  The offset(1) prevents over-writing the last row.
    End If
        End If
        Next i
    End Sub

  4. #4
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    Makes sense. Thanks!!

  5. #5
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Last row starting from row A

    Just as I suspected... there is no need for the .copy / .pasteSpecial funny business.

    Here's what I suggest instead.

    For i = 1 to 12
    
    	IF sheets("Sheet 1").cells(i,1).value = "Copy Me" then
    
    		IF sheets("Sheet 2").cells(1,1) ="" THEN
    			sheets("Sheet 2").cells(1,1).value = sheets("Sheet 1").cells(i,1).value
    		ELSE
    			sheets("Sheet 2").cells(rows.Count,1).end(xlUp).offset(1,0).value = sheets("Sheet 1").cells(i,1).value
    		END IF	
    
    	END IF
    
    NEXT I

    Barring any typos (I just typed that into the browser) that should do it. You can just equate the values and avoid the copy/paste stuff. It's a really good habit to get away from, it's something we all do at one point because the Macro recorder does it....

  6. #6
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    Hey General. It still doesn't work. At the third line it returns "subscript out of range". Not sure why.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Last row starting from row A

    Hi ammartino,

    It looks like you are starting to learn to program in VBA and Excel. Each of us develops a style of programming that we understand.

    One of the first problems is to extract a cell using VBA. Here are a few ways to get the stuff out of B7.
    Range("B7")
    Cells(7, "B")
    Read my favorite author's answer at: http://spreadsheetpage.com/index.php...your_vba_code/

    I rarely use any "Offset" methods in my programming as it needs to know what cell the offset is from and this is one more level of confusion for me.

    The next style concern is to use a For Next construct or a For Each construct.

    I prefer the older For Next construct as I like to know each row and column I'm working with.

    I also don't like to use variable names of j,k,l as a more meaningful name makes code much more readable.

    See which of these two subs make more sense. What kind of coder do you want to be?

    Sub CopyMeSht1to2()
        Dim LastRowA As Double
        Dim RowCtr As Double
        
        LastRowA = Cells(Rows.Count, "A").End(xlUp).Row
        
        For RowCtr = 1 To LastRowA
            If Cells(RowCtr, "A") = "Copy Me" Then
                Sheets("Sheet2").Cells(RowCtr, "A") = Cells(RowCtr, "A")
            End If
        Next RowCtr
        
    End Sub
    
    Sub Test()
    Dim lr, i
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
    If Range("A" & i).Text = "Copy Me" Then
    Sheets(2).Range("a" & i) = "Copy Me"
    End If
    Next
    End Sub
    Look at http://www.excelfunctions.net/VBA-Co...sentation.html for a start to indentation and line breaks.
    Last edited by MarvinP; 03-12-2015 at 02:26 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  8. #8
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    Hi Marvin. I like your code examples, but the problem is that they copy to the same cell in sheet 2, whereas I would like them to copy into A1, A2, A3, etc.

  9. #9
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Last row starting from row A

    OK - I missed that part of the problem. No problem reading my code...
    How about this then.

    Sub CopyMeSht1to2()
        Dim LastRowA As Double
        Dim LastRowSht2 As Double
        Dim RowCtr As Double
        
        LastRowA = Cells(Rows.Count, "A").End(xlUp).Row
        
        For RowCtr = 1 To LastRowA
            If Cells(RowCtr, "A") = "Copy Me" Then
                LastRowSht2 = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
                Sheets("Sheet2").Cells(LastRowSht2 + 1, "A") = Cells(RowCtr, "A")
            End If
        Next RowCtr
        
    End Sub

  10. #10
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    Thanks Marvin. This still has the same issue of copying into A2, not A1 because you are adding 1 to the last row.

  11. #11
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Last row starting from row A

    I thought this might work but it's not for some reason. On the second if statement it doesn't read the lastrow right.

    Sub CopyMeSht1to2()
        Dim LastRowA As Double
        Dim LastRowSht2 As Double
        Dim RowCtr As Double
        
        LastRowA = Cells(Rows.Count, "A").End(xlUp).Row
        
        For RowCtr = 1 To LastRowA
            If Cells(RowCtr, "A") = "Copy Me" Then
                LastRowSht2 = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
            If LastRowSht2 = 1 Then
                Sheets("Sheet2").Cells(LastRowSht2, "A") = Cells(RowCtr, "A")
            Else
                Sheets("Sheet2").Cells(LastRowSht2 + 1, "A") = Cells(RowCtr, "A")
            End If
            End If
        Next RowCtr
        
    End Sub

+ 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. Just starting
    By davids4500 in forum Hello..Introduce yourself
    Replies: 3
    Last Post: 07-17-2014, 08:54 AM
  2. Trouble with spreadsheet layout and connecting multiple sheets
    By jbradley in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-18-2013, 06:51 PM
  3. Replies: 5
    Last Post: 09-27-2012, 07:30 PM
  4. Doublecheck with second lookup starting starting after first match
    By zekethewolf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-06-2011, 06:36 AM
  5. [SOLVED] Starting with A2!
    By Alen32 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2005, 06:06 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