I am running Excel 2007 and writing code that both 2007 and 2003 users run.
I have a report that sometimes list multiple occurrences of a condition. I have written a routine that searches the data for duplicates and returns the duplicates in a ListBox. I can then choose which of the duplicate items I want to use in my final report.
The Problem:
The ListBox works great the first time and I can select and item (line items becomes blue when I click on it in the ListBox); however, when the code continues with a loop and comes around to the ListBox again, new data appears like I want to have happen, but I cannot select any item in the ListBox (will not turn blue when I click on an item and the code will not continue).
Any ideas why this is happening? Can you help me correct or change my code?
I've included code below. The ListBox code is in the Run_Form subroutine.
In addition, here is the UserForm code:
Thank you,
Charles
I have a report that sometimes list multiple occurrences of a condition. I have written a routine that searches the data for duplicates and returns the duplicates in a ListBox. I can then choose which of the duplicate items I want to use in my final report.
The Problem:
The ListBox works great the first time and I can select and item (line items becomes blue when I click on it in the ListBox); however, when the code continues with a loop and comes around to the ListBox again, new data appears like I want to have happen, but I cannot select any item in the ListBox (will not turn blue when I click on an item and the code will not continue).
Any ideas why this is happening? Can you help me correct or change my code?
I've included code below. The ListBox code is in the Run_Form subroutine.
Code:
Private Sub Start_Search()
Range("E1").Select
Call Add_Data
End Sub
--------------------------------------------------------------------------
Private Sub Add_Data()
Do
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "ARCT00615" Then
Application.Run "Search_Dups"
Workbooks("Raw Data.xls").Activate
On Error Resume Next
x = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -4).Value, _
Workbooks("ARCT00615_BOM.xls").Worksheets("ARCT00615") _
.Range("A1:I65536"), 7, False)
If Err.Number <> 0 Then
x = ""
End If
ActiveCell.Offset(0, 16).Value = x
x = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -4).Value, _
Workbooks("ARCT00615_BOM.xls").Worksheets("ARCT00615") _
.Range("A1:I65536"), 6, False)
If Err.Number <> 0 Then
x = ""
End If
ActiveCell.Offset(0, 17).Value = x
End If
Loop Until ActiveCell.Value = ""
End Sub
--------------------------------------------------------------------------
Private Sub Search_Dups()
On Error GoTo 0
MyStr = ActiveCell.Offset(0, 11).Value
Workbooks("ARCT00615_BOM.xls").Activate
Range("H:H").Select
Set SRng = Selection
x = Application.WorksheetFunction.CountIf(SRng, MyStr)
If x > 1 Then
Selection.Find(What:=MyStr, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("A10").Value = ActiveCell.Offset(0, -6).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("B10").Value = ActiveCell.Offset(0, -3).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("C10").Value = ActiveCell.Offset(0, -2).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("D10").Value = ActiveCell.Offset(0, -1).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("E10").Value = ActiveCell.Value
Selection.Find(What:=MyStr, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("A11").Value = ActiveCell.Offset(0, -6).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("B11").Value = ActiveCell.Offset(0, -3).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("C11").Value = ActiveCell.Offset(0, -2).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("D11").Value = ActiveCell.Offset(0, -1).Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data") _
.Range("E11").Value = ActiveCell.Value
Workbooks("Raw Data.xls").Worksheets("Temp_Data").Activate
Cells.Select
Cells.Columns.AutoFit
Range("A1").Select
ActiveCell.Value = Range("A10").Value & "-" & Range("B10").Value _
& "-" & Range("C10").Value & "-" & Range("D10").Value _
& "-" & Range("E10").Value
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = Range("A11").Value & "-" & Range("B11").Value _
& "-" & Range("C11").Value & "-" & Range("D11").Value _
& "-" & Range("E11").Value
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Set SRng2 = Selection
Call Run_Form
End If
End Sub
Private Sub Run_Form()
With UserForm1.ListBox1
.BoundColumn = 1
.ColumnCount = 1
.ControlSource = "'Temp_Data'!A6"
.IntegralHeight = True
.MultiSelect = 0
.RowSource = SRng2.Address
End With
If TypeName(Selection) = "Range" Then
UserForm1.Show
End If
End Sub
--------------------------------------------------------------------------
Private Sub Record_Choice1()
Sheets("Raw Data").Activate
ActiveCell.Offset(0, 16).Value = Sheets("Temp_Data").Range("D10").Value
ActiveCell.Offset(0, 17).Value = Sheets("Temp_Data").Range("C10").Value
Call Clear_Temp_Data
End Sub
--------------------------------------------------------------------------
Private Sub Record_Choice2()
ActiveCell.Offset(0, 16).Value = Sheets("Temp_Data").Range("D11").Value
ActiveCell.Offset(0, 17).Value = Sheets("Temp_Data").Range("C11").Value
Call Clear_Temp_Data
End Sub
--------------------------------------------------------------------------
Private Sub Clear_Temp_Data()
Sheets("Temp_Data").Activate
Cells.Select
Cells.ClearContents
Selection.ColumnWidth = 8.43
Range("A1").Select
Sheets("Raw Data").Select
SRng2.Clear
Call Add_Data
End Sub
In addition, here is the UserForm code:
Code:
Private Sub ContinueButton1_Click()
If Range("$A$6").Value = Range("$A$1").Value Then
Unload UserForm1
Application.Run "Record_Choice1"
End If
If Range("$A$6").Value = Range("$A$2").Value Then
Unload UserForm1
Application.Run "Record_Choice2"
End If
End Sub
Thank you,
Charles