treating by handler error based on two condition when shows errors

leap out

Active Member
Joined
Dec 4, 2020
Messages
288
Office Version
  1. 2016
  2. 2010
hi experts
I want fixing two errors
first if I have two sheets are empty , no data then will shows application defined error in this line
VBA Code:
Set rRng = rRng.Resize(Columnsize:=rRng.Columns.Count - 1)
I want get rid of this error by using procedure handler error , and pops up message " the sheets are empty ,please make sure the sheets aree filled " and nothing happens .
second when I have some columns are missed will sows error invalid procedure call or argument in this line
VBA Code:
.UsedRange.RemoveDuplicates Columns:=Array(2, 3, 4), Header:=xlYes
because I have missed column .
I want get rid of this error by using procedure handler error , and pops up message " make sure the columns are not exised " and nothing happens
this is the whole code
VBA Code:
Sub CreateSummary()
  Const sSum = "SUMMARY"
 
  Dim wSales1 As Worksheet, wSales2 As Worksheet, wSum As Worksheet
  Dim rRng As Range, rTarg As Range
  Dim lLastRow1 As Long, lLastRow2  As Long, lLastRowS As Long
  Dim sName As String, sFrm As String
 
  With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .EnableEvents = False
  End With
 
  'set up the sheet variables
  'assume the first 2 sheets are the sales sheets
  Set wSales1 = ThisWorkbook.Sheets(1)
  Set wSales2 = ThisWorkbook.Sheets(2)
 
  'see if the Summary sheet exists
  'delete it if it does
  If SheetExists(ThisWorkbook, sSum) Then
    ThisWorkbook.Sheets(sSum).Delete
  End If
  'and then recreate it
  With ThisWorkbook
    .Sheets.Add After:=.Sheets(.Sheets.Count)
    ActiveSheet.Name = sSum
  End With
 
  Set wSum = ThisWorkbook.Sheets(sSum)
 
  'start copying the data from the 2 sheets to be combined into the Summary sheet
  With wSales1
    'turn off autofilter mode
    .AutoFilterMode = False
   
    Set rRng = .Range("A1").CurrentRegion
    lLastRow1 = rRng.Rows.Count
    'remove the Qty column for now
    Set rRng = rRng.Resize(Columnsize:=rRng.Columns.Count - 1)
    rRng.Copy
   
    'copy to the Summary sheet
    Set rTarg = wSum.Range("A1")
    rTarg.PasteSpecial Paste:=xlPasteColumnWidths
    rTarg.PasteSpecial Paste:=xlPasteAll
  End With
 
  With wSales2
    'turn off autofilter mode
    .AutoFilterMode = False
   
    Set rRng = .Range("A1").CurrentRegion
    lLastRow2 = rRng.Rows.Count
    'remove the Qty column for now
    Set rRng = rRng.Resize(Columnsize:=rRng.Columns.Count - 1)
    'remove the header row before copying next group of sales
    Set rRng = rRng.Offset(RowOffset:=1).Resize(Rowsize:=rRng.Rows.Count - 1)
    rRng.Copy
   
    'copy to the Summary sheet to the next available row
    Set rTarg = wSum.Range("A" & wSum.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
    rTarg.PasteSpecial Paste:=xlPasteColumnWidths
    rTarg.PasteSpecial Paste:=xlPasteAll
  End With
 
  With wSum
    'now remove duplicate rows on the Summary sheet
    'based on Brand/Type/Manufacture columns
    .UsedRange.RemoveDuplicates Columns:=Array(2, 3, 4), Header:=xlYes
 
    'and try and reset the last cell on the Summary sheet
    lLastRowS = .UsedRange.Rows.Count
    'calc the last row on the Summary sheet
    lLastRowS = .Range("A1").CurrentRegion.Rows.Count
   
    'it appears the item numbers are just sequential, may not be the same across sales sheets
    'renumber the items, assume the 1st item on the 1st sheet has value = 1
    .Range("A2").Value = 1
    .Range("A2").AutoFill Destination:=Range("A2:A" & lLastRowS), Type:=xlFillSeries
   
    'now the trickier part of adding the formulas needed to compare the summary sheets
    'first array formula to match Qty from first sales sheet
    '=IFERROR(INDEX('OIL SALES REPORT 1 JUN 2021'!$E$2:$E$19,MATCH(B2&C2&D2,'OIL SALES REPORT 1 JUN 2021'!$B$2:$B$19&
    '               'OIL SALES REPORT 1 JUN 2021'!$C$2:$C$19&'OIL SALES REPORT 1 JUN 2021'!$D$2:$D$19,0)),0)
   
    sName = wSales1.Name
    sFrm = "=IFERROR(INDEX('" & sName & "'!$E$2:$E$" & lLastRow1 & ",MATCH(B2&C2&D2,'" & sName & "'!$B$2:$B$" & lLastRow1 & "&"
    sFrm = sFrm & "'" & sName & "'!$C$2:$C$" & lLastRow1 & "&'" & sName & "'!$D$2:$D$" & lLastRow1 & ",0)),0)"
    .Range("H2").FormulaArray = sFrm
   
    'now a similar array formula for matching Qty from second sales sheet
    '=IFERROR(INDEX('OIL SALES REPORT 1 MAY 2021'!$E$2:$E$17,MATCH(B2&C2&D2,'OIL SALES REPORT 1 MAY 2021'!$B$2:$B$17&
    '               'OIL SALES REPORT 1 MAY 2021'!$C$2:$C$17&'OIL SALES REPORT 1 MAY 2021'!$D$2:$D$17,0)),0)
 
    sName = wSales2.Name
    sFrm = "=IFERROR(INDEX('" & sName & "'!$E$2:$E$" & lLastRow2 & ",MATCH(B2&C2&D2,'" & sName & "'!$B$2:$B$" & lLastRow2 & "&"
    sFrm = sFrm & "'" & sName & "'!$C$2:$C$" & lLastRow2 & "&'" & sName & "'!$D$2:$D$" & lLastRow2 & ",0)),0)"
    .Range("I2").FormulaArray = sFrm
   
    'now the Qty difference formula
    .Range("J2").Formula = "=H2-I2"
   
    'CASE formula - this will need to be converted to wingding font
    .Range("E2").Formula = "=IF(J2=0,""ü"",""û"")"
   
    'SURPLUS formula
    .Range("F2").Formula = "=IF(J2=0,""-"",IF(J2>0,J2,""""))"
   
    'DEFICIT formula
    .Range("G2").Formula = "=IF(J2=0,""-"",IF(J2<0,J2,""""))"
   
    'copy the formulas to the end of the sheet
    .Range("E2:J2").AutoFill Destination:=Range("E2:J" & lLastRowS), Type:=xlFillSeries
 
    'now copy back just the values, removing the formulas for the columns to be kept
    .Range("E2:G" & lLastRowS).Copy
    .Range("E2").PasteSpecial Paste:=xlPasteValues
   
    'do some housekeeping
    'first remove the intermediate formula columns
    .Columns("H:J").Delete
   
    'lastly some formatting
    .Range("A2").Copy
    .Range("E2:G" & lLastRowS).PasteSpecial Paste:=xlPasteFormats

    .Range("D1").Copy
    .Range("E1:G1").PasteSpecial Paste:=xlPasteFormats
   
    .Range("E1:G1").Value = Array("CASE", "SURPLASE", "DEFICIT")

    .Range("E2:E" & lLastRowS).Font.Name = "Wingdings"

    .Columns("E:G").ColumnWidth = 12
   
    'and try and reset the last cell on the Summary sheet
    lLastRowS = .UsedRange.Rows.Count
  End With
 
  With Application
    .CutCopyMode = False
    .GoTo wSum.Range("A1"), True
    .EnableEvents = True
    .ScreenUpdating = True
  End With

  MsgBox "Summary created"
 
  'for completeness
  Set wSales1 = Nothing: Set wSales2 = Nothing: Set wSum = Nothing
  Set rRng = Nothing: Set rTarg = Nothing
End Sub



'returns TRUE if the sheet exists in the active workbook
Private Function SheetExists(ByVal wBook As Workbook, ByVal sSheet As String) As Boolean
  Dim wSheet As Worksheet
 
  SheetExists = False
 
  On Error Resume Next
  Set wSheet = wBook.Sheets(sSheet)
  On Error GoTo 0
 
  SheetExists = Not wSheet Is Nothing
End Function 'SheetExists
I hope finding solution for theses errors.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top