Share via

Excel macros - why does a random zero appear in the active cell at the time I run the macro?

Anonymous
2019-03-14T18:46:45+00:00

I recorded a macro that would restore some default values so if someone modifies the cells they can revert back to the original - i.e. update the cells on one worksheet with default values from another (see below for code).

I have then assigned that macro to a button.

When i click the button, the default values are restored BUT it also inserts a random 0 in the cell that is active at the time I press the button.  How can I stop it doing this?

Thanks!

Sub RestoreDefaults_Population()

'

' RestoreDefaults_Population Macro

' Restore default values on Population Inputs worksheet

'

'

    ActiveCell.FormulaR1C1 = "='Defaults CS'!RC[-7]"

    Range("I11").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-1]C[-5]"

    Range("I15").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-4]C[-5]"

    Range("I19").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-7]C[-5]"

    Range("I23").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-10]C[-5]"

    Range("I27").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-13]C[-5]"

    Range("I32").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-17]C[-5]"

    Range("M11").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[6]C[-9]"

    Range("M15").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[3]C[-9]"

    Range("M19").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!RC[-9]"

    Range("M23").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-3]C[-9]"

    Range("M27").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-6]C[-9]"

    Range("M32").Select

    ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-10]C[-9]"

    Range("M33").Select

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2019-03-14T23:38:56+00:00

    Yes, that will work. For example:

    Worksheets("Sheet1").Range("I11").Value = Worksheets("Defaults CS").Range("A1").Value

    But I also note that your macro updates columns I and M, in rows 11,15,19, 23,27 and 32, which apart from the final 32 (which would be 31 if the sequence were followed) all increment by 4. So it might be possible to simplify the code even more.

    Put the default values to go in column I in column A, rows 1-6  of sheet 'Defaults CS'.

    Put the default values to go in column M in column B, rows 1-6  of sheet 'Defaults CS'.

    Sub test()

        Dim i As Long, j As Long

        For i = 1 To 6

            j = 4 * i + 7

            If j = 31 Then j = 32

            Worksheets("Sheet1").Range("I" & j).Value = Worksheets("Defaults CS").Range("A" & i).Value

            Worksheets("Sheet1").Range("M" & j).Value = Worksheets("Defaults CS").Range("B" & i).Value

        Next i

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-03-14T23:07:27+00:00

    Thank you - this is really helpful!

    If I was going to write this out (vs. recording it), would the following formula work?

    Worksheets ("insert worksheet name").Range ("Insert Cell code"). Value = Worksheets ("Insert worksheet name".Range ("Insert cell code to be copied"). Value

    So that others could update this (I won't be the only user) i think it would be easier to write out the code vs. record it but I'm not sure if the code above is the best way to get the cells to be updates with default values from another worksheet?

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2019-03-14T22:52:58+00:00

    If you test this by making your starting cell in a few different columns you will find that it only puts a zero if you start in columns A:G, and if you start in column H it will be fine.

    In the very first statement in your macro, you have not preselected a cell first, and  the statement says take the value from seven columns to the left (RC[-7]). For columns 7 or less (A:G), to prevent an error when it hits the left edge of the sheet,  it does a 'wrap-around' and gives you the value from the extreme right. So if you run it starting from F3 and then look at the resultant formula in that cell you will find it contains ='Defaults CS'!XFC3.

     So the solution is to make sure you select a cell first, before the line that adds the formula.

    Also, and I understand that you recorded this, but it is generally bad form, or at least unnecessary, to 'select' a cell first. So the line pair:

        Range("I11").Select

        ActiveCell.FormulaR1C1 = "='Defaults CS'!R[-1]C[-5]"

    can be reduced to

        Range("I11").FormulaR1C1 = "='Defaults CS'!R[-1]C[-5]"

    and similarly for all the other lines.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments