Say I wanted to set the column width for column B of each of the generated sheets, I would insert
Columns("B").ColumnWidth = 25
So I'd end up with
Option Explicit
Sub test()
Dim dic As Object, myDate, a, i As Long, e, flg As Boolean, x As Range, t As Long
Dim r As Range
Set r = Application.InputBox("Select cell that has the date to pick", Type:=8)
If Not IsDate(r.Value) Then
MsgBox "Invalid entry": Exit Sub
End If
myDate = r.Value
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
With Sheets("sheet1").Range("a3").CurrentRegion
a = .Value
For i = 2 To UBound(a, 1)
For Each e In Array(4, 8, 12)
If a(i, e) = myDate Then
flg = True: Exit For
End If
Next
If flg Then
If x Is Nothing Then Set x = .Rows(1).Columns("a:b")
If Not dic.exists(a(i, 1)) Then
Set dic(a(i, 1)) = .Rows(1).Columns("a:b")
End If
Set dic(a(i, 1)) = Union(dic(a(i, 1)), .Rows(i).Columns("a:b"))
Set x = Union(x, .Rows(i).Columns("a:b")): flg = False
End If
Next
End With
If Not x Is Nothing Then
With Workbooks.Add
x.Copy .Sheets(1).Range("a3")
With .Sheets(1)
.Range("c1").Value = myDate
.Range("c1").Font.Bold = True
With .Range("a3").CurrentRegion
End With
.Columns.AutoFit
Columns("B").ColumnWidth = 25
End With
For i = 0 To dic.Count - 1
If .Sheets.Count < i + 2 Then
.Sheets.Add after:=.Sheets(.Sheets.Count)
End If
With .Sheets(i + 2)
dic.items()(i).Copy .Range("a5")
.Range("d2").Value = myDate
.Range("a3").Value = "Name:"
.Range("e3").Value = "Delivery No:"
.Range("a3,d2,e3").Font.Bold = True
.Columns.AutoFit
Columns("B").ColumnWidth = 25
.Name = CStr(dic.keys()(i))
End With
Next
.SaveAs Replace(ThisWorkbook.Name, ".xls", " " & Format$(myDate, "yyyymmdd") & ".xls")
End With
Else
MsgBox "No data"
End If
End Sub
But it only appears to set Column B to 25 for Sheet 1, and then misses the 2nd and 3rd sheet, then the rest are set to 25. Have I also to insert that code at another point?
Bookmarks