+ Reply to Thread
Results 1 to 4 of 4

Locate cell name using variable and looping

  1. #1
    Registered User
    Join Date
    10-12-2005
    Posts
    2

    Locate cell name using variable and looping

    Hi everybody

    I have named some cells in a worksheet in the following way: Skjul1, Skjul2, Skjul3... Skjul69, Skjul70.

    Now I would like to make a vba script that locates each cell and checks the value of the cell. If the value = 0, the entire row should be hidden.

    I thought that I could make a loop like the following:

    Private Sub Skjul()
    Dim i As Long
    i = 0
    Do Until i = 75
    If Sheets("spec").Range("Skjul" & i).Value = "0" Then
    Sheets("spec").Range("Skjul" & i).EntireRow.Hidden = True
    Else
    Sheets("spec").Range("Skjul" & i).EntireRow.Hidden = False
    End If
    On Error GoTo 0
    i = i + 1
    Loop Until i = 75
    End Sub

    ... but it doesn't work! I get the error: "Run-time error '1004': Application-defined or object-defined error".

    Can anyone help me? I would really appreciate the effort!

  2. #2
    Executor
    Guest

    Re: Locate cell name using variable and looping

    Hi Thomas,

    Your codepart "Skjul" & i might create the problem
    Try this

    Sub hideSkjul()
    Dim i As Integer
    Dim sName As String

    For i = 1 To 75
    sName = "Skjul" & Trim(CStr(i))
    If Sheets("spec").Range(sName).Value = 0 Then
    Sheets("spec").Range(sName).EntireRow.Hidden = True
    Else
    Sheets("spec").Range(sName).EntireRow.Hidden = False
    End If
    Next
    End Sub

    Hoop This Helps

    Wouter Magre


  3. #3
    Norman Jones
    Guest

    Re: Locate cell name using variable and looping

    Hi Thomas,

    The following worked for me
    '==============>>
    Sub Skjul()
    Dim i As Long
    For i = 1 To 75
    On Error Resume Next 'In case range does not exist
    With Sheets("spec").Range("skjul" & i)
    .EntireRow.Hidden = .Value = 0
    End With
    Next
    On Error GoTo 0
    End Sub
    '<<==============

    ---
    Regards,
    Norman



    "Thomas Olesen" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > Hi everybody
    >
    > I have named some cells in a worksheet in the following way: Skjul1,
    > Skjul2, Skjul3... Skjul69, Skjul70.
    >
    > Now I would like to make a vba script that locates each cell and checks
    > the value of the cell. If the value = 0, the entire row should be
    > hidden.
    >
    > I thought that I could make a loop like the following:
    >
    > Private Sub Skjul()
    > Dim i As Long
    > i = 0
    > Do Until i = 75
    > If Sheets("spec").Range("Skjul" & i).Value = "0" Then
    > Sheets("spec").Range("Skjul" & i).EntireRow.Hidden =
    > True
    > Else
    > Sheets("spec").Range("Skjul" & i).EntireRow.Hidden =
    > False
    > End If
    > On Error GoTo 0
    > i = i + 1
    > Loop Until i = 75
    > End Sub
    >
    > .. but it doesn't work! I get the error: "Run-time error '1004':
    > Application-defined or object-defined error".
    >
    > Can anyone help me? I would really appreciate the effort!
    >
    >
    > --
    > Thomas Olesen
    > ------------------------------------------------------------------------
    > Thomas Olesen's Profile:
    > http://www.excelforum.com/member.php...o&userid=28025
    > View this thread: http://www.excelforum.com/showthread...hreadid=475313
    >




  4. #4
    Registered User
    Join Date
    10-12-2005
    Posts
    2

    Thumbs up

    Hi Norman & Wouter Magre

    Thanks a lot for your help - It works like a charm!

    I spent 5 hours already trying all sorts of combinations, since I am a beginner at this, so you have been very helpful.

    Thanks again.

    Thomas Olesen

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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