+ Reply to Thread
Results 1 to 17 of 17

Move all rows 1 down but skip row 26 & Public Function IF problem

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    142

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    Ok, so the part that was supposed to be bold is

    If Target = 0 Then

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    You said:
    i end up with little holes in the list whenever i finish a task because what my macroes does is whenever a job is marked as done on the list it moves that job to sheet2
    But, then

    I'd like to have a macro that shifts all the rows 1 down whenever a job is completed
    So, which sheet gets the rows moved down? Sheet2 with its list of completed jobs do that the latest job is at the top?
    Or, the list on sheet1? It seems to me that if you move a record from sheet1 to sheet2 leaving a hole in sheet1, then you would want to move the tasks below the hole UP one to fill that hole, not down one.

    However, prefix , you did not provide any sample tasks for either sheet.
    Ben Van Johnson

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    It sounds like you want row 26 to be stationary.
    If a row (or non-whole row cells?) were inserted above that, you would want the old row 25 to become the new 27 as everything else shifts down.

    Is that the situation?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    142

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    Quote Originally Posted by mikerickson View Post
    It sounds like you want row 26 to be stationary.
    If a row (or non-whole row cells?) were inserted above that, you would want the old row 25 to become the new 27 as everything else shifts down.

    Is that the situation?
    Indeed, that is the situation

    Any help would be greatly appreciated
    Last edited by prefix; 01-25-2010 at 03:54 AM.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    You could put this in the sheet's code module. Then if a cell (in a discreet location) had the formula =ROWS(1:65536), the Calculate event would be triggered by the inserting or deleting of a cell.
    Private Sub Worksheet_Calculate()
        Dim combinedRange As Range, oneArea As Range, homeRange As Range
        Dim colNum As Long
        Const stationaryRow As Long = 5
        Const namePrefix As String = "stationaryCell"
        Const lowColumn As Long = 1
        Const highColumn As Long = 50
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlManual
            .EnableEvents = False
        End With
        
        On Error GoTo CreateCellName
        Set combinedRange = ThisWorkbook.Names(namePrefix & lowColumn).RefersToRange
        For colNum = lowColumn To highColumn
            With ThisWorkbook.Names(namePrefix & colNum).RefersToRange
                Set combinedRange = Application.Union(combinedRange, .Cells)
            End With
        Next colNum
        On Error GoTo 0
        
        For Each oneArea In combinedRange.Areas
            Set homeRange = oneArea.EntireColumn.Rows(stationaryRow)
            Select Case oneArea.Row
                Case Is < stationaryRow: Rem some cell above is deleted
                    homeRange.Cut
                    oneArea.Insert shift:=xlDown
                Case Is > stationaryRow: Rem some cell was inserted
                    oneArea.Cut
                    homeRange.Insert shift:=xlDown
            End Select
        Next oneArea
        
        With Application
            .CutCopyMode = False
            .Calculation = xlAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        Exit Sub
    
    CreateCellName:
        If Err <> 1004 Then GoTo Halt
        If colNum < 1 Then colNum = 1
        ThisWorkbook.Sheets("Sheet1").Cells(stationaryRow, colNum).Name = namePrefix & colNum
        ThisWorkbook.Names(namePrefix & colNum).Visible = False
        Err.Clear
        Resume
    Halt:
        Err.Clear
        On Error GoTo 0
        With Application
            .Calculation = xlAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    In the attached Row 5 is the stationary row.
    The action is restricted to columns 1-50 of that row, but the constants can be altered to meet your situation.
    For example, you want stationaryRow to be 25

    Edit: One line should be changed.
    Attached Files Attached Files
    Last edited by mikerickson; 01-25-2010 at 10:07 AM.

  6. #6
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    142

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    Awesome! Thanks heaps for that answer :-)
    Wish there was a feature for this in Excel where you could ignore a certain row from Shift:=xlDown. Oh well, this code you wrote is certainly very flexible and it can probably be implemented into any existing sheet again, Thank you for spending your time to help me. Rep has been added

    Hate to ask but does anyone know the answer to my second question too? My workbook won't run without solving that particular issue.

    To recap i have the code i want but it seems to be working outside its range from what i can understand, this is the code:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    If Intersect(Target, Range("A15:A77,L15:L77")) Is Nothing Then Exit Sub
    
    If Target = 0 Then
       Target.NumberFormat = "dd.mm.yy"
       Target.Value = Date
    End If
    End Sub
    Whenever i click a button i made that runs this macro:

    Sub Row2Copy()
    
    Dim Answer As String
    Dim MyNote As String
    
    'Place your text here
        MyNote = "Vil du flytte raden til ferdige oppgaver?"
    
        'Display MessageBox
        Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???")
    
        If Answer = vbNo Then
            'Code for No button Press
            Exit Sub
        Else
            'Code for Yes button Press
            Sheets("Aktive Oppgaver").Activate
            Range("A16:L16").Select
            Selection.Copy
            Sheets("Ferdige Oppgaver").Activate
            Rows("15:15").Select
            Selection.Insert Shift:=xlDown
            Sheets("Aktive Oppgaver").Activate
            Range("K16").Select
            Selection.EntireRow.Select
            Selection.ClearContents
            Call RemovePic2
        End If
    End Sub
    The RemovePic2 macro code is:

    Sub RemovePic2()
    Dim Sh As Shape
    With Worksheets("Aktive Oppgaver")
    For Each Sh In .Shapes
    If Not Application.Intersect(Sh.TopLeftCell, .Range("K16:K16")) Is Nothing Then
    If Sh.Type = msoPicture Then Sh.Delete
    End If
    Next Sh
    End With
    End Sub
    I can only assume something here is messing with the Timestamp code.

    When i run the code i just pasted i get: Run time error 13 type mismatch and when i click debug i'm pointed to:

    If Target = 0 Then
    Inside the Timestamp function.

    Anyone got any ideas?

  7. #7
    Forum Contributor
    Join Date
    09-08-2009
    Location
    Norway
    MS-Off Ver
    Excel 2002
    Posts
    142

    Re: Move all rows 1 down but skip row 26 & Public Function IF problem

    Quote Originally Posted by mikerickson View Post
    You could put this in the sheet's code module. Then if a cell (in a discreet location) had the formula =ROWS(1:65536), the Calculate event would be triggered by the inserting or deleting of a cell.
    Private Sub Worksheet_Calculate()
        Dim combinedRange As Range, oneArea As Range, homeRange As Range
        Dim colNum As Long
        Const stationaryRow As Long = 5
        Const namePrefix As String = "stationaryCell"
        Const lowColumn As Long = 1
        Const highColumn As Long = 50
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlManual
            .EnableEvents = False
        End With
        
        On Error GoTo CreateCellName
        Set combinedRange = ThisWorkbook.Names(namePrefix & lowColumn).RefersToRange
        For colNum = lowColumn To highColumn
            With ThisWorkbook.Names(namePrefix & colNum).RefersToRange
                Set combinedRange = Application.Union(combinedRange, .Cells)
            End With
        Next colNum
        On Error GoTo 0
        
        For Each oneArea In combinedRange.Areas
            Set homeRange = oneArea.EntireColumn.Rows(stationaryRow)
            Select Case oneArea.Row
                Case Is < stationaryRow: Rem some cell above is deleted
                    homeRange.Cut
                    oneArea.Insert shift:=xlDown
                Case Is > stationaryRow: Rem some cell was inserted
                    oneArea.Cut
                    homeRange.Insert shift:=xlDown
            End Select
        Next oneArea
        
        With Application
            .CutCopyMode = False
            .Calculation = xlAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        Exit Sub
    
    CreateCellName:
        If Err <> 1004 Then GoTo Halt
        If colNum < 1 Then colNum = 1
        ThisWorkbook.Sheets("Sheet1").Cells(stationaryRow, colNum).Name = namePrefix & colNum
        ThisWorkbook.Names(namePrefix & colNum).Visible = False
        Err.Clear
        Resume
    Halt:
        Err.Clear
        On Error GoTo 0
        With Application
            .Calculation = xlAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    In the attached Row 5 is the stationary row.
    The action is restricted to columns 1-50 of that row, but the constants can be altered to meet your situation.
    For example, you want stationaryRow to be 25

    Edit: One line should be changed.
    I am trying to get this to work so far it does not do what i want.. I am posting my entire workbook since it does not contain any sensitive information.

    Whenever i insert a row stationary row 26 becomes row 27 and whenever my macro does selection.clearcontent and a gap appears between lines(you'll see what i mean in the worksheet) it does not shift them in the proper order..

    ...Once again i failed to upload a file attachment to this post and had to resort to alternative file hosting, hopefully this will be ok with whoever writes an answer.. I would use excelforum if i could but it just gives me errors.. and no, the xls is not above 1mb :P

    Anyway, here's the workbook just type in the captcha and hit Free Download..

    http://ulozto.net/3797153/tiltakslistev3.xls

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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