Related
Sub FallOrSpringsemester()
Dim enrollPeriod As String
Dim i As Integer
Dim LastRow As Integer
Dim w As Worksheet
Dim text As String
Set w = Sheets.Add(after:=Sheets(Sheets.Count))
w.Name = "oldest Students"
Worksheets("oldest Students").Cells(1, 1) = "Student_ID"
Worksheets("oldest Students").Cells(1, 2) = "Enroll_Date"
Worksheets("oldest Students").Cells(1, 3) = "Program_Type_Name"
Worksheets("oldest Students").Cells(1, 4) = "Enrollment_Period"
LastRow = Worksheets("Base").Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Worksheets("oldest students").Cells(i, 1) = Worksheets("Base").Cells(i, 12)
Worksheets("oldest students").Cells(i, 2) = Worksheets("Base").Cells(i, 4)
Worksheets("oldest students").Cells(i, 3) = Worksheets("Base").Cells(i, 11)
Above this coding I ofc have my dimed variables and also coding for creating a new sheet.
I have trouble with this part as it will no do the calculations and tells me there is an error
"13": type miss match
If enrollPeriod Mod 2 = 0 Then
Worksheets("oldest Students").Cells(i, 2) = "Spring"
enrollPeriod = enrollPeriod + 1
Worksheets("oldest Students").Cells(i, 1) = 2018 - ((138 - enrollPeriod) / 2)
Else
Worksheets("oldest Students").Cells(i, 2) = "Fall"
Worksheets("oldest Students").Cells(i, 1) = 2018 - ((138-enrollPeriod) / 2)
End if
Next
End Sub
I think enrollPperiod should be an integer... that should fix it
I am attempting to draw data from a separate sheet and put it into a corresponding cell if the conditions are met. My code works, but it is not efficient. I do not know how to change the For Next loop so that it attempts to draw data only until the final entry. Right now I have it set to go a hundred or so cells further than I need so that I wouldn't have to update the code as often when I input new data to the data sheet (or at least that was the thought). Here is my code:
Sub LRearTest()
Dim R As Integer
Dim j As Integer
For j = 89 To 250
For R = 1 To 300
If Worksheets("Input").Cells(j, 22).Value >= Worksheets("1036L").Cells(R, 5).Value And Worksheets("Input").Cells(j, 22).Value <= Worksheets("1036L").Cells(R, 6).Value Then
Worksheets("Input").Cells(j, 20).Value = Worksheets("1036L").Cells(R, 3).Value
End If
Next R
Next j
End Sub
The problem is when I run this code it takes almost two minutes before it is over. I am not sure if it is because I have used j and r as integers or what. Also I have a dozen of these on one module so I am not sure if that contributes. The code works like I said, it is just far too slow. Help is greatly appreciated.
The point that I am checking is in Column V of Sheet "Input". Each of my columns that I want to populate, F - U, use the same data in column V. The sheets that I am comparing the data in column V against are labeled as 1030L, 1030R, 1031L, 1031R, 1032L, 1032R, 1033L, 1033R, 1034L, 1034R, 1034LA, 1034RA, 1035L, 1035R, 1036L, and 1036R. The data being compared is in the same columns in every sheet. Thank you
Something like this should work for you:
Sub LRearTest()
Dim wb As Workbook
Dim wsInput As Worksheet
Dim wsData As Worksheet
Dim aDataParams() As String
Dim aInput As Variant
Dim aData As Variant
Dim InputIndex As Long
Dim DataIndex As Long
Dim ParamIndex As Long
Dim MinCol As Long
Set wb = ActiveWorkbook
Set wsInput = wb.Sheets("Input")
'Adjust the column associations for each sheet as necessary
ReDim aDataParams(1 To 16, 1 To 3)
aDataParams(1, 1) = "1030L": aDataParams(1, 2) = "F"
aDataParams(2, 1) = "1030R": aDataParams(2, 2) = "G"
aDataParams(3, 1) = "1031L": aDataParams(3, 2) = "H"
aDataParams(4, 1) = "1031R": aDataParams(4, 2) = "I"
aDataParams(5, 1) = "1032L": aDataParams(5, 2) = "J"
aDataParams(6, 1) = "1032R": aDataParams(6, 2) = "K"
aDataParams(7, 1) = "1033L": aDataParams(7, 2) = "L"
aDataParams(8, 1) = "1033R": aDataParams(8, 2) = "M"
aDataParams(9, 1) = "1034L": aDataParams(9, 2) = "N"
aDataParams(10, 1) = "1034R": aDataParams(10, 2) = "O"
aDataParams(11, 1) = "1034LA": aDataParams(11, 2) = "P"
aDataParams(12, 1) = "1034RA": aDataParams(12, 2) = "Q"
aDataParams(13, 1) = "1035L": aDataParams(13, 2) = "R"
aDataParams(14, 1) = "1035R": aDataParams(14, 2) = "S"
aDataParams(15, 1) = "1036L": aDataParams(15, 2) = "T"
aDataParams(16, 1) = "1036R": aDataParams(16, 2) = "U"
'Find minimum column
MinCol = wsInput.Columns.Count
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
If wsInput.Columns(aDataParams(ParamIndex, 2)).Column < MinCol Then MinCol = wsInput.Columns(aDataParams(ParamIndex, 2)).Column
Next ParamIndex
'Based on minimum column, determine column indexes for each sheet/column pair
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
aDataParams(ParamIndex, 3) = wsInput.Columns(aDataParams(ParamIndex, 2)).Column - MinCol + 1
Next ParamIndex
With wsInput.Range("F89", wsInput.Cells(wsInput.Rows.Count, "V").End(xlUp))
If .Row < 89 Then
MsgBox "No data in sheet [" & wsInput.Name & "]"
Exit Sub
End If
aInput = .Value
End With
For ParamIndex = LBound(aDataParams, 1) To UBound(aDataParams, 1)
'Define data sheet based on current column
Set wsData = wb.Sheets(aDataParams(ParamIndex, 1))
aData = wsData.Range("C1", wsData.Cells(wsData.Rows.Count, "F").End(xlUp)).Value
For InputIndex = LBound(aInput, 1) To UBound(aInput, 1)
For DataIndex = LBound(aData, 1) To UBound(aData, 1)
If aInput(InputIndex, UBound(aInput, 2)) >= aData(DataIndex, 3) _
And aInput(InputIndex, UBound(aInput, 2)) <= aData(DataIndex, 4) Then
aInput(InputIndex, aDataParams(ParamIndex, 3)) = aData(DataIndex, 1)
Exit For
End If
Next DataIndex
Next InputIndex
Set wsData = Nothing
Erase aData
Next ParamIndex
wsInput.Range("F89").Resize(UBound(aInput, 1), UBound(aInput, 2)) = aInput
Set wb = Nothing
Set wsInput = Nothing
Set wsData = Nothing
Erase aInput
Erase aData
Erase aDataParams
End Sub
I am new to VBA and sitting with a sum, which includes three if loops. The code looks like this
Dim strKonto As String
Dim str?r As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer
For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
For k = 1 To 20 '200
yearkbottom = (wsRefor.Cells(k + 3, 1) - 2007) + 1
yearktop = (yearkbottom - 2007) + 4000
For j = yearkbottom To yearktop
strKonto = Right(wsArk7.Cells(j + 4, 2), 4)
str?r = wsArk7.Cells(j + 4, 1)
strUdbetaling = Left(wsArk7.Cells(j + 4, 2), 1)
counter = Val(str?r) - 2007
If wsRefor.Cells(i + 1, 2) = strKonto Then
If wsRefor.Cells(1, k + 3) = str?r Then
If strUdbetaling = 2 Then
wsRefor.Cells(i + 1, k + 3) = wsRefor.Cells(i + 1, k + 3) + wsArk7.Cells(j + 4, k + 2 - counter * 12)
End If
End If
End If
Next j
Next k
Next i
For the j-loop I tried to make the boundaries dynamic to make the calculations a little slower. That is, I am sure all values which the j loop finds are not spread over the entire range of j, but rather within the range defined above using k.
However, when I make this alteration I get an 1004 "Application-defined or Object-defined error".
Anyone able to spot the mistake, or alternatively to suggest any methods of speeding up the sum?
Best,
ID
EDIT: I found out what the problem was. The j counter took on zero at some point in the new boundaries, and when that happens (or when it is negative) the mistake I got comes up.
Thanks for the help!
Try below one. I have tried to do some fine tuning.
Dim strKonto As String
Dim strr As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer
Dim arryearkbottom(20) 'Declaring a Static Array of Size 21,(Starts from 0 index)
Dim arryearktop(20) 'Declaring a Static Array of Size 21,(Starts from 0 index)
'Initial Values
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
'Turning off the values for performance
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'Reading all the values from sheet to Arrays
For i = 1 To 20
arryearkbottom(i) = (wsRefor.Cells(i + 3, 1) - 2007) + 1
arryearktop(i) = (arryearkbottom(i) - 2007) + 4000
Next
For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
For k = 1 To 20 '200
'yearkbottom = (wsRefor.Cells(k + 3, 1) - 2007) + 1
'yearktop = (yearkbottom - 2007) + 4000
For j = arryearkbottom(k) To arryearktop(k)
strKonto = Right(wsArk7.Cells(j + 4, 2), 4)
strr = wsArk7.Cells(j + 4, 1)
strUdbetaling = Left(wsArk7.Cells(j + 4, 2), 1)
counter = Val(strr) - 2007
If wsRefor.Cells(i + 1, 2) = strKonto Then
If wsRefor.Cells(1, k + 3) = strr Then
If strUdbetaling = 2 Then
wsRefor.Cells(i + 1, k + 3) = wsRefor.Cells(i + 1, k + 3) + wsArk7.Cells(j + 4, k + 2 - counter * 12)
End If
End If
End If
Next j
Next k
Next i
'Setting to Initial values
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
Solution2:
Dim strKonto As String
Dim strr As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer
Dim rngwsRefor As Range
Dim rngwsArk7 As Range
'Initial Values
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
'Turning off the values for performance
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'Reading Entire sheet values to range
Set rngwsRefor = wsRefor.Cells
Set rngwsArk7 = wsArk7.Cells
For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
For k = 1 To 20 '200
yearkbottom = (rngwsRefor(k + 3, 1).Value - 2007) + 1
yearktop = (yearkbottom - 2007) + 4000
For j = yearkbottom To yearktop
strKonto = Right(rngwsArk7(j + 4, 2).Value, 4)
strr = rngwsArk7(j + 4, 1).Value
strUdbetaling = Left(rngwsArk7(j + 4, 2).Value, 1)
counter = Val(strr) - 2007
If rngwsRefor(i + 1, 2).Value = strKonto Then
If rngwsRefor(1, k + 3).Value = strr Then
If strUdbetaling = 2 Then
rngwsRefor(i + 1, k + 3).Value = rngwsRefor(i + 1, k + 3).Value + rngwsArk7.Cells(j + 4, k + 2 - counter * 12).Value
End If
End If
End If
Next j
Next k
Next i
'Setting to Initial values
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
I have made a userform that allows the user to select a table and add rows to it and fill those rows with various information, all from the userform. I have run into a few problems with this.
First after adding or during adding the items (after hitting submit) excel would crash. It occurs randomly and is hard to reproduce.
Second after running the macro there is a good chance that all the cells in the workbook and every other object except the userform button will stop working, meaning you can't edit interact or even select anything. Then when I close the workbook excel crashes after saving. This is my major offender and I think causes the other problem.
What causes this freezing and why does it occur? How do I fix it? I have looked around and haven't found anything circumstantial. One post said that I should try editing the table with no formatting on it and I did that and it didn't work.
I can provide the excel workbook at a request basis via pm.
The code:
On Activate -
Public Sub UserForm_Activate()
Set cBook = ThisWorkbook
Set dsheet = cBook.Sheets("DATA")
End Sub
Help Checkbox -
Private Sub cbHelp_Click()
If Me.cbHelp.Value = True Then
Me.lbHelp.Visible = True
Else
Me.lbHelp.Visible = False
End If
End Sub
Brand combobox -
Public Sub cmbBrand_Change()
brandTableName = cmbBrand.Value
brandTableName = CleanBrandTableName(brandTableName)
'if brand_edit is not = to a table name then error is thrown
On Error Resume Next
If Err = 380 Then
Exit Sub
Else
cmbItemID.RowSource = brandTableName
End If
On Error GoTo 0
'Set cmbItemID's text to nothing after changing to a new brand
cmbItemID.Text = ""
End Sub
CleanBrandTableName(brandTableName) function -
Option Explicit
Public Function CleanBrandTableName(ByVal brandTableName As String) As String
Dim s As Integer
Dim cleanResult As String
For s = 1 To Len(brandTableName)
Select Case Asc(Mid(brandTableName, s, 1))
Case 32, 48 To 57, 65 To 90, 97 To 122:
cleanResult = cleanResult & Mid(brandTableName, s, 1)
Case 95
cleanResult = cleanResult & " "
Case 38
cleanResult = cleanResult & "and"
End Select
Next s
CleanBrandTableName = Replace(WorksheetFunction.Trim(cleanResult), " ", "_")
End Function
Public Function CleanSpecHyperlink(ByVal specLink As String) As String
Dim cleanLink As Variant
cleanLink = specLink
cleanLink = Replace(cleanLink, "=HYPERLINK(", "")
cleanLink = Replace(cleanLink, ")", "")
cleanLink = Replace(cleanLink, ",", "")
cleanLink = Replace(cleanLink, """", "")
cleanLink = Replace(cleanLink, "Specs", "")
CleanSpecHyperlink = cleanLink
End Function
Browse button -
Public Sub cbBrowse_Click()
Dim rPos As Long
Dim lPos As Long
Dim dPos As Long
specLinkFileName = bFile
rPos = InStrRev(specLinkFileName, "\PDFS\")
lPos = Len(specLinkFileName)
dPos = lPos - rPos
specLinkFileName = Right(specLinkFileName, dPos)
Me.tbSpecLink.Text = specLinkFileName
End Sub
bFile function -
Option Explicit
Public Function bFile() As String
bFile = Application.GetOpenFilename(Title:="Please choose a file to open")
If bFile = "" Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Function
End If
End Function
Preview button -
Private Sub cbSpecs_Click()
If specLinkFileName = "" Then Exit Sub
cBook.FollowHyperlink (specLinkFileName)
End Sub
Add Item button -
Private Sub cbAddItem_Click()
Dim brand As String
Dim description As String
Dim listPrice As Currency
Dim cost As Currency
Dim Notes As String
Dim other As Variant
itemID = Me.tbNewItem.Text
brand = Me.tbBrandName.Text
description = Me.tbDescription.Text
specLink = Replace(specLinkFileName, specLinkFileName, "=HYPERLINK(""" & specLinkFileName & """,""Specs"")")
If Me.tbListPrice.Text = "" Then
listPrice = 0
Else
listPrice = Me.tbListPrice.Text
End If
If Me.tbCost.Text = "" Then
cost = 0
Else
cost = Me.tbCost.Text
End If
Notes = Me.tbNotes.Text
other = Me.tbOther.Text
If Me.lbItemList.listCount = 0 Then
x = 0
End If
With Me.lbItemList
Me.lbItemList.ColumnCount = 8
.AddItem
.List(x, 0) = itemID
.List(x, 1) = brand
.List(x, 2) = description
.List(x, 3) = specLink
.List(x, 4) = listPrice
.List(x, 5) = cost
.List(x, 6) = Notes
.List(x, 7) = other
x = x + 1
End With
End Sub
Submit button -
Private Sub cbSubmit_Click()
Dim n As Long
Dim v As Long
Dim vTable() As Variant
Dim r As Long
Dim o As Long
Dim c As Long
Dim w As Variant
Set brandTable = dsheet.ListObjects(brandTableName)
o = 1
listAmount = lbItemList.listCount
v = brandTable.ListRows.Count
w = 0
For c = 1 To listAmount
If brandTable.ListRows(v).Range(, 1).Value <> "" Then
brandTable.ListRows.Add alwaysinsert:=True
brandTable.ListRows.Add alwaysinsert:=True
Else
brandTable.ListRows.Add alwaysinsert:=True
End If
Next
ReDim vTable(1000, 1 To 10)
For n = 0 To listAmount - 1
vTable(n + 1, 1) = lbItemList.List(n, 0)
vTable(n + 1, 2) = lbItemList.List(n, 1)
vTable(n + 1, 3) = lbItemList.List(n, 2)
vTable(n + 1, 5) = lbItemList.List(n, 4)
vTable(n + 1, 6) = lbItemList.List(n, 5)
vTable(n + 1, 7) = lbItemList.List(n, 6)
vTable(n + 1, 8) = lbItemList.List(n, 7)
If lbItemList.List(n, 3) = "" Then
ElseIf lbItemList.List(n, 3) <> "" Then
vTable(n + 1, 4) = lbItemList.List(n, 3)
End If
If n = 0 And brandTable.DataBodyRange(1, 1) <> "" Then
For r = 1 To brandTable.ListRows.Count
If brandTable.DataBodyRange(r, 1) <> "" Then
o = r + 1
' brandTable.ListRows.Add alwaysinsert:=True
End If
Next
End If
brandTable.ListColumns(1).DataBodyRange(n + o).Value = vTable(n + 1, 1)
brandTable.ListColumns(2).DataBodyRange(n + o).Value = vTable(n + 1, 2)
brandTable.ListColumns(3).DataBodyRange(n + o).Value = vTable(n + 1, 3)
brandTable.ListColumns(4).DataBodyRange(n + o).Value = vTable(n + 1, 4)
brandTable.ListColumns(5).DataBodyRange(n + o).Value = vTable(n + 1, 5)
brandTable.ListColumns(6).DataBodyRange(n + o).Value = vTable(n + 1, 6)
brandTable.ListColumns(7).DataBodyRange(n + o).Value = vTable(n + 1, 7)
brandTable.ListColumns(8).DataBodyRange(n + o).Value = vTable(n + 1, 8)
Next
brandTable.DataBodyRange.Select
Selection.Font.Bold = True
Selection.WrapText = True
brandTable.ListColumns(5).DataBodyRange.Select
Selection.NumberFormat = "$#,##0.00"
brandTable.ListColumns(6).DataBodyRange.Select
Selection.NumberFormat = "$#,##0.00"
Unload Me
End Sub
Remove Items button -
Private Sub cbRemoveItems_Click()
Dim intCount As Long
For intCount = lbItemList.listCount - 1 To 0 Step -1
If lbItemList.Selected(intCount) Then
lbItemList.RemoveItem (intCount)
x = x - 1
End If
Next intCount
End Sub
There is other code that does things for the other tabs but they don't interact with this tabs code.
I have created a code for getting unique value from a column which is filled with date and from that unique column i have compare whether it is Sunday or Monday or Tuesday or etc and if it falls in between two time stamp [2:00:00 am to 2:59:59 am] i increment but if on same date for example 1/5/2014 it falls in two time stamp again[2:00:00 am - 2:59:59 am] i should not increment and if in the same date it falls in another time stamp it should increment that too only once.
It is working for 50 -100 rows but for 200k of rows it is hanging.
Private Sub CommandButton1_Click()
Range("I2:O25") = ""
Set Range1 = Range("B:B")
Dim dates As Variant
Dim Array1() As Variant
Dim MyArray1(24, 7) As Integer
Array1 = UniqueItems(Range1, False)
For Each dates In Array1
If Not (dates = "" Or dates = "Date") Then
For y = 2 To Range("B2").End(xlDown).Row
If (dates = (Cells(y, 2))) Then
For f = 2 To Range("f2").End(xlDown).Row
If ((TimeValue(Cells(y, 4).Text) >= TimeValue(Cells(f, 6).Text)) And (TimeValue(Cells(y, 4).Text) <= TimeValue(Cells(f, 7).Text))) Then
If (Cells(y, 3) = "Sunday") Then
' Cells(f, 12) = 1
Dim g As Integer
g = f - 2
MyArray1(g, 0) = 1
End If
If (Cells(y, 3) = "Monday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 1) = 1
End If
If (Cells(y, 3) = "Tuesday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 2) = 1
End If
If (Cells(y, 3) = "Wednesday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 3) = 1
End If
If (Cells(y, 3) = "Thursday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 4) = 1
End If
If (Cells(y, 3) = "Friday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 5) = 1
End If
If (Cells(y, 3) = "Saturday") Then
' Cells(f, 12) = 1
g = f - 2
MyArray1(g, 6) = 1
End If
End If
Next f
End If
Next y
For k = 0 To 7
For x = 0 To 23
Dim cellsval As Integer
Dim dayvals As Integer
cellsval = x + 2
dayvals = k + 9
Cells(cellsval, dayvals) = Cells(cellsval, dayvals) + MyArray1(x, k)
MyArray1(x, k) = 0
Next x
Next k
End If
Next
'For x = 2 To Range("H2").End(xlDown).Row
' For y = 2 To Range("A2").End(xlDown).Row
' If (Cells(y, 2) = Cells(x, 8)) Then
' If ((TimeValue(Cells(y, 4).Text) >= TimeValue(Cells(16, 6).Text)) And (TimeValue(Cells(y, 4).Text) <= TimeValue(Cells(16, 7).Text))) Then
' If (Cells(y, 3) = "Wednesday") Then
' Cells(x, 22) = 1
' End If
' End If
' End If
' Next y
'Next x
End Sub
Function RetTime(IntTime As Long) As Date
RetTime = TimeSerial(Int(IntTime / 10000), Int((IntTime Mod 10000) / 100), (IntTime Mod 100))
End Function
Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
' Accepts an array or range as input
' If Count = True or is missing, the function returns the number of unique elements
' If Count = False, the function returns a variant array of unique elements
Dim Unique() As Variant ' array that holds the unique items
Dim Element As Variant
Dim i As Integer
Dim FoundMatch As Boolean
' If 2nd argument is missing, assign default value
If IsMissing(Count) Then Count = True
' Counter for number of unique elements
NumUnique = 0
' Loop thru the input array
For Each Element In ArrayIn
FoundMatch = False
' Has item been added yet?
For i = 1 To NumUnique
If Element = Unique(i) Then
FoundMatch = True
Exit For '(exit loop)
End If
Next i
AddItem:
' If not in list, add the item to unique list
If Not FoundMatch And Not IsEmpty(Element) Then
NumUnique = NumUnique + 1
ReDim Preserve Unique(NumUnique)
Unique(NumUnique) = Element
End If
Next Element
' Assign a value to the function
If Count Then UniqueItems = NumUnique Else UniqueItems = Unique
End Function
I have taken the liberty of cleaning up your code a bit, I dropped a couple of comments in there to show you the changes and I have indented it properly.
Option Explicit
Private Sub CommandButton1_Click()
Dim dates As Variant, Array1() As Variant, MyArray1(24, 7) As Long, g As Long, MyWeekday As Variant, X As Long, K As Long, F As Long, Y As Long, Range1 As Range
MyWeekday = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
Range("I2:O25").ClearContents
Set Range1 = Range("B:B")
Array1 = UniqueItems(Range1, False)
For Each dates In Array1
If Not (dates = "" Or dates = "Date") Then
For Y = 2 To Range("B" & Rows.Count).End(xlUp).Row
If (dates = (Cells(Y, 2))) Then
For F = 2 To Range("f" & Rows.Count).End(xlUp).Row
If ((TimeValue(Cells(Y, 4).Text) >= TimeValue(Cells(F, 6).Text)) And (TimeValue(Cells(Y, 4).Text) <= TimeValue(Cells(F, 7).Text))) Then
For X = LBound(MyWeekday) To UBound(MyWeekday)
If (Cells(Y, 3) = MyWeekday(X)) Then
g = F - 2
MyArray1(g, X) = 1
End If
Next
End If
Next
End If
Next
For K = 0 To 7
For X = 0 To 23
Cells(X + 2, K + 9) = Cells(X + 2, K + 9) + MyArray1(X, K)
MyArray1(X, K) = 0
Next
Next
End If
Next
End Sub
Function RetTime(IntTime As Long) As Date
RetTime = TimeSerial(Int(IntTime / 10000), Int((IntTime Mod 10000) / 100), (IntTime Mod 100))
End Function
Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
' Accepts an array or range as input
' If Count = True or is missing, the function returns the number of unique elements
' If Count = False, the function returns a variant array of unique elements
Dim Unique() As Variant, Element As Variant, i As Long, FoundMatch As Boolean, NumUnique As Long
' If 2nd argument is missing, assign default value
If IsMissing(Count) Then Count = True
' Counter for number of unique elements
NumUnique = 0
' Loop thru the input array
For Each Element In ArrayIn
FoundMatch = False
' Has item been added yet?
For i = 1 To NumUnique
If Element = Unique(i) Then
FoundMatch = True
Exit For '(exit loop)
End If
Next i
'AddItem - You don't need this as a GoTo heading you can jump to, keep it commented out
' If not in list, add the item to unique list
If Not FoundMatch And Not IsEmpty(Element) Then
NumUnique = NumUnique + 1
ReDim Preserve Unique(NumUnique)
Unique(NumUnique) = Element
End If
Next Element
' Assign a value to the function
If Count Then UniqueItems = NumUnique Else UniqueItems = Unique
End Function
Please post try the code I posted and see if it does the same as your code did, if so then we can begin making the changes you need.