+ Reply to Thread
Results 1 to 5 of 5

Having problems with the range!

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Having problems with the range!

    Hi,

    I have been trying to expand a bit of code that was written for me to copy a selection of cells from another sheet onto the end.

    I wanted to expand it so that it auto filled the date (weekdays) into the new cells also.

    However I am having problems with the range for the autofill...

    Sub WeekA()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim Previousrow As Long
    Previousrow = Cells(Rows.Count - 5, "B").End(xlUp).Row
    
    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    
    Dim Newrow As Long
    Newrow = Cells(Rows.Count + 5, "B").End(xlUp).Row
    
    Sheets("Sheet2").Range("A3:I7").Copy ws.Range("A" & Rows.Count).End(3)(2)
    Range("B126:C130").AutoFill Destination:=Range("B126:C135"), Type:=xlFillWeekdays
    
    End Sub
    My hope is to generalise the above code using "Previous row, Lastrow and Newrow"

    Thanks for any help!!
    Last edited by ARowbot; 03-18-2015 at 06:58 PM.

  2. #2
    Forum Contributor
    Join Date
    06-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    191

    Re: Having problems with the range!

    Your range reference in red needs to refer to a worksheet first.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Having problems with the range!

    Sub WeekA()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim Previousrow As Long
    Previousrow = Cells(Rows.Count - 5, "B").End(xlUp).Row 'this isn't going to do what you think it is going to do
    
    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    
    Dim Newrow As Long
    'Newrow = Cells(Rows.Count + 5, "B").End(xlUp).Row 'This will error.  Rows.Count + 5 is like saying infinity plus 5.  Just doesn't work
    
    'If Newrow worked; Previousrow, Lastrow, & Newrow will all give the same row (unless all rows in your worksheet are full).  You are doing something wrong
    MsgBox (Previousrow & " " & Lastrow) 'example
    
    Sheets("Sheet2").Range("A3:I7").Copy ws.Range("A" & Rows.Count).End(3)(2)
    Range("B126:C130").AutoFill Destination:=Range("B126:C135"), Type:=xlFillWeekdays 'needs to reference a worksheet in both first range and destination range
    
    'If you want to incorporate first and last row into a range it would be something like this; with a proper previousrow
    'ws.Range("B" & Previousrow, "C" & Lastrow).AutoFill Destination:=ws.Range("B" & Previousrow, "C" & Lastrow), Type:=xlFillWeekdays
    
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Having problems with the range!

    Ok, I have done that...Ideally the bit I want to change is:

    Range("B126:C130").AutoFill Destination:=Range("B126:C135"), Type:=xlFillWeekdays
    to make it:

    Range("B{5 rows up from last row}:C{last row before copy}").AutoFill Destination:=Range("B{last row before copy}:C{last row after copy}"), Type:=xlFillWeekdays
    I am having trouble uploading an attachment at the moment...I am trying!
    Last edited by ARowbot; 03-18-2015 at 05:07 PM.

  5. #5
    Valued Forum Contributor
    Join Date
    11-12-2014
    Location
    Nottingham
    MS-Off Ver
    2013
    Posts
    383

    Re: Having problems with the range!

    I've managed to play around a bit and done it quite cheekily I think...
    Here is the code for anybody interested...

    Sub WeekA()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim Previousrow As Long
    Previousrow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    
    Sheets("Sheet2").Range("A3:I7").Copy ws.Range("A" & Rows.Count).End(3)(2)
    
    Dim Lastrow As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    Dim Newrow As Long
    Newrow = Range("A" & Rows.Count).End(xlUp).Offset(5).Row
    
    ws.Range("B" & Previousrow, "C" & Lastrow).Offset(-5, 0).AutoFill Destination:=ws.Range("B" & Previousrow, "C" & Newrow).Offset(-5, 0), Type:=xlFillWeekdays
    
    End Sub
    Thanks for the help!!

+ 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. Range Problems
    By jamesvw68 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 09-29-2009, 06:48 AM
  2. Range problems
    By jamesvw68 in forum Excel General
    Replies: 1
    Last Post: 09-28-2009, 09:26 AM
  3. Problems with Range
    By tneva in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-02-2009, 07:10 AM
  4. problems with a range
    By holybull in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-23-2007, 12:03 PM
  5. [SOLVED] Range problems...
    By Neil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2005, 03:06 PM

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