Macro bugs on sheet change

Rıfat Yerusalmi

I have been working on a project with Excel. Thanks to Stack Overflow users :), I have couple of worksheet_change macros. Everything works flawlessly but one of them gives run-time error '1004' in some cases.

  Application.ScreenUpdating = False
If Target.Count > 1 Then Exit Sub
If Target.Column <= 25 Then
    If Target.value <> "" Then
    Range("A" & Target.Row & ":Y" & Target.Row).Select
    Selection.borders(xlDiagonalDown).LineStyle = xlNone
    Selection.borders(xlDiagonalUp).LineStyle = xlNone

        With Selection.borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    Selection.borders(xlInsideHorizontal).LineStyle = xlContinuous
    Application.ScreenUpdating = True
    End If
End If

So everytime I enter data in a cell, macro puts borders automatically starting from Column A till Column Y. It works alright but if a cell is selected (not a single selection but double click selection into the cell), if I try to change the sheet it gives me an error with this highlighted line:

Range("A" & Target.Row & ":Y" & Target.Row).Select

I could just deselect the cell and change sheets but I was wondering if it could be fixed within the macro?

Rory

There's no need to select at all:

If Target.Column <= 25 Then
    If Target.Value <> "" Then
        With Range("A" & Target.Row & ":Y" & Target.Row)
            .Borders(xlDiagonalDown).LineStyle = xlNone
            .Borders(xlDiagonalUp).LineStyle = xlNone

            With .Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With

            .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        End With
        Application.ScreenUpdating = True
    End If
End If

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MSG Macro on Sheet change if offset value = TRUE

How to I change this Excel macro (VBA) to run on any sheet?

Can I add a macro to change the value of a cell in sheet 2 based on increasing the value of a cell in sheet 1

Automatic Macro for every sheet

Running a macro on a protected sheet

macOS Dark Mode UI bugs with NSPredicateEditor in a Sheet

Qt Style Sheet Bugs (7 years later)

Run macro when on Worksheet change (i.e. changing sheets, not data within sheet)

Excel form control macro updates another sheet, Worksheet_Change event does not trigger

VBA Macro Works on sheet 1 but not on Sheet 2

Rename Excel Sheet with VBA Macro

"Reference must be to a macro sheet" error

Call Macro on a specific sheet / Excel

How to repeat macro for entire sheet

Macro button to switch to previous sheet

Delimit macro on flexible area in Sheet

Trigger a macro on new sheet addition

Change all formulas in a sheet

Work Sheet Change

Automate formatting of the sheet headings to the all the other sheet using macro excel

Highlight cells in a sheet based on corresponding cell values in another sheet with a macro

macro enabled excel sheet asks to save every time as new sheet

How to run a macro in sheet1 from sheet2

Excel macro to find a cell in a sheet and copy the entire row in another sheet

Executing Macro without showing other sheet

Sub Not Defined Error: Calling a Macro in Another Sheet

Move "active" cell in Googles Sheet macro language

Run Macro if mandatory SHeet name exist

Macro for button to today's sheet in excel