Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Fetch specific info from tab to another

0

Hi, the title is probably a bit misguiding. I'm completely lost here so I don't even have a formula or code. It should be fairly easy, but I don't know where to start. 

I have 150 employees and own 3 houses they can rent rooms untill they find some for themselves (I employ a lot from other countries). It's a steady flow so I need to have a system thats always keep my list updated.

So in my sheet I have the 'Employees' and the 'Rooms' tabs. 

The houses are called by the addresses. 3 houses in the 'Rooms' tab. 

I'm trying to make the sheet fetch the names of the employees from 'Employees' tab and put them in an open room in the 'Rooms' tab. I'm sure I can make a simple IF or @INDEX formula, but that won't work since I have several rooms in the different houses. 

I'm lost, does anyone have any pointers/tips on how I can make this work? Cheers

Answer
Discuss

Discussion

Hi LordBrenden. You have the "luxury" of two working solutions to your problem. Both rely in VBA but the work has been done for you. 
John_Ru (rep: 6152) Oct 2, '23 at 4:24 pm
Add to Discussion

Answers

0
Selected Answer

Hi again LordBrenden

In the attached revised file, I've set up VBA code to manage (allocate, release) rooms and add new addresses via a "graphical" UserForm.

Firstly, I've converted your data to tables (e.g. add a new row /employee, just type in the column A of the row immediately below on worksheet "Employees").

In worksheet "Rooms", I've added columns I and J for Postal Code and Place respectively (so they can be copied to "Employees") and changed an address (in yellow). Up to 5 properties are allowed and you can change the headings and addresses to suit your native language.

If on worksheet "Employees" you click in column D (for new or existing employee), this event code works:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    ' do nothing unless a single cell in column D is changed...
    If Target.CountLarge > 1 Or Intersect(Target, Columns(4)) Is Nothing Then Exit Sub
    ' and it's not D1
    If Not Intersect(Target, Cells(1, 4)) Is Nothing Then Exit Sub

    ' launch form
    Call ShowForm

End Sub

which calls a module code:

Public AdCell As String

Sub ShowForm()
' capture address (for form)
AdCell = ActiveCell.Address
Userform1.Show
End Sub

where the declaration in bold allows the address of the cell to be passed to the code which sets up the form:

Private Sub UserForm_Initialize()

    ' add employee name and address to top of form
    With Sheet1.Range(AdCell)
        Me.Caption = Me.Caption & .Offset(0, -3).Text & " " & .Offset(0, -2).Text & _
        " (currently " & .Offset(0, -1).Text & " " & .Text & " " & ", " & .Offset(0, 2).Text & ")"
    End With

    ' populate vacancies
    With Sheet2.Range("A2")
     ' loop down property rows
     For n = 0 To 4
        ' show property address (if any)
        If .Offset(n, 0) <> "" Then Me.Controls("LB_Prop" & n + 1).Caption = .Offset(n, 0).Value
         ' loop along room columns
        For p = 1 To 7

            If .Offset(n, 0) <> "" Then
                ' if there's an address...
                ' grab cell value
                Occup = .Offset(n, p).Value
                Select Case .Offset(n, p)

                        Case "N/A"
                            ' if there's no room, don't show
                            With Me.Controls("OB_Prop" & n + 1 & "Room" & p)
                                .Caption = ""
                                .Enabled = False
                                .Visible = False
                            End With

                        Case Is <> ""
                            ' if there's a value, show occupant
                            With Me.Controls("OB_Prop" & n + 1 & "Room" & p)
                                .Caption = Occup
                                .Enabled = False
                                .Visible = True
                            End With
                    End Select

                   Else
                   'if no address, hide property and rooms
                   Me.Controls("LB_Prop" & n + 1).Visible = False 'LB_Prop
                   Me.Controls("OB_Prop" & n + 1 & "Room" & p).Visible = False

            End If
        Next p

     Next n
    End With
End Sub

This populates the form to show whick properties exist and which rooms are occupied or vacant (a blank cell in "Rooms" will show as vacant).

The header of the form reminds you which employee/ location is being changed (with their current address), You can write any new address in the three textboxes near the top. If you don't, you can pick one of the vacant rooms. 

If you click cancel, the form goes and nothing happens.

Click Submit and this code checks to see if that employee was in a company rented room and if so sets it to "" (=vacant). It then writes the new address to "Employees". If you didn't add text in those upper boxes, it determines which room was selected, writes the employee name to "Rooms" and adds data to "Employees":

Private Sub CB_Submit_Click()

    With Sheet1.Range(AdCell)
        'first check if previous address was company rented. Loop down...
        For n = 2 To 6
            If .Value = Sheet2.Cells(n, 1).Value Then
                If .Value <> "" Then
                    ' if matches company address, loop along
                    For p = 2 To 8
                        If .Offset(0, -1).Value = Sheet2.Cells(1, p) Then
                            ' if room matches, clear employee room and cell (now vacant)
                            Sheet2.Cells(n, p).Value = ""
                            .Offset(0, -1).Value = ""
                            Exit For
                        End If
                    Next p
                End If
           End If
        Next n

        ' if new address added, write to sheet
        If TB_NewAdd <> "" Then
            .Value = TB_NewAdd.Text
            .Offset(0, 1).Value = TB_Postal.Text
            .Offset(0, 2).Value = TB_Place.Text

            Else
            ' if room chosen
            For Each OB In Me.Controls
                If Left(OB.Name, 3) = "OB_" Then
                    If OB.Value = True Then
                        ' write name to Rooms
                        Sheet2.Cells(Mid(OB.Name, 8, 1) + 1, Right(OB.Name, 1) + 1) = .Offset(0, -3).Value & " " & .Offset(0, -2).Value
                        ' write adress etc. to Employees
                        .Value = Sheet2.Cells(Mid(OB.Name, 8, 1) + 1, 1).Value
                        .Offset(0, -1).Value = Sheet2.Cells(1, Right(OB.Name, 1) + 1)
                        .Offset(0, 1).Value = Sheet2.Cells(Mid(OB.Name, 8, 1) + 1, 9).Value
                        .Offset(0, 2).Value = Sheet2.Cells(Mid(OB.Name, 8, 1) + 1, 10).Value
                        Exit For
                    End If
                End If
            Next OB
        End If
    End With
    'close form
    Unload Me
