Assigning sequence of numbers to visible cells only using VBA code (avoid hidden rows)

VictoriaExcel

New Member
Joined
Nov 15, 2018
Messages
14
Hello,

I am having difficulty with assigning a specific sequence of numbers (seat numbers listed below) to specific records whilst keeping the order. For context, I have a running order of graduands and those known to be attending are marked as attending in column C as well as the row being highlighted orange by a previous macro conditionally formatting.

I have attempted to record a macro by filtering by colour, selecting only visible cells in column B for the full range of records (Seat No.) and entered a formula linking to the start of the below sequence in a different sheet ='TH Seat Nos.'!A2 but unfortunately because the pattern of those attending/not attending is not consistent the formula pulls in from the below sequence inconsistently. :confused:

Therefore, what I am asking is do you know how to assign the below sequence in sheet ='TH Seat Nos.'!A2 in order within column B to only records where column C is a Y.

I need to keep the existing order of the rows and the not attending records as we need the data to hand for slot ins on the day.

Many thanks in advance,

[TABLE="width: 128"]
<colgroup><col></colgroup><tbody>[TR]
[TD]A2[/TD]
[/TR]
[TR]
[TD]A3[/TD]
[/TR]
[TR]
[TD]A4[/TD]
[/TR]
[TR]
[TD]A5[/TD]
[/TR]
[TR]
[TD]A6[/TD]
[/TR]
[TR]
[TD]A7[/TD]
[/TR]
[TR]
[TD]A8[/TD]
[/TR]
[TR]
[TD]A9[/TD]
[/TR]
[TR]
[TD]A10[/TD]
[/TR]
[TR]
[TD]A11[/TD]
[/TR]
[TR]
[TD]A12[/TD]
[/TR]
[TR]
[TD]A13[/TD]
[/TR]
[TR]
[TD]A14[/TD]
[/TR]
[TR]
[TD]B2[/TD]
[/TR]
[TR]
[TD]B3[/TD]
[/TR]
[TR]
[TD]B4[/TD]
[/TR]
[TR]
[TD]B5[/TD]
[/TR]
[TR]
[TD]B6[/TD]
[/TR]
[TR]
[TD]B7[/TD]
[/TR]
[TR]
[TD]B8[/TD]
[/TR]
[TR]
[TD]B9[/TD]
[/TR]
[TR]
[TD]B10[/TD]
[/TR]
[TR]
[TD]B11[/TD]
[/TR]
[TR]
[TD]B12[/TD]
[/TR]
[TR]
[TD]B13[/TD]
[/TR]
[TR]
[TD]B14[/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R]
[TD]W7[/TD]
[/TR]
[TR]
[TD]W8[/TD]
[/TR]
[TR]
[TD]W9[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello Victoria,

Out of your 295 seats listed ... is it on purpose ... some seats seem left out : A15,G15,H15, etc ...
 
Upvote 0
Hi
List your seat numbers in col Z (295 seats). paste the following codes in the macro window.
Code:
Sub victoria()
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
a = Cells(Rows.Count, 3).End(xlUp).Row
c = Cells(Rows.Count, 26).End(xlUp).Row
b = Application.WorksheetFunction.CountIf("C2:C" & a, "Y")
e = 1
        If b > c Then
        MsgBox "there are more students than the seats listed"
        Else
        End If
    For d = 2 To a
        If Cells(d, 3) = "Y" Then
        Cells(d, 25) = Cells(e, 26)
        e = e + 1
        End If
    Next d
MsgBox "complete"
End Sub
run the macro. it will assign seat numbers in col Y if there is a Y in col C. try it on a copy of your excel file .
Ravishankar
 
Upvote 0
Hello,

Ideally your numbering process should be included within your existing macro which performs the filtering by color ....

HTH
 
Upvote 0
Hi,

Yes the seating sequence needs to be as described as those missing seats/spaces need to be reserved for various reasons and thus not allocated to attending students.

Thank you for the code! Unfortunately it isn't working yet. I received a compile error message stating type mismatch which I have highlighted in blue below:

Sub victoria()
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
a = Cells(Rows.Count, 3).End(xlUp).Row
c = Cells(Rows.Count, 26).End(xlUp).Row
b = Application.WorksheetFunction.CountIf("C2:C" & a, "Y")
e = 1
If b > c Then
MsgBox "there are more students than the seats listed"
Else
End If
For d = 2 To a
If Cells(d, 3) = "Y" Then
Cells(d, 25) = Cells(e, 26)
e = e + 1
End If
Next d
MsgBox "complete"
End Sub
If this could work alongside the macro that highlights the row that would be great. At the moment I have recorded conditional formatting to cover this task.

Many thanks,

Victoria
 
Upvote 0
Thank you for the code!

Unfortunately it isn't working just yet. I received a compile error message stating type mismatch on the &

b = Application.WorksheetFunction.CountIf("C2:C" & a, "Y")
 
Upvote 0
Hello,

Could you post the macro you are already using to filter your sheet ... :wink:
 
Upvote 0
Hello,

The filtering method didn't work unfortunately and was a recorded macro, therefore I have removed the filters on the page.

I need to assign a Seat No in column B according to the existing sequence of seat numbers* if column C is a "Y" for attending. Rows not marked as Y for attending need to not interrupt the sequence.

*The seating sequence is in another sheet but can be moved to wherever necessary for the purpose of the macro.

I hope that makes more sense. :)

Many thanks in advance,

Victoria
 
Upvote 0
Hello,

Say you have stored your list of 295 seats in the range Z2:Z296 ...

You could use the following formula :

Code:
=IF(C2="","",INDEX($Z$2:$Z$296,COUNTIF($C$2:C2,"Y")))

Hope this will help
 
Upvote 0
Thank you very much for the Index suggestion it works perfectly :) I'd tried the count if alone and it didn't work so I then over complicated!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top