I'm starting an Excel sheet collecting weekly data, and I'm trying to create an if statement to access the last(index) sheet in the workbook and check multiple conditions.
I need to check whether cells in column k are "true" and, if they are, whether the date in column J falls within the month range from the sheet called "Dashboard". If these two conditions are met, I'll add one to my counter and then ultimately print this counter number into a cell on the Dashboard sheet.
So far my code is:
Sub CreateTable()
Dim n As Integer, RangeCount As Integer
Dim MaxVal As Long
n = 0
RangeCount = 1
sheets(sheets.Count).Select) 'I don't want this line but I don't know how else to access the final sheet in code
MaxVal = WorksheetFunction.CountA(Range("J1:J14")) 'Columns J and K will always be the same length
For RangeCount = 1 to MaxVal
If Cells(RangeCount, 11) = "true" And Sheets("Dashboard").Range("Y17") <= sheets(sheets.Count).Select.Cells(RangeCount, 11) And sheets("Dashboard").WorksheetFunction.EoMonth(Range("Y17", 0)) >= Cells(RangeCount, 11) Then
n = n + 1
End If
Next RangeCount
End Sub
Y17 is the cell in Dashboard where a month is listed (1/12/2022). My current error is Object Required, but I think I've defined everything I need to define, etc. Any help would be appreciated!
Do you have an issue with your conditons?
Option Explicit
Sub CreateTable()
Dim n As Integer, RangeCount As Integer
Dim MaxVal As Long
n = 0
RangeCount = 1
Sheets(Sheets.Count).Select 'I don't want this line but I don't know how else to access the final sheet in code
MaxVal = WorksheetFunction.CountA(Range("J1:J14")) 'Columns J and K will always be the same length
For RangeCount = 1 To MaxVal
If CheckConditions(RangeCount) Then
n = n + 1
End If
Next RangeCount
End Sub
Function CheckConditions(locCnt As Integer) As Boolean
Dim locTest As Boolean
'which worksheet? do you want the activesheet here?
locTest = Cells(locCnt, 11) = "true"
locTest = locTest And Sheets("Dashboard").Range("Y17") <= Sheets(Sheets.Count).Cells(locCnt, 11)
'which worksheet for cells? do you want the activesheet here?
locTest = locTest And Sheets("Dashboard").WorksheetFunction.EoMonth(Range("Y17", 0)) >= Cells(locCnt, 11)
CheckConditions = locTest
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.