could you please help with the following code. It works fantastic however if the name is in the pupil list but not on a register it debugs. easy way to sort this is to delete the name in the pupil list and restart. It would be helpful if someone could help do either of the following with explanation to help me with my coding. Thank you
1. Ignore if the name doesn't exist in the list and move on
2. have a popup that says "this name "insert name" not found, would you like to delete from the list and proceed?"
here is my code
1. Ignore if the name doesn't exist in the list and move on
2. have a popup that says "this name "insert name" not found, would you like to delete from the list and proceed?"
here is my code
VBA Code:
'search for pupils names and joins them together
Sub SearchPupils()
'Declare variables
Dim cc As Range
With Sheets("Pupils")
For Each cc In .Range("A3", "A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
CopyToMaster _
FullName:=Join(Array(Trim(cc.Offset(, 1)), Trim(cc)), " ")
Next cc
End With
End Sub
Sub CopyToMaster(FullName As String)
'Declare some variables
Dim fso As Object, fldr As Object, fl As Object
Dim cc As Range
Dim sht As Worksheet
Dim InRegister As String
Dim nr As Long
'Turn off Screen Updating
Application.ScreenUpdating = False
'Create objects to work with File System
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder(ThisWorkbook.Path)
'Set Number of first output row in Invoice
nr = 13
'// Loop through each file in the folder whos name begins with "Register_"
'// and then search Range B8:B128 of each sheet in those files for the value
'// passed in as FullName
For Each fl In fldr.Files
If InStr(fso.GetBaseName(fl), "Register_") Then
With Workbooks.Open(fl.Path, True, True)
For Each sht In .Sheets
For Each cc In sht.Range("B8:B172")
If Join(Array(cc.Value, cc.Offset(, -1).Value), " ") = FullName Then
With ThisWorkbook
.Activate
'// Fill out the Invoice with information found in the files
With .Sheets("Invoice")
.Activate
.Range("B9") = FullName '// INSERT NAME UNDER BILL TO
.Range("B" & nr).Select '// SELECT FIRST OUTPUT ROW
With ActiveCell
.Value = sht.Range("A2") '// DESCRIPTION
.Offset(, 1) = sht.Range("O" & cc.Row) '// NO. OF SESSIONS
InRegister = Left(.Value, Len(.Value) - (Len(.Value) - InStrRev(.Value, " Week") + 1))
.Offset(, 2) = Sheets("Home").Range("CostPerSession").Find(What:=InRegister, LookIn:=xlValues).Offset(, 1) '// COST PER SESSION
.Offset(1).Select '// MOVE DOWN ONE ROW
End With
End With
End With
nr = nr + 1
Exit For
End If
Next cc
Next sht
.Close _
SaveChanges:=False
End With
End If
Next fl
'// Save Invoice
SaveInvWithNewName
'// Turn on Screen Updating
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: