Writing a short script in VBA to sort through data, essentially counting the number of names entered into a list, and printing them into a new column. However, the & in the row "Set Staff 1" is causing a compile error due to a mismatch. It seems to be caused by setting two ranges at the same time. I get the impression that there's a straightforward solution...
Sub Staffing() Dim Rng As Range Dim i As Long Dim Staff1 As Range Dim Staff2 As Range Dim Staff3 As Range Dim Staff4 As Range Dim Staff5 As Range Dim Staff6 As Range Dim Staff7 As Range While i <= 300 Set Rng = Range("J" & i) Set Staff1 = ("X" & i) Set Staff2 = ("AD" & i) Set Staff3 = ("AJ" & i) Set Staff4 = ("AP" & i) Set Staff5 = ("BB" & i) Set Staff6 = ("BH" & i) Set Staff7 = ("BN" & i) If Staff1 <> "" Then Rng.FormulaR1C1 = "0" i = i + 1 If Staff2 <> "" Then Rng.FormulaR1C1 = "1" i = i + 1 If Staff3 <> "" Then Rng.FormulaR1C1 = "2" i = i + 1 If Staff4 <> "" Then Rng.FormulaR1C1 = "3" i = i + 1 If Staff5 <> "" Then Rng.FormulaR1C1 = "4" i = i + 1 If Staff6 <> "" Then Rng.FormulaR1C1 = "5" i = i + 1 If Staff7 <> "" Then Rng.FormulaR1C1 = "6" i = i + 1 Else Stop End If Wend End Sub Thanks in advance!
Thanks for everyone's patience. I had mad HUGE errors in writing this first code, but have fixed them thanks to your help and some of my own trial and error. The correct code is as follows. I'm sure now you'll be able to see what I was trying to do!
Sub StaffingNumbers() Dim Rng As Range Dim i As Long Dim Staff1 As Range Dim Staff2 As Range Dim Staff3 As Range Dim Staff4 As Range Dim Staff5 As Range Dim Staff6 As Range Dim Staff7 As Range Dim Staff8 As Range Dim Staff9 As Range Dim Staff10 As Range i = 3 While i <= 300 Set Rng = Range("J" & i) Set Staff1 = Range("X" & i) Set Staff2 = Range("AD" & i) Set Staff3 = Range("AJ" & i) Set Staff4 = Range("AP" & i) Set Staff5 = Range("AV" & i) Set Staff6 = Range("BB" & i) Set Staff7 = Range("BH" & i) Set Staff8 = Range("BN" & i) If Staff1 = "" Then Rng.FormulaR1C1 = "0" i = i + 1 ElseIf Staff1 <> "" And Staff2 = "" Then Rng.FormulaR1C1 = "1" i = i + 1 ElseIf Staff2 <> "" And Staff3 = "" Then Rng.FormulaR1C1 = "2" i = i + 1 ElseIf Staff3 <> "" And Staff4 = "" Then Rng.FormulaR1C1 = "3" i = i + 1 ElseIf Staff4 <> "" And Staff5 = "" Then Rng.FormulaR1C1 = "4" i = i + 1 ElseIf Staff5 <> "" And Staff6 = "" Then Rng.FormulaR1C1 = "5" i = i + 1 ElseIf Staff6 <> "" And Staff7 = "" Then Rng.FormulaR1C1 = "6" i = i + 1 ElseIf Staff7 <> "" And Staff8 = "" Then Rng.FormulaR1C1 = "7" i = i + 1 ElseIf Staff8 <> "" Then Rng.FormulaR1C1 = "8" i = i + 1 Else Stop End If Wend Set Rng = Nothing Set Staff1 = Nothing Set Staff2 = Nothing Set Staff3 = Nothing Set Staff4 = Nothing Set Staff5 = Nothing Set Staff6 = Nothing Set Staff7 = Nothing End Sub
Set Staff1 = Range("X" & i)ElseIfinstead ofIfalfter the firstIf?