Hi all
I'm not confident with VBA as yet and have simply been creating macro's and then found the need to edit them, so am gradually exploring VBA and what can be done with it.
I currently have a spreadsheet with column A containing staff names. I then have calculations set up to track their working patterns.
I have created a macro that inserts a column at column f and then updates all of my formulae accordingly for example range (d3:d123).
However, when I add or remove staff this changes the final row reference by one or two digits and I want to know if there is a way that my code re-written to update automatically and amend my range:
here is my code, highlighted are the bits that are variable:Thank you all in advanceCode:Columns("F:F").Select Selection.Insert Shift:=xlToRight Range("G2").Select Selection.AutoFill Destination:=Range("F2:G2"), Type:=xlFillDefault Range("F2:G2").Select Columns("F:F").EntireColumn.AutoFit Range("B4").Select ActiveCell.FormulaR1C1 = "=COUNTA(RC[4]:RC[17])" Range("B4").Select Selection.AutoFill Destination:=Range("B4:B123"), Type:=xlFillDefault Range("B4:B123").Select Range("D4").Select ActiveCell.FormulaR1C1 = "=COUNTA(RC[2]:RC[29])" Range("D4").Select Selection.AutoFill Destination:=Range("D4:D123"), Type:=xlFillDefault Range("D4:D123").Select Range("H2").Select Selection.AutoFill Destination:=Range("G2:H2"), Type:=xlFillDefault Range("G2:H2").Select Columns("G:G").Select Selection.Copy Columns("F:F").Select Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("G124").Select Application.CutCopyMode = False Selection.AutoFill Destination:=Range("F124:G124"), Type:=xlFillDefault Range("F124:G124").Select Range("F4").Select End Sub
ETA a bit of gratitude goes a long way




Reply With Quote