Microsoft
Software
Hardware
Network
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").E
nd(xlUp).R
ow
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-s
ales - For Input").Rows(header).Entir
eRow.Copy _
wb.Sheets("sheet1").Rows(h
eader)
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(i
Row)
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
Random Solutions
Sending a message "on behalf of" without Exchange
Tree View HOWTO
strName and msgbox
linking DBF with filename size greater than 8 characters in Access
How to use a module when referencing a calander
edit ad add to xml fie
Exchage Error SPAM question.
Can install Money update in Windows 7
Using Range(Cells(r1,c1),Cells(r<wbr />2,c2)) instead of Range("A1:B2")
I have 9 Cursore need to make one report