|
Question : Help DEBUG excel macro code!!
|
|
The following macro is not working correctly. I can't figure out for the life of me what is wrong with the code. I get a run-time error on "If c.Value = m Then" line with a c.Value error of 2042.
Sub Split_Files() Dim cMngr As Collection Dim iRow As Long Dim rng As Range Dim c As Range Dim m As Variant Dim wb As Workbook
iRow = Sheets("Non-sales - For Input").Range("bu65536").End(xlUp).Row Set rng = Sheets("Non-sales - For Input").Range("bu15:bu" & iRow)
'build collection of unique managers Set cMngr = New Collection For Each c In rng cMngr.Add c.Value 'duplicates won't be added Next c
Application.DisplayAlerts = False For Each m In cMngr 'create a new workbook Set wb = Application.Workbooks.Add For header = 1 To 14 ThisWorkbook.Sheets("Non-sales - For Input").Rows(header).EntireRow.Copy _ wb.Sheets("sheet1").Rows(header) Next header iRow = 14 For Each c In rng If c.Value = m Then iRow = iRow + 1 'copy data to new workbook c.EntireRow.Copy wb.Sheets("sheet1").Rows(iRow) End If Next c 'close the new workbook and save it wb.Close True, ThisWorkbook.Path & "\" & m Next m
'clean up Set wb = Nothing Set cMngr = Nothing Set c = Nothing Set rng = Nothing Application.DisplayAlerts = True End Sub
|
|
Answer : Help DEBUG excel macro code!!
|
|
Hi,
In order to really get rid of duplicates, you would have to do this:
On Error Resume Next For Each c In rng cMngr.Add c.Value, CStr(c.Value) 'duplicates won't be added Next c On Error GoTo 0
The Cstr(c.value) is the key string to the collection. If it tries to insert the same key twice, it will give you an error. To control the error just add the 'On error resume next' and at the end you add 'On error goto 0'.
Could you just try that out!
Phetu
|
|
|
|