Home

Run time 424 error when using a Range in For loop

%3CLINGO-SUB%20id%3D%22lingo-sub-717241%22%20slang%3D%22en-US%22%3ERun%20time%20424%20error%20when%20using%20a%20Range%20in%20For%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-717241%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Experts%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20a%20novice%20to%20VBA%20scripting%20learning%20to%20code.%26nbsp%3B%20I%20am%20building%20a%20utility%20using%20VBA%20to%20work%20on%20one%20my%20Excel%20workbooks.%26nbsp%3B%20The%20utility%20is%20supposed%20to%20look%20at%20worksheet%20%22Workshop%20Listing%22%20-%20read%20each%20of%20the%20cells%20in%20a%20specific%20column%2C%20which%20holds%20the%20dates.%26nbsp%3B%20There%20is%20another%20worksheet%20called%20Calendar%20which%20has%20the%20calendar%20structure%20built.%26nbsp%3B%20Based%20on%20the%20date%20value%20read%2C%20the%20sub%20is%20supposed%20to%20copy%20the%20text%20from%20another%20column%20in%20the%20workshop%20listing%20sheet%20and%20go%20to%20the%20calendar%20worksheet%20and%20insert%20copied%20text%20under%20the%20date%20value%20row%20in%20the%20calendar.%26nbsp%3B%20While%20I%20am%20trying%20to%20run%20the%20code%20and%20understand%20the%20issues%2C%20I%20am%20unable%20to%20debug%20the%20error%20I%20am%20encountering%20when%20I%20m%20calling%20the%20range%20and%20using%20it%20in%20a%20For%20loop.%26nbsp%3B%20Error%20is%20-%20Run%20time%20error%20'424'%3A%20Object%20required.%26nbsp%3B%20Can%20anyone%20help%20me%20understand%20why%20this%20error%20is%20showing%20up%20and%20what%20should%20I%20correct%20in%20calling%20the%20range%20in%20the%20For%20loop%3F%26nbsp%3B%20%26nbsp%3BBelow%20is%20the%20code%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Calendar_function()%3CBR%20%2F%3EDim%20dates_input%2C%20c_copy_addr%20As%20Range%3CBR%20%2F%3EDim%20c_m%2C%20c_d%2C%20c_fmrow%2C%20c_fmaddr%2C%20c_nm%2C%20c_nmaddr%20As%20String%3CBR%20%2F%3EDim%20c_s_rg%2C%20c_t_range%20As%20Range%3CBR%20%2F%3EDim%20c_t_cell%2C%20c_t_blank%20As%20String%3CBR%20%2F%3EDim%20c_det_cell%20As%20Range%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EMsgBox%20%22This%20is%20the%20Calendar%20routine%22%3CBR%20%2F%3EMsgBox%20%22This%20routine%20will%20loop%20through%20each%20cell%20of%20the%20range%20provided%20as%20input%20and%20inserts%20the%20corresponding%20meeting%20text%20from%20another%20column%20into%20the%20respective%20calendar%20section%20based%20on%20the%20date%20it%20belongs%22%3C%2FP%3E%3CP%3E'Set%20dates_input%20%3D%20Application.ActiveWorkbook.Worksheets(%22Workshop%20Listing%22).Range(%22K2%3AK66%22)%3CBR%20%2F%3E'Set%20dates_input%20%3D%20Application.InputBox(%22Please%20select%20the%20Column%20of%20cells%20to%20be%20considered%20for%20processing%22%2C%20%22Data%20Range%22)'MsgBox%20(%22the%20range%20selected%20is%22%20%26amp%3B%20dates_input)%3CBR%20%2F%3E'Debug.Print%20dates_input.Cells(1%2C%201).Value%3C%2FP%3E%3CP%3EFor%20Each%20cell%20In%20Worksheets(%22Workshop%20Listing%22).Range(%22K2%3AK66%22)%3CBR%20%2F%3ESet%20c_copy_addr%20%3D%20cell.Address.Offset(0%2C%20-7)%3CBR%20%2F%3Ec_m%20%3D%20Month(cell.Value)%3CBR%20%2F%3EMsgBox%20%22current%20cell%20selected%20is%22%20%26amp%3B%20cell.Address%20%26amp%3B%20%22%20The%20month%20derived%20is%22%20%26amp%3B%20c_m%3CBR%20%2F%3Ec_d%20%3D%20Day(cell.Value)%3CBR%20%2F%3EActiveWorkbook.Sheets(%22Calendar%22).Activate%3CBR%20%2F%3Ec_fmrow%20%3D%20Range(%22AE%3AAE%22).Find(What%3A%3Dc_m%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchOrder%3A%3DxlByColumns%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue).Row%3CBR%20%2F%3Ec_fmaddr%20%3D%20c_fmrow%20%26amp%3B%201%3CBR%20%2F%3Ec_nm%20%3D%20Month(cell.Value)%20%2B%201%3CBR%20%2F%3Ec_nmaddr%20%3D%20Range(%22AE%3AAE%22).Find(What%3A%3Dc_nm%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchOrder%3A%3DxlByColumns%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue).Address%3CBR%20%2F%3ESet%20c_s_rg%20%3D%20Range(%22c_fmaddr%3Ac_nmaddr%22)%3CBR%20%2F%3Ec_t_range%20%3D%20c_s_rg.Find(What%3A%3Dc_d%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue).Offset(1%2C%200)%3CBR%20%2F%3Ec_t_cell%20%3D%20c_s_rg.Find(What%3A%3Dc_d%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue).Offset(1%2C%200)%3CBR%20%2F%3EIf%20IsEmpty(c_t_cell)%20%3D%20%22TRUE%22%20Then%3CBR%20%2F%3ERange(c_t_range).EntireRow.Insert%3CBR%20%2F%3Ec_det_cell%20%3D%20Range(c_t_cell).Offset(1%2C%200)%3CBR%20%2F%3ERange(c_copy_addr).Copy%20c_det_cell%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E'ActiveWorkbook.Sheets(%22Workshop%20Listing%22).Activate%3CBR%20%2F%3ENext%20cell%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-717241%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-719240%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20time%20424%20error%20when%20using%20a%20Range%20in%20For%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-719240%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F365606%22%20target%3D%22_blank%22%3E%40ukmohan81%3C%2FA%3E%20Can%20you%20please%20indicate%20on%20which%20line%20the%20error%20occurs%20precisely%3F%3C%2FP%3E%0A%3CP%3EI%20expect%20it%20is%20on%20this%20line%3A%3C%2FP%3E%0A%3CPRE%3Ec_fmrow%20%3D%20Range(%22AE%3AAE%22).Find(What%3A%3Dc_m%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchOrder%3A%3DxlByColumns%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue).Row%3C%2FPRE%3E%0A%3CP%3Eand%20happens%20when%20the%20c_m%20value%20cannot%20be%20found.%20Better%20to%20use%20an%20object%20variable%3A%3C%2FP%3E%0A%3CPRE%3E%20%20%20%20%20%20%20%20Dim%20FoundCell%20As%20Range%3CBR%20%2F%3E%20%20%20%20%20%20%20%20Set%20FoundCell%20%3D%20Nothing%0A%20%20%20%20%20%20%20%20Set%20FoundCell%20%3D%20Range(%22AE%3AAE%22).Find(What%3A%3Dc_m%2C%20LookIn%3A%3DxlValues%2C%20LookAt%3A%3DxlWhole%2C%20SearchOrder%3A%3DxlByColumns%2C%20SearchDirection%3A%3DxlNext%2C%20MatchCase%3A%3DTrue)%0A%20%20%20%20%20%20%20%20If%20Not%20FoundCell%20Is%20nothing%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20c_fmrow%20%3D%20FoundCell.Row%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20'Not%20found%2C%20do%20something!%0A%20%20%20%20%20%20%20%20End%20If%0A%3C%2FPRE%3E%0A%3CP%3ESome%20comments%20on%20your%20code%3A%3C%2FP%3E%0A%3CP%3E-%20The%20variable%20cell%20is%20not%20declared%3C%2FP%3E%0A%3CP%3E-%20Your%20DIM%20statements%20are%20wrong%3A%3C%2FP%3E%0A%3CPRE%3EDim%20dates_input%2C%20c_copy_addr%20As%20Range%3C%2FPRE%3E%0A%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3Eonly%20declares%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3Ec_copy_addr%20As%20Range%3C%2FFONT%3E%2C%20dates_input%20is%20declared%20as%20variant.%20The%20correct%20syntax%20is%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CPRE%3E%20Dim%20dates_input%20As%20Range%2C%20c_copy_addr%20As%20Range%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
ukmohan81
Occasional Visitor

