Consulting

Results 1 to 5 of 5

Thread: How does this code work????

  1. #1

    How does this code work????

    I have received this code (I believe from Firefytr) and it worked perfectly in the attached workbook. Even though I have learnt other techniques for hide\ing/unhiding rows, I want to be certain I understand how this code works so that I can adapt it to other situations where the number of rows per block is different:

    [VBA]
    Private Sub OptionButton1_Click()
    ' Auto populate
    For i = 5 To Range("A65536").End(xlUp).Row
    If Left(Range("A" & i), 4) = "Auto" Then
    Rows(i & ":" & i).EntireRow.Hidden = False
    Rows(i + 1 & ":" & i + 1).EntireRow.Hidden = True
    Range("G4") = True
    End If
    Next
    End Sub
    [/VBA]

    Why is i=5?

    In the line:

    [VBA]If Left(Range("A" & i), 4) = "Auto" Then[/VBA]

    what is the purpose of 4 inside the brackets and where is "Auto" being read?

    All very fundamental, but I am easily confused!

  2. #2
    MS Excel MVP VBAX Tutor
    Joined
    Mar 2005
    Posts
    246
    Location
    Quote Originally Posted by K. Georgiadis
    I have received this code (I believe from Firefytr) and it worked perfectly in the attached workbook. Even though I have learnt other techniques for hide\ing/unhiding rows, I want to be certain I understand how this code works so that I can adapt it to other situations where the number of rows per block is different:

    [vba]
    Private Sub OptionButton1_Click()
    ' Auto populate
    For i = 5 To Range("A65536").End(xlUp).Row
    If Left(Range("A" & i), 4) = "Auto" Then
    Rows(i & ":" & i).EntireRow.Hidden = False
    Rows(i + 1 & ":" & i + 1).EntireRow.Hidden = True
    Range("G4") = True
    End If
    Next
    End Sub
    [/vba]

    Why is i=5?
    This is to leave the top 4 rows alone, and only start looking for rows to hide in row 5.

    In the line:

    [vba]If Left(Range("A" & i), 4) = "Auto" Then[/vba]

    what is the purpose of 4 inside the brackets and where is "Auto" being read?
    Left(string, N) returns the leftmost N characters of string.

    Rather than Range("A" & i), it would be better to use Range("A" & i).Value (even though .Value is the default property of the Range object), if only for readability.
    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com
    _______

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You also don't need Rows and EntireRow, they are synonomous

    [vba]
    Rows(i & ":" & i).EntireRow.Hidden = False
    [/vba]
    can be

    [vba]
    Rows(i).Hidden = False
    [/vba]

  4. #4
    MS Excel MVP VBAX Tutor
    Joined
    Mar 2005
    Posts
    246
    Location
    [vba]With Range("A" & i)
    If Left$(.Value, 4) = "Auto" Then
    .EntireRow.Hidden = False
    .Offset(1).EntireRow.Hidden = True
    Range("G4") = True
    End If
    End With[/vba]
    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com
    _______

  5. #5
    Thanks Jon, XLD, it is crystal clear now. Jon, it is good to see your name again; I have always been a fan of your website.

Posting Permissions

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