Results 1 to 15 of 15

Thread: If statements question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    If statements question

    VB Code:
    1. With rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)

    Ath this point the macro has determined the cell to put the data in. I want to write an if statement that says" if the row height is greater than X, move one cell over and append the data.

    I've got every part of this if statement written except for this part. All I need to know is how to write the "move over one cell" part of the code.

    Thanks!
    Last edited by gtg689a; Apr 27th, 2006 at 02:28 PM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Move one cell to the right

    use the .Offset property of the selected cell.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    This is what I have written so far, but it is not working:
    VB Code:
    1. 'Determine which cell in the grid the employee belongs to
    2.                         With rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)
    3.                        
    4.                             If rngCell.RowHeight > 450 Then
    5.                              
    6.                               With rngGrid.Cells(1 + lResCounter, 1 + lCapCounter).Offset(0, 1)
    7.                                      .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    8.                            
    9.                             End With
    10.                             Else
    11.                            
    12.                        
    13.                             .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    14.                                                          
    15.                             End If
    16.                            
    17.                            
    18.                            
    19.                                                                                                                  
    20.                         End With
    21.                        
    22.                     End If

    It is crapping out and giving me the error message "Object Variable or With Block Variable not set".

    Thanks for your help Declan,
    I have almost finished this thing up, I will definitly post the finish product if anyone else want to use it.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    BY THE WAY, probably pretty important here.

    Instead of having 5 columsn for the 5 possible rankings, I have ten columns. I also added 5 options that the Capability array can hold(Blank1, Blank2, Blank3, Blank4, Blank5). But these are not options for someone to choose. They just create a grid that has blank "spillover" columns.

    This is the reason why I want to be able to move one cell to the right if the rowheight has gone over 450(I run a macro after every loop that autofits everything).

  5. #5
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Move one cell to the right

    try this :

    VB Code:
    1. 'Determine which cell in the grid the employee belongs to
    2.                         with rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)
    3.                        
    4.                             If rngCell.RowHeight > 450 Then
    5.                              
    6.                               rngGrid.Cells(1 + lResCounter, 1 + lCapCounter).Offset(0, 1)
    7.                                      .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    8.                            
    9.  
    10.                             Else
    11.                            
    12.                        
    13.                             .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    14.                                                          
    15.                             End If
    16.                            
    17.                            
    18.                            
    19.                                                                                                                  
    20.                         End With
    21.                        
    22.                     End If

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Move one cell to the right

    Try the following
    VB Code:
    1. With rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)
    2.                             If rngCell.RowHeight > 450 Then
    3.                                      .Offset(0, 1).Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    4.                             Else
    5.                                 .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    6.                             End If
    7.                         End With
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    The bionic orange, your way produced a syntax error

    Declan, your way produced the same error as before and highlighted this line when debugging:

    If rngCell.RowHeight > 450 Then

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Move one cell to the right

    OK
    It look slike you haven't correctly set a reference for the rngCell object variable.
    Should rngCell and rngGrid.Cells(1 + lResCounter, 1 + lCapCounter) be the same cell?
    if so then try the following...
    VB Code:
    1. Set rngCell = rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)
    2.                         With rngCell
    3.                             If .RowHeight > 450 Then
    4.                                      .Offset(0, 1).Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    5.                             Else
    6.                                 .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    7.                             End If
    8.                         End With
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    Well it works, but at the same time it doesn't work. *sigh*

    I am not really sure how to explain what it is doing wrong, so I am posting it here. It should be inserting all of the names, and then once it inserts a name when the cell row height has reached 350, it should start putting the names in the cell directly to the right of the one it was originally going to put it in.

    ***However, it is moving the entire contents of the 1st cell over to the cell to the right and appending the new name. It is only supposed to move to the next cell and start appending, not bringing anything with it.*****



    WARNING- This macro takes about 15 sec. to run, probably not the most efficient use of code.
    Attached Files Attached Files
    Last edited by gtg689a; Apr 18th, 2006 at 10:48 AM.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    Nevermind, its good to go!

  11. #11
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Move one cell to the right

    You all sorted now ?
    If so ... its probably worth posting your code in case anyone else comes across a similar problem.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    I wish I could say I wrote all of this, but DKenny wrote 85% of it. Thanks to everyone who helped out.

    Out of curiousity(haha).... if I wanted to add a distribution counter at the bottom, how could I do that? In other words, I guess I need something that adds up how many names are in each box on the grid.

    Obviously, I've got the main issue solved here, but just in case anyone wants to impart more knowledge!


    VB Code:
    1. Sub PerformanceGrid()
    2. Dim rngMgrInput As Range
    3. Dim rngGrid As Range
    4. Dim saEmployee() As String
    5. Dim lRecordNum As Long
    6. Dim vResults As Variant
    7. Dim vCapability As Variant
    8. Dim lResCounter As Long
    9. Dim lCapCounter As Long
    10. Dim rngCell As Range
    11. Dim lHeight As Long
    12.  
    13.     'Build two arrays to hold the options for Results and Capability
    14.     vResults = Array("Consistently/Sustainable Exceeded", "Partially Exceeded", "Achieved", "Partially Achieved", "Not Achieved")
    15.     vCapability = Array("Unsatisfactory", "Blank1", "Needs Improvement", "Blank2", "Meets Expectations", "Blank3", "Exceeds Expectations", "Blank4", "Excellent", "Blank5")
    16.    
    17.     'Get the Input and output ranges
    18.     Set rngMgrInput = ThisWorkbook.Worksheets("Manager Input").UsedRange
    19.     Set rngGrid = ThisWorkbook.Worksheets("Performance Grid").Range("B7:K11")
    20.    
    21.     'Resize the employee array to hold 3 pieces of data for each employee Name, Results and Capability
    22.     ReDim saEmployee(1 To 3, 1 To rngMgrInput.Rows.Count - 2)
    23.    
    24.     'Loop through the input table, adding each employee, their result, and capability
    25.     For lRecordNum = 1 To rngMgrInput.Rows.Count - 2
    26.         saEmployee(1, lRecordNum) = rngMgrInput.Cells(2 + lRecordNum, 1)
    27.         saEmployee(2, lRecordNum) = rngMgrInput.Cells(2 + lRecordNum, 6)
    28.         saEmployee(3, lRecordNum) = rngMgrInput.Cells(2 + lRecordNum, 11)
    29.      
    30.     Next lRecordNum
    31.    
    32.     'Clear the output grid
    33.     rngGrid.ClearContents
    34.    
    35.     'Loop through each record in the Employee array
    36.     For lRecordNum = 1 To UBound(saEmployee, 2)
    37.        
    38.         'Loop through each record in the Results array
    39.         For lResCounter = LBound(vResults) To UBound(vResults)
    40.            
    41.             'Loop through each record in the Capability array
    42.             For lCapCounter = LBound(vCapability) To UBound(vCapability)
    43.                    
    44.                     'If the current employee has both the current Results
    45.                     'and Capability value then add them to that cell in the grid
    46.                     If saEmployee(2, lRecordNum) = vResults(lResCounter) _
    47.                     And saEmployee(3, lRecordNum) = vCapability(lCapCounter) Then
    48.                        
    49.                         'Determine which cell in the grid the employee belongs to, if the row height is above 350, switch to the next cell over
    50.                                  Set rngCell = rngGrid.Cells(1 + lResCounter, 1 + lCapCounter)
    51.                         With rngCell
    52.                             If .RowHeight > 350 Then
    53.                                        With .Offset(0, 1)
    54.                                         If .Value = "" Then
    55.                                         .Value = saEmployee(1, lRecordNum)
    56.                                          Else
    57.                                        
    58.                                           .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    59.                                           End If
    60.                                      End With
    61.                                    
    62.                             Else
    63.                                 .Value = .Value & Chr(10) & saEmployee(1, lRecordNum)
    64.                             End If
    65.                         End With
    66.                         End If
    67.                    
    68.                     'run the "fitnicely" macro
    69.                     Application.Run "FitNicely"
    70.                    
    71.            
    72.             Next lCapCounter
    73.         Next lResCounter
    74.     Next lRecordNum
    75.    
    76.    
    77.     Sheets("Performance Grid").Select
    78.    
    79.    
    80.     'Application.Run "CompensationGrid"
    81. End Sub

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    Ok, I would like to add a rngCell2 to this code. underneath this grid, I want a distribution list. There are 10 categories of people, based on what box they are in on the grid. Underneath the grid, I would like 10 columns, and in the macro...

    In the macro I would like to have If statements that say
    If the results counter = X and the CapCounter = Y, then put this person's name on the next line in this column Z.

    I don't know how to write that part of the code, and are there any special considerations to writing a ton(25 to be exact) of If statements like that.

    Or could I say.. If rescounter = A and Capcounter = B or If rescounter = C and Capcounter = d OR If rescounter = E and Capcounter = F
    Then.. put the next name in this column? Thanks!
    Last edited by gtg689a; Apr 27th, 2006 at 09:32 AM.

  14. #14
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Move one cell to the right

    Gtg
    Can you explain this a little more? I don't understand what exactly you are after here.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Move one cell to the right

    Sure,
    Right now the macro from the code above puts everyone's names on this grid that is 5x5. You could further divide this grid into 5 "zones" made up of different boxes. I need to have a Distribution chart at the bottom that has 5 columns (1 for each "zone") and then underneath lists the people in each zone in seperate cells.

    So, I figured this could be accomplished because when the macro says... Ok, this person is supposed to be in box "Results 2 by Capabilities 3", I should put them under the next available cell in Zone 3 of the distribution list.

Posting Permissions

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



Click Here to Expand Forum to Full Width