Hi Experts, 

I am a novice to VBA scripting learning to code.  I am building a utility using VBA to work on one my Excel workbooks.  The utility is supposed to look at worksheet "Workshop Listing" - read each of the cells in a specific column, which holds the dates.  There is another worksheet called Calendar which has the calendar structure built.  Based on the date value read, the sub is supposed to copy the text from another column in the workshop listing sheet and go to the calendar worksheet and insert copied text under the date value row in the calendar.  While I am trying to run the code and understand the issues, I am unable to debug the error I am encountering when I m calling the range and using it in a For loop.  Error is - Run time error '424': Object required.  Can anyone help me understand why this error is showing up and what should I correct in calling the range in the For loop?   Below is the code :

 

Sub Calendar_function()
Dim dates_input, c_copy_addr As Range
Dim c_m, c_d, c_fmrow, c_fmaddr, c_nm, c_nmaddr As String
Dim c_s_rg, c_t_range As Range
Dim c_t_cell, c_t_blank As String
Dim c_det_cell As Range


MsgBox "This is the Calendar routine"
MsgBox "This routine will loop through each cell of the range provided as input and inserts the corresponding meeting text from another column into the respective calendar section based on the date it belongs"

'Set dates_input = Application.ActiveWorkbook.Worksheets("Workshop Listing").Range("K2:K66")
'Set dates_input = Application.InputBox("Please select the Column of cells to be considered for processing", "Data Range")'MsgBox ("the range selected is" & dates_input)
'Debug.Print dates_input.Cells(1, 1).Value

