+ Reply to Thread
Results 1 to 5 of 5

range doesnt work for last varible

  1. #1
    Registered User
    Join Date
    03-28-2013
    Location
    Kent, Engmland
    MS-Off Ver
    Excel 2010
    Posts
    15

    range doesnt work for last varible

    Hi I am trying to get this print to loop thru several cells and plug them into another sheet to print out. The rang works until the last variable. even if I comment it out then the new last one errors out. Can someone tell me what I'm doing wrong:

    Sub IteratePrint()
    Dim r, clean, squat, bench As Range, lngLast As Long
    lngLast = Sheets("MAXSHEET2").Range("C" & Rows.Count).End(xlUp).Row + 1

    For Each r In Sheets("MAXSHEET2").Range("C2:C" & lngLast)
    clean = Sheets("MAXSHEET2").Range("D2:D" & lngLast)
    squat = Sheets("MAXSHEET2").Range("E2:E" & lngLast)
    bench = Sheets("MAXSHEET2").Range("F2:F" & lngLast)
    If Not IsEmpty(r) Then
    Range("A8") = r
    Range("E9") = clean 'Sheets("MAXSHEET2").Range("D" & lngLast).Value
    Range("F9") = squat 'Sheets("MAXSHEET2").Range("E" & lngLast)
    Range("G9") = bench 'Sheets("MAXSHEET2").Range("F" & lngLast)
    ActiveSheet.PrintOut
    End If
    Next
    End Sub
    Last edited by ukyank; 09-18-2020 at 02:39 AM. Reason: solved

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: range doesnt work for last varible

    Try something like this...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-28-2013
    Location
    Kent, Engmland
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: range doesnt work for last varible

    Thanks but this makes it a list it doesnt seem to replace the values

  4. #4
    Registered User
    Join Date
    03-28-2013
    Location
    Kent, Engmland
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: range doesnt work for last varible

    Its probably not very pretty but I resolved it by using OFFSET:

    If Not IsEmpty(r) Then
    Range("A8") = r
    Range("E9") = r.Offset(0, 1).Value
    Range("F9") = r.Offset(0, 2).Value
    Range("G9") = r.Offset(0, 3).Value
    'ActiveSheet.PrintOut
    End If

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: range doesnt work for last varible

    It's unclear what you code should be doing.

    First error I get is the assignment of bench variable. You have defined it as a range object. In which case you would need to use the Set assignment.

    The variables clean, squat, bench are being loaded with an array of values. But when you write the values out you only specify a single cell. So only the first item is outputted.
    In the loop you always load the same range into the variables so you always write out the same top row.

    Describe what are you trying to do with the code.
    Cheers
    Andy
    www.andypope.info

+ 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] Range method with space doesnt work?
    By miso.dca in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2015, 05:07 AM
  2. Why dynamic range doesnt work with COUNTA
    By Kushal_1991 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-31-2013, 11:17 AM
  3. why does the code Range("A1:A50") work and Range(A1).entirecolumn doesnt?
    By kjy1989 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-29-2013, 03:22 AM
  4. [SOLVED] Select range doesnt work
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-16-2012, 11:09 AM
  5. Range(Range1) Variable doesnt work. _Global error
    By prefix in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-31-2011, 10:35 AM
  6. sort doesnt work with Range of cells
    By xfusr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-20-2007, 02:39 PM
  7. [SOLVED] insert entire row for new data, external data range doesnt work
    By orlya1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 03:45 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