Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Row Mixup

Tags:

excel

vba

To start with, I have never seen anything like this in 15 years of Excel programming.

On a lighter note I feel that this particular Excel File is Haunted :P

On a serious note, I really can't understand what's going on.

Problem

The code (I didn't write it) runs and hides the rows based on a condition. When you click on any cell except in column C in row 59, it shows that it is from row 58. Only in cell C, it shows the correct address.

Screenshot

enter image description here

What have I tried?

  1. First I thought it is a ScreenUpdating issue but as you can see the code, that this is ruled out.
  2. I manually went into all those cells starting from Col A to Col D and in Immediate window typed ?Activecell.Row. I got 58,59,58,58
  3. Checked Google if anyone has ever experienced this kind of issue. But couldn't find a single instance! I don't know what else could be the issue.

Code

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim i, j, k, lastrow As Long
    Dim finlast As Long
    
    Application.ScreenUpdating = False
    
    'Income_Disc
    lastrow = Sheets("Financial_Disc1").Range("E65536").End(xlUp).Row
    finlast = Sheets("Financial_Disc").Range("A65536").End(xlUp).Value + 1
    
    If Target.Column = 9 And Target.Row = 1 Then
        'Unhide Rows if I1 is double clicked
        Sheets("Financial_Disc1").Rows("1:65536").EntireRow.Hidden = False
    Else
        If Target.Row > 7 And Target.Row < lastrow Then
            If Target.Column = 9 Then
                'Hide Rows
                For j = Target.Row To lastrow
                    If Sheets("Financial_Disc1").Range("B" & CStr(j + 1)) <> "" Then
                        'Hide Rows
                        Sheets("Financial_Disc1").Range("A" & CStr(j)).EntireRow.Hidden = True
                        Exit For
                    Else
                        'Hide Rows
                        Sheets("Financial_Disc1").Range("A" & CStr(j)).EntireRow.Hidden = True
                    End If
                Next j
            End If
        End If
    End If
    
    Sheets("Financial_Disc1").Range("A8:A65536").ClearContents
    
    For i = 8 To lastrow
        If Sheets("Financial_Disc1").Range("A" & CStr(i)).EntireRow.Hidden = True Then
            If Sheets("Financial_Disc1").Range("B" & CStr(i)) <> "" Then
                Sheets("Financial_Disc1").Range("A" & CStr(i)) = ""
            End If
        Else
            If Sheets("Financial_Disc1").Range("B" & CStr(i)) <> "" Then
                Sheets("Financial_Disc1").Range("A" & CStr(i)) = finlast
                finlast = finlast + 1
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Misc Points

As you can see this file has columns in reverse because it is from one of my client in Saudi Arabia.

My Question

Can anyone see what could be the problem? Or I should simply dismiss it as a "Rare" bug?

like image 700
Siddharth Rout Avatar asked May 23 '15 21:05

Siddharth Rout


1 Answers

Please check the row for merged cells - traversing them can cause row "shifts"

like image 86
Gary's Student Avatar answered Oct 19 '22 03:10

Gary's Student