VBA create a List from selected inputs on a seperate worksheet

Gurnek

New Member
Joined
Apr 13, 2015
Messages
16
All,

I am trying to create a macro which creates a list on a seperate worksheet.

The data i have is arranged in different cells in "Sheet 1" but predominantly looks like:

Room
No.
Lecture Rooms
12
Seminar Rooms
3
Dining Hall
2
Sports Hall
2
Activity Rooms
4

<tbody>
</tbody>


What i am hoping to achieve is a list in the following format but in a different spreadsheet:

Lecture Rooms
1
Lecture Rooms
2
Lecture Rooms
3
Lecture Rooms
4
Lecture Rooms
5
Lecture Rooms
6
Lecture Rooms
7
Lecture Rooms
8
Lecture Rooms
9
Lecture Rooms
10
Lecture Rooms
11
Lecture Rooms
12
Seminar Rooms
1
Seminar Rooms
2
Seminar Rooms
3
Dining Hall
1
Dining Hall
2
Sports Hall
1
Sports Hall
2
Activity Rooms
1
Activity Rooms
2
Activity Rooms
3
Activity Rooms
4

<tbody>
</tbody>

The list can be generated at any point so i think the start of the macro needs to reset the list produced.


Any help is appreciated.

Regards,


Gurnek.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
something along the lines of

Dim c As Range, rng
Dim count_val As Integer
Dim room As String

Dim lastrow As Long
Set rng = Range("a2:a6")
For Each c In rng
room = c.Value

count_val = c.Offset(0, 1).Value
Sheets("sheet2").Activate

lastrow = Cells(Rows.count, "a").End(xlUp).Row + 1
Do Until count_val = 0
Range("a" & lastrow).Value = room
count_val = count_val - 1
lastrow = Cells(Rows.count, "a").End(xlUp).Row + 1
Loop
Sheets("sheet1").Activate
Next c
 
Upvote 0
That is excellent.

I introduced a Sheet2.Cells.Clear to clear the sheet before the macro starts as it seemed to keep adding to the list.

The macro does not seem to give the room number in column B though ...

Any thoughts?
 
Upvote 0
Try this VBA
Code:
Option Explicit


Sub Rooms()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    Dim lr1 As Long, lr2 As Long
    lr1 = sh1.Range("B" & Rows.Count).End(xlUp).Row
    lr2 = sh2.Range("B" & Rows.Count).End(xlUp).Row
    Dim i As Long, x As Long
    Dim rn As Long, sn As Long
    
        
    sh2.Range("A1:B" & lr2).ClearContents
    sh2.Range("A1") = "Rooms"
    sh2.Range("B1") = "No."
    
    Application.ScreenUpdating = False
    For i = 2 To lr1
    lr2 = sh2.Range("B" & Rows.Count).End(xlUp).Row
    rn = sh1.Range("B" & i)
    sn = lr2 + 1
    sh1.Range("A" & i).Copy sh2.Range("A" & lr2 + 1)
    
    x = 1
    sh2.Range("B" & sn).Select
    Do Until ActiveCell.Row = rn + sn
        If ActiveCell.EntireRow.Hidden = False Then
            ActiveCell.Value = x
            x = x + 1
        End If
        ActiveCell.Offset(1).Select
    Loop
    Next i
    
    lr2 = sh2.Range("B" & Rows.Count).End(xlUp).Row
    For i = 3 To lr2
    If sh2.Range("A" & i) = "" Then sh2.Range("A" & i) = sh2.Range("A" & i - 1)
    Next i
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Dim c As Range, rng
Dim count_val As Integer
Dim room As String
Dim room_no As Integer

Dim lastrow As Long
Set rng = Range("a2:a6")
Range("sheet2!a2:b1000").ClearContents
For Each c In rng
room = c.Value
room_no = 1

count_val = c.Offset(0, 1).Value
Sheets("sheet2").Activate

lastrow = Cells(Rows.Count, "a").End(xlUp).Row + 1
Do Until count_val = 0
Range("a" & lastrow).Value = room
Range("b" & lastrow).Value = room_no
room_no = room_no + 1

count_val = count_val - 1
lastrow = Cells(Rows.Count, "a").End(xlUp).Row + 1
Loop
Sheets("sheet1").Activate
Next c
 
Upvote 0
Steve,

This is Perfect!!!!

THere is one slight tweek, the code does not like the: Range("sheet2!a2:b1000").ClearContents

Is there an alternative way to clear sheet and select a cell? If i delete this line, it returns the list exactly how i want it but directly below the existing list in sheet 1.

Regards,

Gurnek
 
Upvote 0
Run-time error 1004

Application-defined or object-defined error.

When i remove this line, it populated the list exactly how i need it on the first sheet under the existing list.

It is almost as thought the sheet 2 activate command isnt recognised neither.
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,733
Members
449,465
Latest member
TAKLAM

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