+ Reply to Thread
Results 1 to 3 of 3

Multiple If's and Last value in range

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    45

    Red face Multiple If's and Last value in range

    Hello everyone!

    Can anyone help me out with my issue here?

    So far this is my code.

    Sub AddRow_CopySheet_Rename2()
        Dim Sheet2 As Variant
        Dim CellX As Range
        Dim WS As Worksheet, WB As Workbook
    '-----------------------------
        Dim sShtName As String
        Dim lr As Long
    '------------------------------
        If A14 > 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        ElseIf A14 = 100 Then
         Set Sheet2 = Worksheets("Template2")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template2")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        Else
        Application.ScreenUpdating = False
    '------------------------------
        Set Sheet2 = Worksheets("Template3")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template3")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        End If
    End Sub
    Now, what I want is instead of it referring to cell A14, it refers to the last cell with data in it in column A.

    How would I go about doing this?

    I have since posting this, tried to change some code, and here is what I have come up to.

    Option Explicit
    Sub AddRow_CopySheet_Rename()
        Dim Sheet2 As Variant
        Dim CellX As Range
        Dim WS As Worksheet, WB As Workbook
    '-----------------------------
        Dim sShtName As String
        Dim lr As Long
    '------------------------------
        If lr = Range("A" & Rows.Count).End(xlUp).Row < 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template3")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template3")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        ElseIf lr = Range("A" & Rows.Count).End(xlUp).Row = 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template2")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template2")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        Else
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
    End If
    End Sub
    And still it isn't selecting the last value in the range.

    PLEASE help me out here guys!
    Last edited by Dagoom; 11-10-2013 at 05:31 AM.

  2. #2
    Registered User
    Join Date
    04-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Multiple If's and Last value in range

    Got it myself,

    This is what made it work!

    Option Explicit
    Sub AddRow_CopySheet_Rename()
        Dim Sheet2 As Variant
        Dim CellX As Range
        Dim WS As Worksheet, WB As Workbook
    '-----------------------------
        Dim sShtName As String
        Dim lr As Long
    '------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        If lr - 10 < 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        ElseIf lr - 10 = 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template2")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template2")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
        ElseIf lr - 10 > 100 Then
        Application.ScreenUpdating = False
        Set Sheet2 = Worksheets("Template3")
        Sheet2.Visible = True
        Set CellX = ActiveCell
        ActiveWorkbook.Sheets(1).Activate
        Range("Table2").Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True
    '-------------------------------
        lr = Range("A" & Rows.Count).End(xlUp).Row
        sShtName = Range("A" & lr).Value
    '-------------------------------
        Range("InsertSection").Select
        Set WB = ActiveWorkbook
        Set WS = WB.Sheets("Template3")
        WS.Copy After:=Sheets(WB.Sheets.Count)
    '-------------------------------
        ActiveSheet.Name = sShtName
    '-------------------------------
        Application.Goto CellX
        Sheet2.Visible = False
        Application.ScreenUpdating = True
    End If
    End Sub

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,752

    Re: Multiple If's and Last value in range

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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] Macro DtRNG as Range, Dt as Range, Multiple Criteria or dates
    By Jack7774 in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 03-18-2013, 03:46 PM
  2. Replies: 2
    Last Post: 03-05-2013, 03:07 PM
  3. Replies: 6
    Last Post: 11-11-2012, 12:40 AM
  4. [SOLVED] Copy range from multiple sheets, into a master sheet and moving over by variable col range
    By g1eagle in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-23-2012, 01:36 PM
  5. [SOLVED] Copy range from multiple files in multiple folders to single sheet in master WB
    By Royzer in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-18-2012, 03:40 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