End Sub

I suggest you try it (with the imaginary Nordby address) then correct "Rooms" (clearing names from all rooms) and reallocate them to real employees in "Employees". 

Hopefully it all works well for you. If so and your prefer it to other answer(s), please be sure to mark this Answer as Selected.

Discuss

Discussion

This is exactly what is was looking for, just even better. Less manual work means smaller chances of making errors. Thanks! 
LordBrenden (rep: 10) Oct 3, '23 at 2:55 am
One last thing. I'm having trouble implementing this into my actual sheet. It is in Norwegian so some of the words are different, I just translated it to english so you would understand. I still use English coding though, so it should be the same. Can I upload the sheet I'm using at work so you can help me implement it? 
LordBrenden (rep: 10) Oct 3, '23 at 3:13 am
Glad you like the approach. Thanks for selecting my Answer.

I'll look at your Norwegian implementation later today  if you revise your original  question to upload your file as a SECOND file. I'll need to know what you will use instead of "N/A" (=not applicable) for non-existent rooms on worksheet Rooms plus the header on the UserForm.

Please don't forget to thank Willie for his effort.
John_Ru (rep: 6152) Oct 3, '23 at 3:36 am
I said above I'd adjust your own file "later today" but you haven't responded yet to post it. I'll be busy from 5pm (UK time) today and most of tomorrow so it might be Thursday before I do so.
John_Ru (rep: 6152) Oct 3, '23 at 7:57 am
Did you see my posts above / did you get an email alert? (Sometimes they're not sent for unknown reasons.) 
John_Ru (rep: 6152) Oct 4, '23 at 12:45 pm
Add to Discussion
0

Hello LordBrenden and welcome to the forum,

I took your file and made a couple of modifications as well as writting code to add an employee to the "Rooms" sheet.

On the "Employees" sheet I added a simple table with formulas and conditional formatting to show the staus of the rooms (Taken or Avail). I added a button to the "Employees" sheet with a macro to add the selected employee to the selected house and room via a userform.

Here is the code to initialize the userform:

Private Sub UserForm_Initialize()

' written by WillieD24 - Sept 29/2023 for teachexcel.com

' this code will populate the itmes to the listboses

' populate ListBox1  --  edit as needed
With ListBox1
    .AddItem "Chapman Lane 1"
    .AddItem "Dalton Street 1"
    .AddItem "Street noname"
End With
' populate ListBox2  --  edit as needed
With ListBox2
        .AddItem "Room 1"
        .AddItem "Room 2"
        .AddItem "Room 3"
        .AddItem "Room 4"
        .AddItem "Room 5"
        .AddItem "Room 6"
        .AddItem "Room 7"
End With

End Sub

This is the code to add the employee to the "Rooms" sheet:

Private Sub CommandButton1_Click()     ' "Book Room" button

' written by WillieD24 - Sept 29/2023 for teachexcel.com

' this code will run when the "Book Room" button is cliked

Dim House As String, Room As String
Dim H_Row As Long, R_Column As Long
Dim Employee As String

House = ListBox1.Text
'MsgBox House
Room = ListBox2.Text
'MsgBox Room

' determine house row number
If House = "Chapman Lane 1" Then H_Row = 2
If House = "Dalton Street 1" Then H_Row = 4
If House = "Street noname" Then H_Row = 6
' detremine room column number
If Room = "Room 1" Then R_Column = 2
If Room = "Room 2" Then R_Column = 3
If Room = "Room 3" Then R_Column = 4
If Room = "Room 4" Then R_Column = 5
If Room = "Room 5" Then R_Column = 6
If Room = "Room 6" Then R_Column = 7
If Room = "Room 7" Then R_Column = 8

' no house has been selected
If House = "" Then
    MsgBox "Please select a house"
    Exit Sub
End If
' noroom has been selected
If Room = "" Then
    MsgBox "Please select a room"
    Exit Sub
End If

If House <> "Chapman Lane 1" And Room = "Room 7" Then
    MsgBox "That room does not exist in the chosen house."
    Exit Sub
End If
If ActiveCell.Column = 1 Then
    Employee = ActiveCell.Value & " " & ActiveCell.Offset(, 1).Value
End If
' enter employee's name to room on "Rooms" sheet
Sheets("Rooms").Cells(H_Row, R_Column) = Employee

Dim i As Long

' deselect the house in ListBox1
For i = 0 To Me.ListBox1.ListCount
    Me.ListBox1.Selected(i) = False
Next i
' deselect the room in ListBox2
For i = 0 To Me.ListBox2.ListCount
    Me.ListBox2.Selected(i) = False
Next i

End Sub

Here is the code for the "CANCEL" button:

Private Sub CommandButton2_Click()     ' "CANCEL" button

' written by WillieD24 - Sept 29/2023 for teachexcel.com

ListBox1.Clear
ListBox2.Clear

Unload Me     ' close UserForm1

Sheets("Employees").Range("G1").Select

End Sub

If this solves your problem please mark the answer as selected.

Cheers   :-)

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login