+ Reply to Thread
Results 1 to 25 of 25

Creating IF statement

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Creating IF statement

    Hi everyone

    I am relatively new to VBA and am working on a piece of existing code after modifying an excel 2010 spreadsheet slightly. I have thrown this question out to Mr Excel however had not yet had an answer (and unfortunately I am working to a deadline at work with this sheet) and therefore I am hoping I may be able to locate an expert on this forum. http://www.mrexcel.com/forum/excel-q...statement.html If someone would be able to assist me, I would greatly appreciate it.


    The macro I'm using is throwing up an error; this is the code:

    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    'delete old claims and move to old claims tab
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub
    Occassionally there will be instances where there is no data to select (no rows at all - because any blanks have been deleted and there are no additional rows of data) and the macro throws up an object error at the CurrClaims.EntireRow.Select (line 7)

    'delete old claims and move to old claims tab
            CurrClaims.EntireRow.Select

    It was suggested to me that I put IF statements in the code to say if there is nothing in that range then do this (after delete old claims):
     'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
           
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

    But IF there are claims then:
     'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    'delete old claims and move to old claims tab
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

    Would someone please be able to assist me with the code to make this happen? I am not experienced enough to work this out. I have tried various things and it seems to ignore what I am putting in there.

    Thanks very much
    Last edited by anoctua; 02-26-2014 at 06:09 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Hi anoctua,

    Welcome to the forum and a fellow Ozzie!!

    Let us know how this goes:

    'If the 'CurrClaims' range has something in it, then...
        If Not CurrClaims Is Nothing Then
            '...run the following code.
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        End If
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Hi Robert, my fellow Aussie!!

    I put in your code (in the way I understand I was supposed to... not very experienced here!) but it still throws up an "object required" error on the CurrClaims.EntireRow.Select line... Here's what I did - and by the way, I put the "end if" just before "'delete all blank rows from "current claims" tab" seeing as though if there are no claims, it only needs to do the actions from that line down... does that make sense?:


    'delete old claims and move to old claims tab
        If Not CurrClaims Is Nothing Then
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
    
        End If
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    What variable are you using to define CurrClaims?

  5. #5
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Because I am not really awesome with this stuff - here's my whole code... maybe this will help!

        Sub AddNewClaims()
    '
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    Dim NewClaims, CurrClaims, CurrNewClaims, PrevClaims, Rng, PrevClaimsLastRow, OrigRow As Range
    Dim FirstRow, LastRow, iX As Integer
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims to only xxxx
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="xxxx"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            Range("A2:AE5000" & LastRow).Copy
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
        If Not CurrClaims Is Nothing Then
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
    
        End If
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Defining your variables that way results in all but the last being defined as variant. This means that as CurrClaims is not a range my IF statement doesn't work on it.

    Use these and try again (note too that I use a long variable over an interger as the former can avoids the 32,767 limitation that the later has):

    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    HTH

    Robert

  7. #7
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thanks Robert - hope I did this right, as unfortunately it's still throwing the object required error at the same line..

        Sub AddNewClaims()
    '
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims to only xxxx
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="xxxx"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            Range("A2:AE5000" & LastRow).Copy
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
        If Not CurrClaims Is Nothing Then
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
    
        End If
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    You'll have to attach the workbook.

  9. #9
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Okay - will just have to see if I am allowed given it's an internal document. I'll see if I can provide it if I blank out any identifying information... just give me a moment and I'll find out!

  10. #10
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Alrighty, here it is, all blanked out but hopefully you will get the drift...
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    and here's what it does when there are actually claims to move:
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    I'm a little confused as the second file runs fine once, then errors out on a different part of the code

  13. #13
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    I think that would be because (I am presuming) the second time the macro is run, there is no data to select in "new claims". This won't ever be an issue because there will always be data in "new claims" to import into "current claims"; there just won't always be data to select from "current claims" to move into "old claims".

    That seems to be the issue, when there's claims to move from current to old, it works okay but as soon as there is nothing and no range to select, it gets upset.

  14. #14
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    OK - assuming there will always be at least headings in Row 1, try putting this code as the very first lines of your code, i.e. immediatley under the AddNewClaims macro heading:

    If Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then
            MsgBox "There are no new claims to be transferred!!", vbExclamation, "Transfer Claim Data Editor"
            Exit Sub
        End If
    Last edited by Trebor76; 02-26-2014 at 10:18 PM.

  15. #15
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    I presume this is what you meant, but it did the same thing and threw an error on the same line (object required error)

    'delete old claims and move to old claims tab
        If Not CurrClaims Is Nothing Then
            CurrClaims.EntireRow.Select


        Sub AddNewClaims()
        
        If Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then
            MsgBox "There are no new claims to be transferred!!", vbExclamation, "Transfer Claim Data Editor"
            Exit Sub
        End If
    
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    'Dim NewClaims, CurrClaims, CurrNewClaims, PrevClaims, Rng, PrevClaimsLastRow, OrigRow As Range
    'Dim FirstRow, LastRow, iX As Integer
    
    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="anoctua"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            Range("A2:AE5000" & LastRow).Copy
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
        If Not CurrClaims Is Nothing Then
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("a2", ActiveSheet.Range("a65536").End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:= _
            Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
    
        End If
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("e1", ActiveSheet.Range("e65536").End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
        End Sub

  16. #16
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    It's do with the 'CurrClaims' range playing up. Try the following where I simply reset that named range (I've also changed the way the last row if found as your method is based on Excel 2003 where you could get caught out):

    Option Explicit
    Sub AddNewClaims()
        
        If Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then
            MsgBox "There are no new claims to be transferred!!", vbExclamation, "Transfer Claim Data Editor"
            Exit Sub
        End If
    
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    'Dim NewClaims, CurrClaims, CurrNewClaims, PrevClaims, Rng, PrevClaimsLastRow, OrigRow As Range
    'Dim FirstRow, LastRow, iX As Integer
    
    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range, _
            ToClear As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="anoctua"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            Range("A2:AE5000" & LastRow).Copy 'Trebor76 - not sure about this.  I suspose it works because 'LastRow' at this stage is zero??
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
            Set CurrClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            CurrClaims.EntireRow.Select
            Application.CutCopyMode = False
            Selection.Copy
            Sheets("Old Claims").Select
            Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert

  17. #17
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thanks Rob, I wish I could say it worked! I can see what you've done but unfortunately now it moves all the claims to "old claims" when it should only move the claims which are on the current claims but not in the new claims when they're imported.

    So essentially the sheet does this when the macro is run:
    - pastes all new claims into current claims
    - looks for duplicates and with those gets the data from the old current (duplicated) claims in columns from AG- AH and assigns them to the new duplicates and does the same fr column H but applies that to the respective claim in column I
    - it then clears the old duplicates
    - it also pastes any new, non duplicated claims into current claims

    I'm then left with two ranges - new duplicates and new claims down the bottom and any "old claims" up the top. The old claims are any left over claims that were not on the new claims list. The new claims are any duplicates plus any claims that were not already in current claims.

    - it then removes any blank lines from the old claims range and puts all that data into the old claims tab.

    However when there are no old claims that range does not exist and so the macro has an error (from my understanding).

    I thought if I could tell it that if the old current claims range was not there then to just sort by dwc and so on and a forth but if it was there then to execute the process as per the macro. But my if statement attempts never worked.

    I really wish I could solve this

  18. #18
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    OK last try - this will only run the problematic section if there's no error in trying to select the row(s) from the 'CurrClaims' range:

    Option Explicit
    Sub AddNewClaims()
        
        If Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then
            MsgBox "There are no new claims to be transferred!!", vbExclamation, "Transfer Claim Data Editor"
            Exit Sub
        End If
    
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    'Dim NewClaims, CurrClaims, CurrNewClaims, PrevClaims, Rng, PrevClaimsLastRow, OrigRow As Range
    'Dim FirstRow, LastRow, iX As Integer
    
    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range, _
            ToClear As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="anoctua"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            Range("A2:AE5000" & LastRow).Copy 'Trebor76 - not sure about this.  I suspose it works because 'LastRow' at this stage is zero??
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
        On Error Resume Next
            CurrClaims.EntireRow.Select
            If Err.Number = 0 Then
                Application.CutCopyMode = False
                Selection.Copy
                Sheets("Old Claims").Select
                Range("A" & Rows.Count).End(xlUp).Offset(1).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
        Err.Clear
        On Error GoTo 0
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
         
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
    
    End Sub
    Robert
    Last edited by Trebor76; 02-27-2014 at 01:32 AM.

  19. #19
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Actually ignore what I said, it's not supposed to select old claims, so what you did was great however if there is no range to select then it should skip to this bit (because it won't need to execute any processes in the "old claims" tab as it won't be adding any old claims...

    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
    
    End Sub

  20. #20
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Okay I have changed it slightly and now it seems to run... touch wood. Did you want to have a look and see what you think?

    Option Explicit
    Sub AddNewClaims()
        
        If Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then
            MsgBox "There are no new claims to be transferred!!", vbExclamation, "Transfer Claim Data Editor"
            Exit Sub
        End If
    
    'Save a Backup
        Dim relativePath As String
            relativePath = ThisWorkbook.Path & "\" & ActiveWorkbook.Name
            On Error Resume Next
            MkDir ThisWorkbook.Path & "\Backups"
            On Error GoTo 0
            relativePath = ThisWorkbook.Path & "\Backups\" & ActiveWorkbook.Name
            ActiveWorkbook.SaveCopyAs Filename:=relativePath & Format(Now, "d-mmm-yy hh-mm-ss")
    
    'Dim NewClaims, CurrClaims, CurrNewClaims, PrevClaims, Rng, PrevClaimsLastRow, OrigRow As Range
    'Dim FirstRow, LastRow, iX As Integer
    
    Dim NewClaims As Range, _
            CurrClaims As Range, _
            CurrNewClaims As Range, _
            PrevClaims As Range, _
            Rng As Range, _
            PrevClaimsLastRow As Range, _
            OrigRow As Range, _
            ToClear As Range
        
        Dim FirstRow As Long, _
            LastRow As Long, _
            iX As Long
    
    'turn screen updating off
            Application.ScreenUpdating = False
    
    'Filter new claims
            Sheets("New Claims").Select
            Rows("1:1").Select
            Selection.AutoFilter
            ActiveSheet.Range("$A$1:$AI$5000").AutoFilter Field:=1, Criteria1:="Bendigo - K. Graham"
            
    'insert new column to accomodate for old DWC
            Columns("I:I").Select
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
       
    'Move New Claims to Current Claims
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            Range("A2:AE5000" & LastRow).Copy 'Trebor76 - not sure about this.  I suspose it works because 'LastRow' at this stage is zero??
            Sheets("Current Claims").Activate
        
     'remove data validation (work-around to enable macro to work on Excel 2003)
            Columns("AG:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
            :=xlBetween
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        If Cells(2, 1) <> "" Then
        Set CurrClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        Else: Set CurrClaims = Cells(2, 1)
        End If
            FirstRow = CurrClaims.Row
            LastRow = CurrClaims(CurrClaims.Count).Row
            Cells(LastRow, 1).Offset(1, 0).Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
     
    'Re-set data validation
            Range("AG:AG").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Y,N"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
        
    Columns("AH:AH").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="Save, Likely Save, Breached"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
     
    'Clear New Claims tab
            Sheets("New Claims").Select
            Selection.AutoFilter
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
            LastRow = NewClaims(NewClaims.Count).Row
        Set ToClear = ActiveSheet.Range("A2:AE" & LastRow)
            ToClear.ClearContents
            
    'Delete Column I from New Claims Tab
            Columns("I:I").Select
            Selection.Delete Shift:=xlToLeft
          
            Columns("A:AE").Locked = False
            Columns("AF:AH").Locked = True
                   
    ' Find duplicates and clear
        Sheets("Current Claims").Activate
    
    'Set the CurrNewClaims variable to include all the current plus new claims
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
    
    'update the new claim with the user entered values from the old
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Range("AG" & iX & ":AI" & iX).Value = Range("AG" & OrigRow.Row & ":AI" & OrigRow.Row).Value
            Range("I" & iX).Value = Range("H" & OrigRow.Row).Value
    
    'delete "old" claims
            Rows(OrigRow.Row).ClearContents
        End If
        Next
            CurrNewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    'delete old claims and move to old claims tab
        On Error Resume Next
            CurrClaims.EntireRow.Select
            If Err.Number = 0 Then
                Application.CutCopyMode = False
                Selection.Copy
                Sheets("Old Claims").Select
                Range("A" & Rows.Count).End(xlUp).Offset(1).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    'add date to existing selection
            Selection.EntireRow.Columns("AJ").Value = Date
        
    'Remove duplicated claims and update with recent data
        Set CurrNewClaims = ActiveSheet.Range("E1", ActiveSheet.ActiveSheet.Range("E" & Rows.Count).End(xlUp))
        For iX = CurrNewClaims.Rows.Count To 2 Step -1
        If WorksheetFunction.CountIf(CurrNewClaims, Cells(iX, 5).Value) > 1 Then
        Set OrigRow = CurrNewClaims.Find(Cells(iX, 5).Value, LookIn:=xlValues, lookat:=xlWhole)
            Rows(OrigRow.Row).ClearContents
        End If
        Next
        
        'delete all blank rows
        Set NewClaims = ActiveSheet.Range("A2", ActiveSheet.Range("A" & Rows.Count).End(xlUp))
        FirstRow = NewClaims.Row
        LastRow = NewClaims(NewClaims.Count).Row
        On Error Resume Next
        NewClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
        'Selection.EntireRow.ClearContents
            
    'sort by DWC at closure and move back to "current claims" tab
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Old Claims").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
            Sheets("Current Claims").Activate
            
        End If
        On Error Resume Next
        
    'delete all blank rows from "current claims" tab
        Set CurrClaims = ActiveSheet.Range("E1", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
            Selection.ClearContents
            CurrClaims.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            Range("AG2:AG5000").ClearContents
    
    'sort remaining claims in ascending order by current DWC
            Cells.Select
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Current Claims").Sort.SortFields.Add Key:=Range( _
            "H2:H844"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Current Claims").Sort
            .SetRange Range("A1:AI844")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    'come back to current claims tab
        Sheets("Current Claims").Activate
        
    'turn screen updating on
    Application.ScreenUpdating = True
    
    End Sub
    And yes I am not sure about this part either, I didn't design the code, I've just modified it to accomodate tweaks I've made to the sheet.
            Range("A2:AE5000" & LastRow).Copy 'Trebor76 - not sure about this.  I suspose it works because 'LastRow' at this stage is zero??
            Sheets("Current Claims").Activate
    would you suggest something different?


    Thank you so much for your help, I appreciate it a lot!

  21. #21
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Were you able to run that macro? I've put your code in there and the first time it did something odd - instead of adding the dates when the data went into "old claims" it added the dates in the new claims. I don't know why this was, so I thought I'd run it again into a new sheet. I stepped into the macro and note that it appears not to be selecting the old claims sheet now.

    Sheets("Old Claims").Select
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
    On Error Resume Next

    I'm suspecting it has something to do with the error code??

  22. #22
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    Okay I have changed it slightly and now it seems to run... touch wood. Did you want to have a look and see what you think?
    You know better than I if the expectyed results are what they should be so hopefully we're on the right track. Just bear in mind that the statement On Error Resume Next doesn't fix anything - it merely stops error messages from appearing that's why I turn it back on via On Error Goto 0 which you've removed.

    would you suggest something different?
    Yes, try this which finds the last row used regardles of what column it resides in:

    Range("A2:AE" & Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row).Copy
    If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert
    Last edited by Trebor76; 02-27-2014 at 06:14 PM.

  23. #23
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    fantastic thank you Robert. I guess I should use that type of code wherever it's trying to select entire rows?

  24. #24
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Creating IF statement

    I guess I should use that type of code wherever it's trying to select entire rows?
    Normally I check if the range is empty or not, but yes what I've written is another way of achieving the same result.

    I'm glad we were able to get it sorted for you

    Robert

  25. #25
    Registered User
    Join Date
    02-26-2014
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Creating IF statement

    Thank you so much, you've been such a champ! I am really glad we resolved it, I was starting to get grey hair!

+ 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] Creating an if statement
    By brianjones00 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-30-2013, 04:30 PM
  2. Creating an If/then statement in Excel
    By IheartIUP in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-12-2012, 01:40 PM
  3. Help Creating a IF Statement
    By markhuges in forum Excel General
    Replies: 7
    Last Post: 10-30-2011, 04:00 AM
  4. Creating an If statement
    By twatkins513 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2010, 02:40 PM
  5. Creating a .bmp file with Put statement
    By incjourn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2009, 01:12 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