+ Reply to Thread
Results 1 to 4 of 4

Excel VBA For Creative Problem Solving Part 1 Assignment 4

  1. #1
    Registered User
    Join Date
    10-04-2023
    Location
    Norfolk, VA
    MS-Off Ver
    Office 365
    Posts
    3

    Excel VBA For Creative Problem Solving Part 1 Assignment 4

    Hi all,
    Can someone to provide me with a to solve the assignment #4? My subroutine as follows:
    Please Login or Register  to view this content.
    Not sure why "expected array" error pops up.

    Please, I just want an "advice" , not a complete fix.

    Thanks in advance
    PS: fairly new with VBA by the way
    Last edited by 6StringJazzer; 10-19-2023 at 01:58 PM. Reason: please use code tags

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,162

    Re: Excel VBA For Creative Problem Solving Part 1 Assignment 4

    This code has a lot of problems for such a short bit of code. I can't get it to compile because of syntax errors.

    That error suggests that you are using syntax to reference an array, but the variable is not declared as an array. I am not getting the same error.

    What is it supposed to do? Giving us a part # and assignment # is not helpful since we don't know what that refers to.

    It appears that you are trying to set the fill color in cells in columns A:C if some condition is met. But I can't make sense out of anything in your If condition.

    Please Login or Register  to view this content.
    identifier is a String variable. Your condition starts with identifier( but that doesn't make sense. There is no matching parenthesis, and there is no syntax for putting a ( after a String variable.

    You refer to Range(ID) but ID is never given a value.

    You are comparing it to Range("identifier") but it's not clear if you intend for this be the variable identifier, or if you have a named range called identifier in your file.
    Same problem with Range("key")
    If you intend for these to be references to your variables, do not use quotes. Also those variable are never given values.

    You are comparing Range(ID) to both Range("identifier") And Range("key"). This condition will be TRUE only if all three are the same.

    Because you have the action after the Then on the same line, you do not need the End If afterwards.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-04-2023
    Location
    Norfolk, VA
    MS-Off Ver
    Office 365
    Posts
    3

    Re: Excel VBA For Creative Problem Solving Part 1 Assignment 4

    Hi again, thanks for the response.

    See below the narrative explaining the assignment:

    Problem Statement:

    INSTRUCTIONS: In the "Assignment 4 - STARTER.xlsm" file, you will find information for a bunch of batches from a certain process. Column A contains the Batch ID, Column B contains the production date, and Column C contains the ship date.

    The Batch ID has a two-digit code to the left of the hyphen and a 3- or 4-digit code to the right of the hyphen. The first letter of the Batch ID is known as the Identifier and the leading number of the 3- or 4-digit code to the right of the hyphen is known as the Key. For example, in the Batch ID "N9-363B", the Identifier is "N" and the Key is 3.

    Your goal is to create a subroutine that allows the user to select the Identifier from a drop-down menu in cell F2 and the Key from a drop-down menu in cell F3 (these drop-down/data validation menus are already available in the starter file) and any rows of the data (columns A, B, and C) whose Batch ID meets those criteria will be highlighted GREEN.

    The RUN button is linked to the HighlightRows subroutine in the starter file. The RESET button is already linked to the Reset subroutine in the starter file so please do not modify it. When run, the RESET button (or Reset sub) will remove any fill color/highlighting from anywhere on the "Data" sheet. However, it will not remove any additional rows that you may add, so be sure to reset the data (by copying/pasting) present in the "Original Data" tab if you add or change any of the original data.

    IMPORTANT: Also contained within the starter file are 3 additional procedures: 1) a sub called Example, 2) a function called Identifier, and 3) a function called Key. The Identifier function takes as an argument a Batch ID and outputs the Identifier. The Key function takes as an argument a Batch ID and outputs the Key. I encourage you to utilize these functions within your HighlightRows sub - you can see how to use these two functions in the Example sub.

    Requirements:

    1) When a match is found in a row, all three columns of data (columns A, B, and C) must be highlighted green.

    2) "Green" refers to the interior color index equal to 4. For example, Range("A1").Interior.ColorIndex = 4 would color cell A1 green. The grader file is specifically looking for this ColorIndex! If you color the cells a different shade of green, it won't pass the grader!

    3) When additional rows are added to the data (for example, in row 27 and beyond), your sub should automatically detect the size of data (number of rows) and adjust accordingly.

    Hints

    I would recommend using the Range approach to working with data arrays such as this. See the screencast "Iterating through a Selection vs. a Range" and pay close attention to the approach using Range instead of Selection.

    Note that in the starter file, cell F2 has been named "identifier" and cell F3 has been named "key".


    And below my unsuccessful attempt:

    [Sub HighlightRows()
    Dim nr As Integer, i As Integer, identifier As String, key As Integer, ID As String

    nr = WorksheetFunction.CountA(Columns("A:A"))


    For i = 2 To nr
    ID = Cells(i, 1)
    If identifier(ID) = Range("identifier") And key(ID) = Range("key") Then Range("A" & i & ":" & "C" & i).Interior.ColorIndex = 4

    End If

    Next i

    End Function]

    The exercises includes the followings codes also (MOT NEEDED TO BE CHECKED:
    Sub Example()
    ' This is just to show how the Identifier and Key functions below can be utilized in VBA code
    Dim ID As String
    ID = "Y4-824X"
    MsgBox "The identifier is " & identifier(ID) & " and the key is " & key(ID)
    End Sub

    Function identifier(ID As String) As String
    identifier = Left(ID, 1)
    End Function

    Function key(ID As String) As Integer
    key = Left(Mid(ID, 4, 4), 1)
    End Function


    When I try to run my code, that is the message: "Compile error: expected array", highlighting the identifier function.

    Thanks again for the help,

  4. #4
    Registered User
    Join Date
    05-14-2025
    Location
    Pune
    MS-Off Ver
    Version 2408
    Posts
    1

    Re: Excel VBA For Creative Problem Solving Part 1 Assignment 4

    Option Explicit

    ' NOTE: For highlighting, use .ColorIndex = 4
    ' For example, Range("A1").Interior.ColorIndex = 4 would color cell A1 green

    Sub HighlightRows()
    Dim nr As Integer, i As Integer, idVal As String, keyVal As Integer, ID As String

    nr = WorksheetFunction.CountA(Columns("A:A"))


    For i = 2 To nr
    ID = Cells(i, 1)
    If Identifier(ID) = Range("identifier") And Key(ID) = Range("key") Then Range("A" & i & ":" & "C" & i).Interior.ColorIndex = 4

    Next i

    End Sub

    Sub Example()
    ' This is just to show how the Identifier and Key functions below can be utilized in VBA code
    Dim ID As String
    ID = "Y4-824X"
    MsgBox "The identifier is " & Identifier(ID) & " and the key is " & Key(ID)
    End Sub

    Function Identifier(ID As String) As String
    Identifier = Left(ID, 1)
    End Function

    Function Key(ID As String) As Integer
    Key = Left(Mid(ID, 4, 4), 1)
    End Function

    Sub Reset()
    ' Obtained through a macro recording:
    With Cells.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    End Sub

+ 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. Help Needed: Excel VBA For Creative Problem Solving Part 1 Assignment 4
    By webcam723 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-04-2023, 08:28 PM
  2. Kindly Help in Solving this Excel Problem
    By yousufsaleem in forum Excel General
    Replies: 1
    Last Post: 12-07-2015, 06:37 PM
  3. Multivariate Calculus in Excel - Help Solving a Problem
    By el4n in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-07-2012, 04:52 PM
  4. Need help in solving a problem using Excel 2007
    By newbieexcel123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2010, 12:59 PM
  5. Mathematic problem solving using Excel
    By Stonewall in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-15-2006, 03:20 PM
  6. [SOLVED] i've got a simple excel problem that needs solving...
    By Massive in forum Excel General
    Replies: 8
    Last Post: 05-16-2005, 04:28 AM
  7. Thorny VBA problem. Any creative solution appreciated.
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2005, 06:06 AM

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