I'm running this macro that auto scrolls at designated time intervals. I'm having two issues:
When the macro finishes, I want it to return to the top of the sheet, but it doesn't.
I want it to run only on designated sheets, not across the entire workbook.
What's wrong with my code?
Sub ReRunMacro()
Dim xMin As String
Dim lastRow As Long, i As Long
Dim ws As Worksheet
ws = ThisWorkbook.Worksheets("CNC Machining Cell 2", "CNC Grinding Cell", "CNC Turning Cell 1 & 3", "CNC Turning Cell 2")
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To 14 Step 2
Cells(i, 1).Select
ActiveWindow.SmallScroll down:=1
Application.Wait (Now + TimeValue("0:00:03"))
If i = lastRow - 2 Or i = lastRow - 1 Then
i = 0
Cells(1, 1).Select
End If
Next i
Debug.Print (i)
xMin = GetSetting(AppName:="Kutools", Section:="Macro", Key:="min", Default:="")
If (xMin = "") Or (xMin = "False") Then
xMin = Application.InputBox(prompt:="Please input the interval time you need to repeat the Macro", Title:="Kutools for Excel", Type:=2)
SaveSetting "Kutools", "Macro", "min", xMin
End If
If (xMin <> "") And (xMin <> False) Then
Application.OnTime Now() + TimeValue("0:" + xMin + ":0"), "ReRunMacro"
Else
Exit Sub
End If
End Sub
Here you go, I've explained how it works in the code comments
Sub ReRunMacro()
Dim xMin As String
Dim lastRow As Long, i As Long
Dim ws As Worksheet
Dim validSheets() As Variant
Set ws = ActiveSheet
' put the sheet names you want visible when the code is running into an array
validSheets = Array("CNC Machining Cell 2", "CNC Grinding Cell", "CNC Turning Cell 1 & 3", "CNC Turning Cell 2")
' check were on one of those sheets, if not exit (or pause the code, whatever you want to do
If UBound(Filter(validSheets, ws.Name)) = -1 Then ' we're not on the right sheet
Exit Sub ' you can use the worksheet selection event to run this code again when the user moves to a different sheet
End If
lastRow = ws.Range("A100000").End(xlUp).Row ' it's best not to use row count, its unreliable, also you were going from the last row up and could land on row 1
For i = 1 To 14 Step 2
ws.Cells(i, 1).Select ' always best to prefix a range with the worksheet it's on
ActiveWindow.SmallScroll down:=1
Application.Wait (Now + TimeValue("0:00:03"))
If i = lastRow - 2 Or i = lastRow - 1 Then
i = 0
ws.Cells(1, 1).Select
End If
Next i
xMin = GetSetting(AppName:="Kutools", Section:="Macro", Key:="min", Default:="")
If (xMin = "") Or (xMin = "False") Then
xMin = Application.InputBox(prompt:="Please input the interval time you need to repeat the Macro", Title:="Kutools for Excel", Type:=2)
SaveSetting "Kutools", "Macro", "min", xMin
End If
If (xMin <> "") And (xMin <> False) Then
Application.OnTime Now() + TimeValue("0:" + xMin + ":0"), "ReRunMacro"
Else
MsgBox "No values supplied, code will end", vbInformation ' it's polite to inform people you're stopping the code
Exit Sub
End If
End Sub
Related
I've been fighting with this for a couple days now, and am at a loss on what else to try. My goal is to have a prompt for where a workbook is saved, this spreadsheet is obtained from an external source and name/location can vary. After opening the workbook, switch over to the second sheet and start searching for the values to copy to the workbook the macro is run out of.
The code I have works great if I set a breakpoint at the calculation for the last row, and at the For loop. Without those 2 breakpoints, it appears that none of the information in the workbook loads before running the rest of the code.
Public Sub Clm2Count()
Dim i, j, k, last As Long
Dim wkbSource, wkbCrnt As Workbook
Dim str As Variant
Dim strArray()
strArray() = Array("THIS", "IS", "MY", "ARRAY")
Set wkbCrnt = ThisWorkbook
k = 1
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Application.ScreenUpdating = False
Set wkbSource = Workbooks.Open(.SelectedItems(1))
Application.ScreenUpdating = True
End If
End With
Sheets(2).Activate
Cells(5,1).Select 'Trying to activate a cell before calculating last, didn't work
last = wkbSource.Sheets(2).Cells(wkbSource.Sheets(2).Cells.Rows.Count, 1).End(xlUp).Row
For i = 51 To last
If InStr(1, wkbSource.Sheets(2).Cells(i, 2).Value, "TEST") > 0 Then
For Each str In strArray
If InStr(1, wkbSource.Sheets(2).Cells(i, 2).Text, str, vbTextCompare) > 0 Then
If InStr(1, wkbSource.Sheets(2).Cells(i, 2).Text, "A", vbTextCompare) > 0 Or InStr(1, Cells(i, 2).Text, "B", vbTextCompare) > 0 Then
If str = "MY" Then 'Specific value from the array
wkbSource.Sheets(2).Cells(i, 3).Copy
wkbCrnt.Sheets(1).Cells(k, 1).PasteSpecial
wkbCrnt.Sheets(1).Cells(k, 2).Value = "QC"
wkbCrnt.Sheets(1).Cells(k, 3).Value = i & ", " & str
k = k + 1
Exit For
End If
ElseIf InStr(1, wkbSource.Sheets(2).Cells(i, 2).Text, "C", vbTextCompare) > 0 Then
wkbSource.Sheets(2).Cells(i, 3).Copy
wkbCrnt.Sheets(1).Cells(k, 1).PasteSpecial
wkbCrnt.Sheets(1).Cells(k, 2).Value = "QC"
wkbCrnt.Sheets(1).Cells(k, 3).Value = i & ", " & str
k = k + 1
Exit For
Else
Exit For
End If
End If
Next str
wkbSource.Activate
End If
Next i
End Sub
Any ideas on what I might be overlooking?
Edit:
Here are images of the beginning and end of column A, with the identifiers removed
Beginning
End
Instead of Sheets(2).Activate use wkbSource.Sheets(2).Activate. Same goes for cells and any other kind of ranges you are using.
It is especially crucial to be as explicit as possible which is the target workbook when you have a multiple workbook interaction.
To find last row use this line:
last = wkbSource.Sheets(2).Cells(wkbSource.Sheets(2).Cells.Rows.Count, 1).End(xlUp).Row
Edit: The issue was caused by a hidden sheet - it is better to use sheets name in this case.
I'm writing a VBA program to search through a large spreadsheet and copy rows that have the same account five or more times associated with the data to a different sheet. The program does exactly what it's supposed to do when I step through each individual line (F8), but when I run the program (F5), it doesn't end up copying any information to the second sheet. I've tried adding a two second delay between switching sheets and pasting the data, just in case this was the problem, but so far it hasn't helped.
Any suggestions?
Edit: I thought that the screen updating might be causing the problem, so I disabled it. The program still didn't paste the data in the other worksheet.
Second Edit: I noticed that when I put a stop in at the beginning of the while loop and step the program through in chunks, it also does not copy and paste the data like it should be. It still works when stepping through individual lines of code, though. I also removed the 2 second pauses as those didn't make a difference.
Here's the code:
Public Sub Main()
Worksheets(2).Activate
Range("A1").Select
Worksheets(1).Activate
Range("C2").Select
AcctName = ActiveCell.Value
LoopControl = 0
AcctNameCt = 1
CurrentAcctRow = ActiveCell.Row
Do While LoopControl <> 1
SecondLoopControl = 0
If AcctName = ActiveCell.Offset(AcctNameCt, 0).Value Then
AcctNameCt = AcctNameCt + 1
If AcctNameCt > 4 Then
GreaterThanFour
End If
ElseIf ActiveCell.Offset(AcctNameCt, 0).Value = "" Then
Exit Do
Else
ActiveCell.Offset(AcctNameCt, 0).Activate
AcctName = ActiveCell.Value
AcctNameCt = 1
CurrentAcctRow = ActiveCell.Row
End If
Loop
End Sub
Public Sub CopyData()
Dim EndRow As Integer
Dim StopCopy As Integer
Dim RestartRow As Integer
EndRow = CurrentAcctRow + AcctNameCt
StopCopy = EndRow - 1
RestartRow = EndRow + 1
ActiveSheet.Range("C" & CurrentAcctRow & ":" & "C" & StopCopy).EntireRow.Copy
Worksheets(2).Activate
LookForEmptyRow
ActiveCell.EntireRow.PasteSpecial
CurrentAcctRow = CurentAcctRow + 1
Worksheets(1).Activate
Range("C" & EndRow).Select
AcctNameCt = 0
End Sub
Public Sub GreaterThanFour()
Do While SecondLoopControl <> 1
If AcctName = ActiveCell.Offset(AcctNameCt, 0).Value Then
AcctNameCt = AcctNameCt + 1
Else
CopyData
SecondLoopControl = 1
End If
Loop
End Sub
Public Sub LookForEmptyRow()
Range("A1").Select
Dim LookAnotherLoopControl As Integer
LookAnotherLoopControl = 0
Do While LookAnotherLoopControl <> 1
If ActiveCell.Value = "" Then Exit Sub Else ActiveCell.Offset(1, 0).Activate
Loop
End Sub
I set the worksheet names to variables and called those, rather than calling the worksheets directly. For some reason, this works better.
Set wbA = Workbooks(Workbook Name)
Set wsA = Worksheets(Worksheet Name 1)
Set wsB = Worksheets(Worksheet Name 2)
Where the "Workbook Name" and "Worksheet Name 1" reflect the actual names, instead. Those are working better than:
Worksheets(2).Activate
LookForEmptyRow
ActiveCell.EntireRow.PasteSpecial
CurrentAcctRow = CurentAcctRow + 1
Worksheets(1).Activate
Range("C" & EndRow).Select
I also used a better method to look for an empty row, rather than writing my own subroutine. The original code had this sub that I wrote:
Public Sub LookForEmptyRow()
Range("A1").Select
Dim LookAnotherLoopControl As Integer
LookAnotherLoopControl = 0
Do While LookAnotherLoopControl <> 1
If ActiveCell.Value = "" Then Exit Sub Else ActiveCell.Offset(1, 0).Activate
Loop
Which, while effective, was highly inefficient. I replaced it with the much more efficient line of code:
lRow = Range("A1000").End(xlUp).Row
Cells(lRow + 1, 1).Activate
I have an Excel dataset that has a string in A1, and other values in B1, B2, and B3 that relate to A1; and so on down the page. Sometimes there are more than three cells that relate to the other string (unpredictable). In this example, cells A2 and A3 are blank. I want to create a macro that will fill A2 and A3 (etc) with the contents of A1.
In the example below I am using [] to help format it as Excel cells. I want to go from:
[SMITH, John] [Home]
[Mobile]
[Work]
[DOE, John] [Home]
[Mobile]
to
[SMITH, John] [Home]
[SMITH, John] [Mobile]
[SMITH, John] [Work]
[DOE, John] [Home]
[DOE, John] [Mobile]
I want the macro to repeat this for varying iterations, sometimes I have 1000 lines to adjust manually. Tweaking the software that outputs the data is not an option.
The code I have is as follows:
Sub rname()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
i = 0
While i < 50
If ActiveCell.Offset(1,0) = "" Then
ActiveCell.Offset(1,0) = cellvar
i = i + 1
ElseIf ActiveCell.Offset(1,0) = "*" Then
ActiveCell.Offset(1,0).Activate
i = i + 1
End If
Wend
End Sub
The above code adds text to the cell below the active cell once and then stops responding. The following code runs once and doesn't stop responding - I can run it again, but it doesn't automatically move down a row.
Sub repeat_name()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
i = 1
For i = 1 To 50
If ActiveCell.Offset(1, 0) = "" Then
ActiveCell.Offset(1, 0) = cellvar
End If
If ActiveCell.Offset(1, 0) = "*" Then
ActiveCell.Offset(1, 0).Select.Activate 'I have tried .Offset(2,0)too
End If
i = i + 1
Next
End Sub
I am stumped here. Does anyone have any thoughts or suggestions?
Try it as,
Sub fillBlanks()
With Worksheets("Sheet1")
With .Range(.Cells(2, "B"), .Cells(Rows.Count, "B").End(xlUp))
With .Offset(0, -1).SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
End With
With .Offset(0, -1)
.Value = .Value
End With
End With
End With
End Sub
Before fillBlanks procedure After fillBlanks procedure
Others have given working solutions, I'll just outline the problems with your code.
cellvar = ActiveCell assigns the value of the active cell to cellvar but cellvar won't change if ActiveCell changes so you'll just copy [SMITH, John] for all other people. You'd have to reassign cellvar.
If ActiveCell.Offset(1, 0) = "*" Then This checks if the cell contains an asterisk. Instead use Not ActiveCell.Offset(1, 0) = "", ActiveCell.Offset(1, 0) <> "", Not isEmpty(ActiveCell.Offset(1, 0)) or just Else (which would be the preferred version here since it doesn't require further calculations).
Edit: "*" Can be used as a wildcard with the Like operator as in If ActiveCell.Offset(1, 0) Like "*" Then but this would also be true for the empty string. To be sure that there is at least one sign you'd have to use "?*" instead. The question mark stands for exactly one character and the asterisk for 0 or more. To check if a cell is empty I would recommend one of the above ways though.
In you first sub this means that if the cell anything but "*", i will not be incremented and you end in an endless loop. In the second function, it means that the the active cell will not be changed and neither "" not "*" will be detected for the rest of the loop.
In the second sub, you don't need i=i+1, the for loop does that for you. This would mean that you increment i by 2 every iteration.
ActiveCell.Offset(1, 0).Select.Activate Here the "select" is too much
Here are the subs with minimal changes:
Sub rname()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
i = 0
While i < 50
If ActiveCell.Offset(1, 0) = "" Then
ActiveCell.Offset(1, 0) = cellvar
ActiveCell.Offset(1, 0).Activate 'the code will run without this but need to iterations per row
i = i + 1
MsgBox "a " & i
Else
ActiveCell.Offset(1, 0).Activate
cellvar = ActiveCell 'reassign cellvar
i = i + 1
MsgBox "b " & i
End If
Wend
End Sub
second sub:
Sub repeat_name()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
'i = 1 'this is not necessary
For i = 1 To 50
If ActiveCell.Offset(1, 0) = "" Then
ActiveCell.Offset(1, 0) = cellvar
End If
If Not ActiveCell.Offset(1, 0) = "" Then 'if else endif would be nicer here
ActiveCell.Offset(1, 0).Activate 'remove "select"
cellvar = ActiveCell 'reassign cellvar
End If
'i = i + 1 'this is not necessary/wrong
Next i 'safer to include i
End Sub
Note that this is just to explain the problems with your code, I still recommend to use one of the other solutions here.
Try this:
Sub repeat_name()
Dim cellvar As String
Dim i As Integer
Dim ws As Worksheet
Set ws = Sheet1 'Change according to your sheet number
cellvar = ""
For i = 1 To 50
if Trim(ws.Range("A" & i )) <> "" then
cellvar = Trim(ws.Range("A" & i ))
Else
ws.Range("A" & i ) = cellvar
End if
Next i
End Sub
How about this:
Sub FillBlanks()
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
End Sub
try this:
Sub repeat_name()
Dim k As Integer
Dim i As Integer
i = 1
k = ActiveSheet.UsedRange.Rows.Count
While i <= k
With ActiveSheet
If .Range("A1").Value = "" Then
MsgBox "Error: First cell can not be empty."
Exit Sub
End If
If .Range("A" & i).Value = "" And .Range("B" & i).Value <> "" Then
.Range("A" & i).Value = .Range("A" & i - 1).Value
End If
End With
i = i + 1
Wend
End Sub
try this
Sub test()
lastrow = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 1) = "" Then
Cells(i, 1) = Cells(i - 1, 1)
End If
Next i
End Sub
Screen shot of what I want:
I want to time stamp each line as a change gets made so I can upload to a central file all lines that have been updated after a certain time. Since one asset might have multiple rows for each sub component, the user can fill in one line and autofill/copy paste to the relevant lines beneath. The rows might not be in a continuous range (e.g. when filtered).
The code I've got works great for changing one cell at a time and it works for a range but incredibly slowly.
This sub is called by worksheet_change shown in full below.
Sub SetDateRow(Target As Range, Col As String)
Dim TargetRng As Range
Dim LastCol, LastInputCol As Integer
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 24
End With
For Each TargetRng In Target.Cells
If TargetRng.Cells.Count > 1 Then
Application.EnableEvents = True
Exit Sub
Else
Application.EnableEvents = False
Cells(TargetRng.Row, LastCol - 2) = Now()
Cells(TargetRng.Row, LastCol - 1).Value = Environ("username")
Cells(TargetRng.Row, LastCol).Value = Target.Address
End If
Next
Application.EnableEvents = True
End Sub
Target.Cells.Address returns the range (including non-visible cells), but I can't work out how to split this into individual, visible cells that I can loop through.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Errorcatch
Dim TargetRng As Range
Dim LastCol, LastInputCol, LastRow As Integer
Dim LastInputColLetter As String
Dim ContinueNewRow
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 24
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
LastInputCol = LastCol - 3
If LastInputCol > 26 Then
LastInputColLetter = Chr(Int((LastInputCol - 1) / 26) + 64) & Chr(((LastInputCol - 1) Mod 26) + 65)
Else
LastInputColLetter = Chr(LastInputCol + 64)
End If
For Each TargetRng In Target.Cells
If TargetRng.Row <= 2 Then
Exit Sub
End If
If TargetRng.Column <= LastInputCol Then
SetDateRow Target, LastCol - 3
If TargetRng.Count = 1 Then
Application.EnableEvents = False
'
Dim cmt As String
' If Target.Value = "" Then
' Target.Value = " "
'
' End If
'----------------------------------------------------------------
If Intersect(TargetRng, Range("AC3:AC10000")) Is Nothing Then ' need to make column into variables in the code based on column name
Application.EnableEvents = True
Else
Application.EnableEvents = False
Cells(TargetRng.Row, "Z") = Now() 'Date booking was made column
Cells(TargetRng.Row, "AD").Value = Cells(Target.Row, "AD").Value + 1 ' iteration column
End If
'----------------------------------------------------------------
If TargetRng.Comment Is Nothing Then
cmt = Now & vbCrLf & Environ("UserName") & " *" & TargetRng.Value & "*"
Else
cmt = Now & vbCrLf & Environ("UserName") & " *" & TargetRng.Value & "* " & TargetRng.Comment.Text
End If
With TargetRng
.ClearComments
.AddComment cmt
End With
End If
End If
Application.EnableEvents = True
Next
Exit Sub
Errorcatch:
MsgBox Err.Description
Application.EnableEvents = True
End Sub
I have done some adjustments to your code (see comments within code)
This solution assumes the following:
Sample data has a two rows header and fields to be updated have the following titles located at row 1 (adjust corresponding lines in code if needed):
Date Change Made, Who Made Change and Last Cell Changed as per picture provided.
Booked Date, BkdDte Change and Iteration for columns AC, Z and AD respectively (this names are used for testing purposes, change code to actual names)
I have also combined both procedures into a common one in order to avoid the inefficient approach of looping twice the cells of the changed range. Let me know if they must remain separated and will do the necessary adjustments.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Wsh As Worksheet, rCll As Range
Dim iDteChn As Integer, iWhoChn As Integer, iLstCll As Integer
Dim iBkdDte As Integer, iBkdChn As Integer, iBkdCnt As Integer
Dim sCllCmt As String
Dim lRow As Long
On Error GoTo ErrorCatch
Rem Set Application Properties
Application.ScreenUpdating = False 'Improve performance
Application.EnableEvents = False 'Disable events at the begining
Rem Set Field Position - This will always returns Fields position
Set Wsh = Target.Worksheet
With Wsh
iDteChn = WorksheetFunction.Match("Date Change Made", .Rows(1), 0)
iWhoChn = WorksheetFunction.Match("Who Made Change", .Rows(1), 0)
iLstCll = WorksheetFunction.Match("Last Cell Changed", .Rows(1), 0)
iBkdDte = WorksheetFunction.Match("Booked Date", .Rows(1), 0) 'Column of field "Booked date" (i.e. Column `AC`)
iBkdChn = WorksheetFunction.Match("BkdDte Change", .Rows(1), 0) 'Column of field "Booked date changed" (i.e. Column `Z`)
iBkdCnt = WorksheetFunction.Match("Iteration", .Rows(1), 0) 'Column of field "Iteration" (i.e. Column `AD`)
End With
Rem Process Cells Changed
For Each rCll In Target.Cells
With rCll
lRow = .Row
Rem Exclude Header Rows
If lRow <= 2 Then GoTo NEXT_Cll
Rem Validate Field Changed
Select Case .Column
Case Is >= iLstCll: GoTo NEXT_Cll
Case iDteChn, iWhoChn, iBkdChn, iBkdCnt: GoTo NEXT_Cll
Case iBkdDte
Rem Booked Date - Set Count
Wsh.Cells(lRow, iBkdChn) = Now()
Wsh.Cells(lRow, iBkdCnt).Value = Cells(.Row, iBkdCnt).Value + 1
End Select
Rem Update Cell Change Details
Wsh.Cells(lRow, iDteChn).Value = Now()
Wsh.Cells(lRow, iWhoChn).Value = Environ("username")
Wsh.Cells(lRow, iLstCll).Value = .Address
Rem Update Cell Change Comments
sCllCmt = Now & vbCrLf & Environ("UserName") & " *" & .Value & "*"
If Not .Comment Is Nothing Then sCllCmt = sCllCmt & .Comment.Text
.ClearComments
.AddComment sCllCmt
End With
NEXT_Cll:
Next
Rem Restate Application Properties
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
ErrorCatch:
MsgBox Err.Description
Rem Restate Application Properties
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Do let me know of any questions you might have about the resources used in this procedure.
You could use something like this:
Sub SetDateRow(Target As Range, Col As String)
Dim TargetRng As Range
Dim LastCol As Long
Dim LastInputCol As Long
Dim bEvents As Boolean
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 24
End With
bEvents = Application.EnableEvents
Application.EnableEvents = False
If Target.Cells.Count > 1 Then
For Each TargetRng In Target.SpecialCells(xlCellTypeVisible).Areas
Cells(TargetRng.Row, LastCol - 2).Resize(TargetRng.Rows.Count, 1).Value = Now()
Cells(TargetRng.Row, LastCol - 1).Resize(TargetRng.Rows.Count, 1).Value = Environ("username")
Cells(TargetRng.Row, LastCol).Resize(TargetRng.Rows.Count, 1).Value = Target.Address
Next
Else
Cells(Target.Row, LastCol - 2).Value = Now()
Cells(Target.Row, LastCol - 1).Value = Environ("username")
Cells(Target.Row, LastCol).Value = Target.Address
End If
Application.EnableEvents = bEvents
End Sub
but make sure you call it before or after the loop in your change event, not inside it as you are now!
I have five worksheet in all that are using the below code which is stored in a workbook. The first worksheet works perfectly well with the code. The second spreadsheet can check for the first item before returning the error. The subsequent third and fourth worksheet return the error immediately. The fifth worksheet on the other hand return error 400. May I know is my code the source of the problem or it's the checkbox because I copied and paste from the first worksheet.
Sub test5()
Dim MyFile As String
Dim FinalRow As Long
Dim Row As Long
Dim i As Integer
Dim d As Integer
d = 2
i = 0
FinalRow = Cells(Rows.count, "S").End(xlUp).Row
For Row = 3 To FinalRow
If Not IsEmpty(ActiveSheet.Cells(Row, "S")) Then
i = i + 1
d = d + 1
MyFile = ActiveSheet.Cells(Row, "S").Value
If Dir(MyFile) <> "" Then
ActiveSheet.OLEObjects("CheckBox" & i). _
Object.Value = True ' <~~~~~~~~~~~~~~~~ Error occurs here
With ActiveSheet.Cells(d, "F")
.Value = Now
.NumberFormat = "dd/mm/yy"
'If (ActiveSheet.Cells(d, "F") - ActiveSheet.Cells(d, "G") >= 0) Then
' ActiveSheet.Cells(d, "F").Font.Color = vbRed
'End If
If (.Value - .Offset(0, 1).Value) >= 0 Then
.Font.Color = vbRed
Else
.Font.Color = vbBlack
End If
End With
' i = i + 1
'd = d + 1
End If
End If
Next
End Sub
The program terminates after stepping into this line of code:
ActiveSheet.OLEObjects("CheckBox" & i). _ Object.Value = True
OLEObject does not have a member called value. If you are trying to display the OLEObject, use visible instead
ActiveSheet.OLEObjects("CheckBox" & i).Visible = True
See all OLEObject members here :
OLEObject Object Members