For Each cell In Worksheets("Workshop Listing").Range("K2:K66")
Set c_copy_addr = cell.Address.Offset(0, -7)
c_m = Month(cell.Value)
MsgBox "current cell selected is" & cell.Address & " The month derived is" & c_m
c_d = Day(cell.Value)
ActiveWorkbook.Sheets("Calendar").Activate
c_fmrow = Range("AE:AE").Find(What:=c_m, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=True).Row
c_fmaddr = c_fmrow & 1
c_nm = Month(cell.Value) + 1
c_nmaddr = Range("AE:AE").Find(What:=c_nm, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=True).Address
Set c_s_rg = Range("c_fmaddr:c_nmaddr")
c_t_range = c_s_rg.Find(What:=c_d, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True).Offset(1, 0)
c_t_cell = c_s_rg.Find(What:=c_d, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True).Offset(1, 0)
If IsEmpty(c_t_cell) = "TRUE" Then
Range(c_t_range).EntireRow.Insert
c_det_cell = Range(c_t_cell).Offset(1, 0)
Range(c_copy_addr).Copy c_det_cell
End If
'ActiveWorkbook.Sheets("Workshop Listing").Activate
Next cell
End Sub

1 Reply

@ukmohan81 Can you please indicate on which line the error occurs precisely?

I expect it is on this line:

c_fmrow = Range("AE:AE").Find(What:=c_m, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=True).Row

and happens when the c_m value cannot be found. Better to use an object variable:

        Dim FoundCell As Range
Set FoundCell = Nothing Set FoundCell = Range("AE:AE").Find(What:=c_m, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=True) If Not FoundCell Is nothing Then c_fmrow = FoundCell.Row Else 'Not found, do something! End If

Some comments on your code:

- The variable cell is not declared

- Your DIM statements are wrong:

Dim dates_input, c_copy_addr As Range

only declares c_copy_addr As Range, dates_input is declared as variant. The correct syntax is:

 Dim dates_input As Range, c_copy_addr As Range
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies