+ Reply to Thread
Results 1 to 12 of 12

Sub Or Function not Defined ('Worksheet' not working as intended)

Hybrid View

  1. #1
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Question Sub Or Function not Defined ('Worksheet' not working as intended)

    Its been a long time since i tried VB in excel and i followed this ladies tutorial on youtube (Use Barcodes in Excel to track in and outs - Barb Henderson, 2019 [Youtube]). I copied everything and adjusted it to fit my sheets but I don't know why this error keeps occurring on this line.
    _______________________________________________________________________________

    Sub checkin()
    Dim barcode As String
    Dim rng As Range
    Dim rownumber As Long
    
    
    barcode = Worksheet("Sheet4").Cells(1, 2)
    
        Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
        LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
        If rng Is Nothing Then
            ActiveSheet.Columns("a:a").Find("").Select
            ActiveCell.Value = barcode
            ActiveCell.Offset(0, 1).Select
            ActiveCell.Value = Date & " " & Time
            ActiveCell.NumberFormat = "d/m/yyyy h:mm AM/PM"
            Worksheets("Sheet4").Cells(1, 2) = ""
        End If
    End Sub
    ________
    _______________________________________________________________________

    The error I am getting is "Compile error: Sub or Function not defined" It then highlights the word Worksheet in the yellow line.


    image_2021-12-28_032933.png
    Last edited by aZileanWays2Die; 12-27-2021 at 02:24 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Change below red snippet to Sheets or Worksheets

    barcode = Worksheet("Sheet4").Cells(1, 2)
    Code can be simplified...
    Sub checkin()
    Dim barcode As String, rng As Range
    barcode = Sheets("Sheet4").Cells(1, 2).Value
    Set rng = Sheet1.Columns("A:A").Find(barcode, , xlValues, xlWhole)
    If rng Is Nothing Then Sheet1.Range("A" & Rows.Count).End(xlUp)(2).Resize(, 2) = Array(barcode, Format(Date, "d/m/yyyy h:mm AM/PM"))
    End Sub
    Last edited by Sintek; 12-27-2021 at 02:12 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Cheers, I read over this for over an hour lol! But sadly its now producing the "Run-time error '9': Subscript out of range" and stopping on the same line. I changed it to both Sheets and Worksheets to see if it made a difference to the new error but it didnt. I think this error means im either referencing something that doesnt exist or misusing an array. I dont think im using Arrays but i also cant see any reference that isnt apart of this sheet

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    According to above image there is no sheet called "Sheet4"

    Should it not be...
    Sheets("Check-in")
    or it can be referenced as
    Sheet4
    Last edited by Sintek; 12-27-2021 at 02:16 PM.

  6. #6
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    I believe so. In the screenshot on the original post. Both those exist on the right tab under 'Excel Objects' and are both tabs at the bottom of my excel spreadsheet. Unless there is an extra step for indexing that I don't know about I think it should all be there.

  7. #7
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    I see ur code references. Ill give it ago

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Sheet1 refers Sheets("Inventory")
    Sheet4 refers Sheets("Check-In")

    The 1 & 4 refers Index numbers

    Sub checkin()
    Dim barcode As String, rng As Range
    barcode = Sheet4.Cells(1, 2).Value
    Set rng = Sheet1.Columns("A:A").Find(barcode, , xlValues, xlWhole)
    If rng Is Nothing Then
        Sheet1.Range("A" & Rows.Count).End(xlUp)(2).Resize(, 2) = Array(barcode, Format(Date, "d/m/yyyy h:mm AM/PM"))
    End If
    End Sub

  9. #9
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Gold Star!! That's it you genius person. At least now I know how to reference sheets properly. Thankyou so much

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Pleasure...Welcome to the Forum...Enjoy your learning curve...Happy coding...

    As you are new here, Please take a moment to read all the rules as per ALAN request...
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Also, If you feel a member has helped, pls feel free to click left of post [Add Reputation]

  11. #11
    Registered User
    Join Date
    12-27-2021
    Location
    Australia
    MS-Off Ver
    2021
    Posts
    9

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Cheers, I have done that and followed Alans request the moment I saw it XD You deserve your reputation mate

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Sub Or Function not Defined ('Worksheet' not working as intended)

    Tx for rep + "Actual" Chad

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] IF OR statement not working as intended
    By chris01395 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2020, 09:50 AM
  2. VBA function not working as intended- Loop Problem?
    By KrombopulosMichael in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2018, 05:50 PM
  3. PowerPivot not working as intended?
    By fingerstyle in forum Excel General
    Replies: 11
    Last Post: 10-22-2017, 08:35 PM
  4. [SOLVED] Msgbox not working as intended
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2013, 09:12 AM
  5. [SOLVED] Nested if formula not working as intended
    By Reibie in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-11-2013, 05:59 AM
  6. [SOLVED] VLOOKUP not working as intended
    By RAS 2112 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2013, 08:58 AM
  7. [SOLVED] macro not working as intended
    By Michael A in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2005, 10:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1