Lee Francis
New Member
Hi Guys,
I hope you can help
I have some code which works fine but doesn't fully complete the task at hand. I need to either add to this code or call another macro to complete the job.
I'm struggling to do this. Each time I try I am failing. The last function of the code is a next loop and I think that when I try to call the macro at the end of the sub it is simply looping backthrough the next loop.
Has anyone experienced this before or can you help with this?
I have pasted the code below and I can send the spreadsheet to anyone who needs to see in detail
Thanks for your help guys
Lee
Sub CommandButton1_Click()
Sheets(2).Visible = xlVeryHidden
Dim a, b, c, ws, wb, d, e, f, i
On Error GoTo err
d = Split(Sheets(2).Range("D1".Value, ";"
a = WorksheetFunction.Match(Sheets(1).Range("B3".Value, Sheets(2).Range("B:b", 0)
If Len(a) = 1 Then
a = "0" & WorksheetFunction.Match(Sheets(1).Range("B3".Value, Sheets(2).Range("B:B", 0)
End If
If Len(Sheets(1).Range("B4".Value) = 1 Then a = "0" & Sheets(1).Range("B4".Value & "." & a
If Len(Sheets(1).Range("B4".Value) = 2 Then a = Sheets(1).Range("B4".Value & "." & a
a = a & "." & Sheets(1).Range("B2".Value & ".xlsx"
b = Sheets(1).Range("B5".Value
If b = "" Then b = "."
If Right(b, 1) <> "" Then b = b & ""
Sheets(2).Range("E:E".ClearContents
Call dirfiles(b)
c = Sheets(2).Range("E65536".End(xlUp).Row
If c = 1 Then GoTo nofiles
For Each cell In Sheets(2).Range("E1:E" & c)
If Right(cell.Value, 15) <> a Then cell.Value = ""
Next
a = Left(a, Len(a) - 5)
Application.DisplayAlerts = False
On Error Resume Next
Sheets(a).Delete
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = a
Application.DisplayAlerts = True
Sheets(a).Range("A1".Value = "DDR vs QM for " & a
Sheets(a).Range("A1".EntireColumn.ColumnWidth = 12
Sheets(a).Range("A3".Value = "Delivery Note Number"
Set ws = Sheets(a)
ws.Activate
For Each ccel In Sheets(2).Range("E1:E" & c)
If ccel.Value = "" Then GoTo nextccel
wb = ccel.Value
Workbooks.Open Filename:=b & wb, UpdateLinks:=False, ignorereadonlyrecommended:=True
Workbooks(wb).Sheets(1).UsedRange.MergeCells = False
i = 1
For i = 1 To 5
Set e = Workbooks(wb).Sheets(d(i)).UsedRange.Find(What:="Delivery Note", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
f = e.Row + 1
e = Mid(e.Address, InStr(e.Address, "$" + 1, InStr(2, e.Address, "$" - 2)
Workbooks(wb).Sheets(d(i)).Range(e & f & ":" & e & Workbooks(wb).Sheets(d(i)).Range(e & "65536".End(xlUp).Row).Copy
ThisWorkbook.Sheets(a).Range("A65536".End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next i
Workbooks(wb).Close savechanges = False
nextccel:
Next ccel
ThisWorkbook.Sheets(a).Range("A2".Select
Exit Sub
nofiles:
MsgBox "No files with name " & Chr(34) & "*" & a & Chr(34) & " in folder " & Chr(34) & b & Chr(34) & " found. Nothing processed."
Exit Sub
err:
MsgBox err.Description
End Sub
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
If Target.Value = "February" And Target.Offset(1, 0).Value > 28 Then Target.Offset(1, 0).Value = 28
If Target.Value = "April" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "June" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "September" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "November" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
End If
If Target.Address = "$B$4" Then
If Target.Value = 31 And Target.Offset(-1, 0).Value = "November" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "September" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "June" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "April" Then Target.Value = 30
If Target.Value > 28 And Target.Offset(-1, 0).Value = "February" Then Target.Value = 28
End If
End Sub
Sub dirfiles(b)
Set myobject = New Scripting.FileSystemObject
Set mysource = myobject.getfolder(b)
On Error Resume Next
irow = 2
For Each myfile In mysource.Files
Sheets(2).Cells(irow, 5).Value = myfile.Name
irow = irow + 1
Next
End Sub
I hope you can help
I have some code which works fine but doesn't fully complete the task at hand. I need to either add to this code or call another macro to complete the job.
I'm struggling to do this. Each time I try I am failing. The last function of the code is a next loop and I think that when I try to call the macro at the end of the sub it is simply looping backthrough the next loop.
Has anyone experienced this before or can you help with this?
I have pasted the code below and I can send the spreadsheet to anyone who needs to see in detail
Thanks for your help guys
Lee
Sub CommandButton1_Click()
Sheets(2).Visible = xlVeryHidden
Dim a, b, c, ws, wb, d, e, f, i
On Error GoTo err
d = Split(Sheets(2).Range("D1".Value, ";"
a = WorksheetFunction.Match(Sheets(1).Range("B3".Value, Sheets(2).Range("B:b", 0)
If Len(a) = 1 Then
a = "0" & WorksheetFunction.Match(Sheets(1).Range("B3".Value, Sheets(2).Range("B:B", 0)
End If
If Len(Sheets(1).Range("B4".Value) = 1 Then a = "0" & Sheets(1).Range("B4".Value & "." & a
If Len(Sheets(1).Range("B4".Value) = 2 Then a = Sheets(1).Range("B4".Value & "." & a
a = a & "." & Sheets(1).Range("B2".Value & ".xlsx"
b = Sheets(1).Range("B5".Value
If b = "" Then b = "."
If Right(b, 1) <> "" Then b = b & ""
Sheets(2).Range("E:E".ClearContents
Call dirfiles(b)
c = Sheets(2).Range("E65536".End(xlUp).Row
If c = 1 Then GoTo nofiles
For Each cell In Sheets(2).Range("E1:E" & c)
If Right(cell.Value, 15) <> a Then cell.Value = ""
Next
a = Left(a, Len(a) - 5)
Application.DisplayAlerts = False
On Error Resume Next
Sheets(a).Delete
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = a
Application.DisplayAlerts = True
Sheets(a).Range("A1".Value = "DDR vs QM for " & a
Sheets(a).Range("A1".EntireColumn.ColumnWidth = 12
Sheets(a).Range("A3".Value = "Delivery Note Number"
Set ws = Sheets(a)
ws.Activate
For Each ccel In Sheets(2).Range("E1:E" & c)
If ccel.Value = "" Then GoTo nextccel
wb = ccel.Value
Workbooks.Open Filename:=b & wb, UpdateLinks:=False, ignorereadonlyrecommended:=True
Workbooks(wb).Sheets(1).UsedRange.MergeCells = False
i = 1
For i = 1 To 5
Set e = Workbooks(wb).Sheets(d(i)).UsedRange.Find(What:="Delivery Note", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
f = e.Row + 1
e = Mid(e.Address, InStr(e.Address, "$" + 1, InStr(2, e.Address, "$" - 2)
Workbooks(wb).Sheets(d(i)).Range(e & f & ":" & e & Workbooks(wb).Sheets(d(i)).Range(e & "65536".End(xlUp).Row).Copy
ThisWorkbook.Sheets(a).Range("A65536".End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next i
Workbooks(wb).Close savechanges = False
nextccel:
Next ccel
ThisWorkbook.Sheets(a).Range("A2".Select
Exit Sub
nofiles:
MsgBox "No files with name " & Chr(34) & "*" & a & Chr(34) & " in folder " & Chr(34) & b & Chr(34) & " found. Nothing processed."
Exit Sub
err:
MsgBox err.Description
End Sub
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
If Target.Value = "February" And Target.Offset(1, 0).Value > 28 Then Target.Offset(1, 0).Value = 28
If Target.Value = "April" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "June" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "September" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
If Target.Value = "November" And Target.Offset(1, 0).Value > 30 Then Target.Offset(1, 0).Value = 31
End If
If Target.Address = "$B$4" Then
If Target.Value = 31 And Target.Offset(-1, 0).Value = "November" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "September" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "June" Then Target.Value = 30
If Target.Value = 31 And Target.Offset(-1, 0).Value = "April" Then Target.Value = 30
If Target.Value > 28 And Target.Offset(-1, 0).Value = "February" Then Target.Value = 28
End If
End Sub
Sub dirfiles(b)
Set myobject = New Scripting.FileSystemObject
Set mysource = myobject.getfolder(b)
On Error Resume Next
irow = 2
For Each myfile In mysource.Files
Sheets(2).Cells(irow, 5).Value = myfile.Name
irow = irow + 1
Next
End Sub