I'm doing a Data Entry Table with a From using VBA. The code works fine, but it adds the entry to the first row after the table. For example, if the table's last row is row 20, then it will add the entry in row 21 outside of the table. Is there a way to assign the data to be added in the last row of the table?
This is the code I'm using to add the data to the table:
Private Sub buttonSave_Click()
If MsgBox("Deseas incluir este trabajo?", vbYesNo, "Guardar repuesto") = vbYes Then
'write the data to the worksheet from controls
Call WriteDataToSheet
'empty textboxes
Call EmptyTextBoxes
End If
End Sub
Private Sub WriteDataToSheet()
Dim newRow As Long
With Sheet1
newRow = .Cells(.Rows.Count, 2).End(xlUp).row + 1
.Cells(newRow, 1).Value = textboxNewDate.Value
.Cells(newRow, 2).Value = textboxNameNew.Value
.Cells(newRow, 3).Value = textboxNewEquipment.Value
.Cells(newRow, 4).Value = textboxNewTech.Value
.Cells(newRow, 5).Value = textboxNewCot.Value
.Cells(newRow, 6).Value = textboxNewPrice.Value
.Cells(newRow, 7).Value = textboxNewFac.Value
.Cells(newRow, 8).Value = textboxNewStatusF.Value
.Cells(newRow, 9).Value = textboxNewServ.Value
.Cells(newRow, 10).Value = textboxNewEnt.Value
.Cells(newRow, 11).Value = textboxNewDesc.Value
.Cells(newRow, 12).Value = textboxNewStatus.Value
End With
End Sub
This is the code I'm using to extract the range from the table. This is in a separate module.
Public Function GetType() As Variant
GetType = Sheet1.ListObjects("tbTabla2").DataBodyRange.Value
End Function
Is there a way I can assign the data to be added after the last row inside of "Tabla2".
This is the table for reference:
To keep your data inside the table, I suppose you would want to use the ListObject.ListRows.Add() method to add a new row. You could do something like this:
Private Sub WriteDataToTable()
Dim newTableRow As Range
newTableRow = Sheet1.ListObjects("Table2").ListRows.Add().Range
WriteDataToRow newTableRow
End Sub
Private Sub WriteDataToRow(row As Range)
Dim newRowNumber As Long
newRowNumber row.row
With Sheet1
.Cells(newRow, 1).Value = textboxNewDate.Value
.Cells(newRow, 2).Value = textboxNameNew.Value
.Cells(newRow, 3).Value = textboxNewEquipment.Value
.Cells(newRow, 4).Value = textboxNewTech.Value
.Cells(newRow, 5).Value = textboxNewCot.Value
.Cells(newRow, 6).Value = textboxNewPrice.Value
.Cells(newRow, 7).Value = textboxNewFac.Value
.Cells(newRow, 8).Value = textboxNewStatusF.Value
.Cells(newRow, 9).Value = textboxNewServ.Value
.Cells(newRow, 10).Value = textboxNewEnt.Value
.Cells(newRow, 11).Value = textboxNewDesc.Value
.Cells(newRow, 12).Value = textboxNewStatus.Value
End With
End Sub
Now, since you are using newRow = .Cells(.Rows.Count, 2).End(xlUp).row + 1 to find a last-used-row, that implies to me that you could be looking at a case were there is room in your table and you don't want to add a new row. In that case, you'll want to do some sort of conditional test to check if adding the new row is needed. Depending on how things are managed, I think its easier to just ensure the table always ends with the last used row so that you will ALWAYS be adding a row to the table when you want to add data.
Related
I am New to VBA, so my codes are usually very slow/suboptimized.
In one of my programs I have cells in the sheet that has to be filled when the user press a button, the renges change depending on the button but the concept is the same.
So I did this monstrosity:
Cells((Range("namedrange").Row + 5), 1).Value = ThisWorkbook.Sheets(5).Cells(4, 7).Value
Cells((Range("namedrange").Row + 5), 3).Value = ThisWorkbook.Sheets(5).Cells(4, 8).Value
Cells((Range("namedrange").Row + 5), 5).Value = ThisWorkbook.Sheets(5).Cells(4, 9).Value
Cells((Range("namedrange").Row + 5), 8).Value = ThisWorkbook.Sheets(5).Cells(4, 10).Value
Cells((Range("namedrange").Row + 5) + 1, 1).Value = ThisWorkbook.Sheets(5).Cells(5, 7).Value
Cells((Range("namedrange").Row + 5) + 1, 3).Value = ThisWorkbook.Sheets(5).Cells(5, 8).Value
Cells((Range("namedrange").Row + 5) + 1, 5).Value = ThisWorkbook.Sheets(5).Cells(5, 9).Value
Cells((Range("namedrange").Row + 5) + 1, 8).Value = ThisWorkbook.Sheets(5).Cells(5, 10).Value
but later changed to:
With Range("namedrange")
.Offset(5).Columns(1).Value = ThisWorkbook.Sheets(3).Cells(4, 7).Value
.Offset(5).Columns(3).Value = ThisWorkbook.Sheets(3).Cells(4, 8).Value
.Offset(5).Columns(5).Value = ThisWorkbook.Sheets(3).Cells(4, 9).Value
.Offset(5).Columns(8).Value = ThisWorkbook.Sheets(3).Cells(4, 10).Value
.Offset(6).Columns(1).Value = ThisWorkbook.Sheets(3).Cells(5, 7).Value
.Offset(6).Columns(3).Value = ThisWorkbook.Sheets(3).Cells(5, 8).Value
.Offset(6).Columns(5).Value = ThisWorkbook.Sheets(3).Cells(5, 9).Value
.Offset(6).Columns(8).Value = ThisWorkbook.Sheets(3).Cells(5, 10).Value
End With
which is a bit faster, however I feel that it is still suboptimized. And I would like to know if there is a way to make it cleaner/more elegant.
Just to be noted that there are discontinuities in the columns, e.g. it starts in the 1st columns but jumps to the 3rd and then to the 5th and at last to the 8th.
The code works but it is slow, I just want a way to make it faster/cleaner.
Using Variables
In regards to efficiency, that's about it: you're using the most efficient way to copy values from one cell to another aka copying by assignment.
If you want it to be more flexible, maintainable, and readable(?), here are some ideas.
Additionally, you can move the remaining magic numbers and text to constants at the beginning of the code or even use the constants as arguments.
Sub CopyValues()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Specify the worksheet if you know it.
'Dim dnrg As Range: Set dnrg = wb.Sheets("Sheet1").Range("NamedRange")
' Otherwise, make sure the workbook is active.
If Not wb Is ActiveWorkbook Then wb.Activate
Dim dnrg As Range: Set dnrg = Range("NamedRange")
Dim drg As Range: Set drg = dnrg.Range("A1,C1,E1,H1").Offset(5)
Dim cCount As Long: cCount = drg.Cells.Count
' If you know the tab name, use it instead of the index (3).
Dim sws As Worksheet: Set sws = wb.Sheets(3)
Dim srg As Range: Set srg = sws.Range("G4").Resize(, cCount)
Dim r As Long, c As Long
For r = 0 To 1
For c = 1 To cCount
drg.Offset(r).Cells(c).Value = srg.Offset(r).Cells(c).Value
Next c
Next r
End Sub
Accessing Excel for values from VBA is a slow operation and this adds up when you make multiple requests. When you are essentially retrieving the same information on a repetitive basis there are two two methods can be used to reduce access times.
Replace a lookup with a calculated value
Use a with statement
Thus you code could be written as
Dim myCol as long
myCol =Range("namedrange").Row + 5
With ThisWorkook.Sheets(5)
Cells(myCol, 1).Value = .Cells(4, 7).Value
Cells(myCol, 3).Value = .Cells(4, 8).Value
Cells(myCol, 5).Value = .Cells(4, 9).Value
Cells(myCol, 8).Value = .Cells(4, 10).Value
myCol=myCol+1 ' trivial example
Cells(mycol, 1).Value = .Cells(5, 7).Value
Cells(myCol, 3).Value = .Cells(5, 8).Value
Cells(myCol, 5).Value = .Cells(5, 9).Value
Cells(myCol, 8).Value = .Cells(5, 10).Value
End with
Please also install the free, opensource, and fantastic Rubberduck addin for VBA. The code inspections will help you write VBA that is much more correct.
I wonder what would happen if you use all the .Offset parameters, row and column. Example:
With Range("namedrange")
.Offset(5, 0).Value = ThisWorkbook.Sheets(3).Cells(4, 7).Value
.Offset(5, 2).Value = ThisWorkbook.Sheets(3).Cells(4, 8).Value
.Offset(5, 4).Value = ThisWorkbook.Sheets(3).Cells(4, 9).Value
.Offset(5, 7).Value = ThisWorkbook.Sheets(3).Cells(4, 10).Value
.Offset(6, 0).Value = ThisWorkbook.Sheets(3).Cells(5, 7).Value
.Offset(6, 2).Value = ThisWorkbook.Sheets(3).Cells(5, 8).Value
.Offset(6, 4).Value = ThisWorkbook.Sheets(3).Cells(5, 9).Value
.Offset(6, 7).Value = ThisWorkbook.Sheets(3).Cells(5, 10).Value
End With
You can try to disable screenupdating during execution.
Disable ScreenUpdating
To disable ScreenUpdating, at the beginning of your code put this line:
Application.ScreenUpdating = False
Enable ScreenUpdating
To re-enable ScreenUpdating, at the end of your code put this line:
Application.ScreenUpdating = True
I want to add new data to a table with a form. I have it adding data at the bottom of the sheet.
I want the new info at the top.
With my code, it sends the data to two sheets, the "home" sheet and the sheet that is selected in the first combo box.
Private Sub CommandButton1_Click()
TargetSheet = ComboBox1.Value
If TargetSheet = "" Then
Exit Sub
End If
Worksheets(TargetSheet).Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox8.Value
Worksheets("Home").Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = ComboBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 9).Value = TextBox8.Value
ActiveSheet.Cells(lastrow + 1, 10).Value = Date
ActiveSheet.Cells(lastrow + 1, 11).Value = TimeValue(Now)
ActiveSheet.Cells(lastrow + 1, 12).Value = TextBox9.Value
MsgBox ("Item Added Successfully.")
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
Worksheets("Home").Activate
Worksheets("Home").Cells(1, 1).Select
End Sub
How do I put the new data in the second row since I have headers on the sheet?
There are a lot of stuff to improve your my code, but I want to keep it simple
Some things to begin:
Use option explicit so you don't have unexpected behavior with undefined variables
Always indent your code (see www.rubberduckvba.com a free tool that helps you with that)
Try to separate your logic defining variables and the reusing them
Name your forms' controls
Check a great article about UserForms (when you feel you're ready to advance)
Check the code's comments, and adapt it to fit your needs
EDIT: No need for the EntireRow qualifier as we are already selecting the whole row, and added the copy format from below
Code:
Private Sub CommandButton1_Click()
' Define object variables
Dim targetSheet As Worksheet
Dim homeSheet As Worksheet
Dim targetSheetName As String
Dim homeSheetName As String
Dim targetSheetTopRow As Long
Dim homeSheetTopRow As Long
Dim textBox1Value As Variant
Dim textBox2Value As Variant
Dim textBox3Value As Variant
Dim textBox4Value As Variant
Dim textBox5Value As Variant
Dim textBox6Value As Variant
Dim textBox7Value As Variant
Dim textBox8Value As Variant
Dim textBox9Value As Variant
' Define parameters
targetSheetTopRow = 2
homeSheetTopRow = 2
homeSheetName = "Home"
' Validate if combobox has any value
If Me.ComboBox1.Value = vbNullString Then Exit Sub
' Get target sheet name
targetSheetName = Me.ComboBox1.Value
' Add a reference to sheets
Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)
Set homeSheet = ThisWorkbook.Worksheets(homeSheetName)
' Store current controls values
textBox1Value = Me.TextBox1.Value
textBox2Value = Me.TextBox2.Value
textBox3Value = Me.TextBox3.Value
textBox4Value = Me.TextBox4.Value
textBox5Value = Me.TextBox5.Value
textBox6Value = Me.TextBox6.Value
textBox7Value = Me.TextBox7.Value
textBox8Value = Me.TextBox8.Value
' No need to activate stuff
With targetSheet
' Insert a row after row 2
.Range(targetSheetTopRow & ":" & targetSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(targetSheetTopRow, 1).Value = textBox1Value
.Cells(targetSheetTopRow, 2).Value = textBox2Value
.Cells(targetSheetTopRow, 3).Value = textBox3Value
.Cells(targetSheetTopRow, 4).Value = textBox4Value
.Cells(targetSheetTopRow, 5).Value = textBox5Value
.Cells(targetSheetTopRow, 6).Value = textBox6Value
.Cells(targetSheetTopRow, 7).Value = textBox7Value
.Cells(targetSheetTopRow, 8).Value = textBox8Value
End With
With homeSheet
' Insert a row after row 2
.Range(homeSheetTopRow & ":" & homeSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(homeSheetTopRow, 1).Value = textBox1Value
.Cells(homeSheetTopRow, 2).Value = textBox2Value
.Cells(homeSheetTopRow, 3).Value = textBox3Value
.Cells(homeSheetTopRow, 4).Value = textBox4Value
.Cells(homeSheetTopRow, 5).Value = textBox5Value
.Cells(homeSheetTopRow, 6).Value = textBox6Value
.Cells(homeSheetTopRow, 7).Value = textBox7Value
.Cells(homeSheetTopRow, 8).Value = textBox8Value
.Cells(homeSheetTopRow, 9).Value = Date
.Cells(homeSheetTopRow, 10).Value = TimeValue(Now)
.Cells(homeSheetTopRow, 11).Value = textBox9Value
End With
' Clear control's values
Me.TextBox1.Value = vbNullString
Me.TextBox2.Value = vbNullString
Me.TextBox3.Value = vbNullString
Me.TextBox4.Value = vbNullString
' Alert user
MsgBox ("Item Added Successfully.")
' Goto...
homeSheet.Activate
homeSheet.Cells(1, 1).Select
End Sub
Let me know if it works or you need more help
Trying to get text boxes to populate when a name is selected from my combo box (cboCo). Getting the error method range of object'_worksheet' failed on fourth line of code.
I am not a strong programmer and VB has always been a struggle for me. I am working on creating a form for a vendor spreadsheet that will allow users to view data in the spreadsheet, edit vendor data, add new data and delete data as needed. I so far have got it to add data and show the company names in the combo box. What I am working on currently is getting the text boxes to populate with the data in the row for the company selected in the combobox.
Private Sub cboCo_Change()
Dim iRow As Long, LastRow As Long, ws As Worksheet
Set ws = Worksheets("VendorInfo")
LastRow = ws.Range(1 & Rows.Count).End(x1Up).Row
For iRow = 2 To LastRow
If (Me.cboCo.Value) = ws.Cells(iRow, 1) Then
Me.cboCat = ws.Cells(iRow, 19).Value
Me.cboCat = ws.Cells(iRow, 19).Value
Me.cboYrApprv = ws.Cells(iRow, 14).Value
Me.txtContact = ws.Cells(iRow, 2).Value
Me.txtPhone = ws.Cells(iRow, 3).Value
Me.txtEmail = ws.Cells(iRow, 4).Value
Me.txtCoAdd = ws.Cells(iRow, 5).Value
Me.txtWebSite = ws.Cells(iRow, 6).Value
Me.txtAccred = ws.Cells(iRow, 8).Value
Me.txtStanding = ws.Cells(iRow, 9).Value
Me.txtSince = ws.Cells(iRow, 10).Value
Me.txtNotes = ws.Cells(iRow, 11).Value
Me.txtVerified = ws.Cells(iRow, 12).Value
Me.txtToday = ws.Cells(iRow, 13).Value
Me.txtServProd = ws.Cells(iRow, 7).Value
Me.txtApprvBy = ws.Cells(iRow, 15).Value
Me.txtAprvReas = ws.Cells(iRow, 16).Value
Me.txtOrder = ws.Cells(iRow, 17).Value
Me.txtPurchs = ws.Cells(iRow, 18).Value
End If
Next iRow
End Sub
When I select a company from my cboCo combo box I get the method range of object'_worksheet' failed error on the fourth Line (starting with LastRow just above my loop)
You are calling Range() incorrectly. Try this:
LastRow = ws.Cells(1, Rows.Count).End(xlUp).Row
or
LastRow = ws.Range("A" & rows.count).End(xlUp).Row
Originally, it was resolving to basically ws.Range(1 & 1048576)... --> ws.Range(11048576).End(xlUp).Row, which A) isn't a proper Range and B) is a non-existent row number.
Also, note it's xlUp, not x1Up (it's an "L", not a "1")
I am using the following code to enter data from Userform to Excel sheet and works fine.
The problem is that it overwrites the same row of data. But if I change:
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value to contain a 1 --> .Cells(RowCount, 1).Value = Me.DepSectDrop.Value, and likewise for the rest (2 fore SiteFacOpen, 3 for CaseStartOpen, etc), it does not overwrite.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim RowCount As Long
Dim ws As Worksheet
Set ws = Worksheets("TRACK")
RowCount = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value
.Cells(RowCount, 5).Value = Me.SiteFacOpen.Value
.Cells(RowCount, 6).Value = Me.CaseStartOpen.Value
.Cells(RowCount, 7).Value = Me.TypeDrop.Value
.Cells(RowCount, 8).Value = Me.ProcessDrop.Value
.Cells(RowCount, 9).Value = Me.CompNameOpen.Value
.Cells(RowCount, 10).Value = Me.CompEIDOpen.Value
.Cells(RowCount, 11).Value = Me.RespNameOpen.Value
.Cells(RowCount, 12).Value = Me.RespEIDOpen.Value
.Cells(RowCount, 13).Value = Me.DescOpen.Value
End With
'Clear input controls.
Me.DepSectDrop.Value = ""
Me.SiteFacOpen.Value = ""
Me.CaseStartOpen.Value = ""
Me.TypeDrop.Value = ""
Me.ProcessDrop.Value = ""
Me.CompNameOpen.Value = ""
Me.CompEIDOpen.Value = ""
Me.RespNameOpen.Value = ""
Me.RespEIDOpen.Value = ""
Me.DescOpen.Value = ""
End Sub
What do I need to do to so I maintain the right columns for it all to be entered, but not be overwritten? Thank you
You need to change all lines that start
.Cells(RowCount, 5).Value ...
To
.Cells(RowCount + 1, 5).Value
The '+1' bit means you're using the next blank line.
Also, as Samuel pointed out, you should also change to
RowCount = ws.Cells (Rows.Count, 4).End (xlUp).Offset (1,0).Row
so that you're testing a column that's guaranteed to have data in it!
Sorry, I missed the offset bit ... No need to '+1' if you're offsetting by 1 ... It amounts to the same thing.
I'm having an issue getting my loops correctly skip records. I have multiple records that need updating in a master from many slave workbooks. Each slave has a different name (of a it's user) and I need to make my "Update" loop more efficient by only comparing those records that contain the name of the User, then comparing the unique IDs, rather than what it currently does, which is compare all of the unique IDs to find a match.
My current command button simply looks for the users name in the column and if the count is higher than 0, it calls the module to update the records for that user.
In both Master and Slave column 1 is always the unique ID of the record and column 2 is always the user that the record is assigned to. Here is my current test coding (will become a template for other user's workbooks):
Option Explicit
Public Sub Agnes_Update()
Dim owb As Workbook
Dim Master, Slave As Worksheet
Dim fpath As String
Dim i, j As Integer
fpath = Application.ActiveWorkbook.Path & "\Agnes.xlsx"
Set owb = Application.Workbooks.Open(fpath)
Set Master = ThisWorkbook.Worksheets("Allocated")
Set Slave = owb.Worksheets("Work")
For j = 2 To 10 '(the master sheet)
For i = 2 To 10 '(the slave sheet)
'if ID cell is blank exit - ends loop when all updates are completed
If Trim(Slave.Cells(j, 1).Value2) = vbNullString Then Exit For
'if column 2 of master does not contain the current username being
'updated then move to next record
If Master.Cells(j, 2).Value = "Agnes" Then
'if unique ID in column 1 matches slave from master then begin
'updating of required cells
If Master.Cells(i, 1).Value2 = Slave.Cells(j, 1).Value2 Then
Master.Cells(i, 4).Value = Slave.Cells(j, 4).Value
Master.Cells(i, 5).Value = Slave.Cells(j, 5).Value
Master.Cells(i, 6).Value = Slave.Cells(j, 6).Value
Master.Cells(i, 7).Value = Slave.Cells(j, 7).Value
Master.Cells(i, 8).Value = Slave.Cells(j, 8).Value
Master.Cells(i, 9).Value = Slave.Cells(j, 9).Value
Master.Cells(i, 10).Value = Slave.Cells(j, 10).Value
Master.Cells(i, 11).Value = Slave.Cells(j, 11).Value
Master.Cells(i, 12).Value = Slave.Cells(j, 12).Value
Master.Cells(i, 13).Value = Slave.Cells(j, 13).Value
Master.Cells(i, 14).Value = Slave.Cells(j, 14).Value
Master.Cells(i, 15).Value = Slave.Cells(j, 15).Value
Master.Cells(i, 16).Value = Slave.Cells(j, 16).Value
Master.Cells(i, 17).Value = Slave.Cells(j, 17).Value
Master.Cells(i, 18).Value = Slave.Cells(j, 18).Value
Master.Cells(i, 19).Value = Slave.Cells(j, 19).Value
Master.Cells(i, 20).Value = Slave.Cells(j, 20).Value
Master.Cells(i, 21).Value = Slave.Cells(j, 21).Value
Master.Cells(i, 22).Value = Slave.Cells(j, 22).Value
Master.Cells(i, 23).Value = Slave.Cells(j, 23).Value
End If
End If
Next
Next
Workbooks("Agnes").Close
End Sub
I prefer to use the Master.cells = Slave.cells method as some of the slave cells are locked to prevent the user amending data, and in the future the position of some of the data may change columns, so I will simply amend which master column = which slave column. I realize I could set the code to unlock the workbook, but that is even more coding for a simple bit of updating.
I believe the current issue with the code lies in the line If Master.Cells(j, 2).Value = "Agnes" Then as removing this line allows the code to go through all the unique IDs to find and update the master on all matches, but i'd rather it only try to match unique IDs when the user name is found in column 2 to try and make the code quicker and more efficient.
Can anyone help correct this code for me please?
If I am understanding correctly, you need to move the test prior to the inner loop (I have made a couple of other small changes to reduce the amount of code and stop repetition)
Public Sub Agnes_Update()
Dim owb As Workbook
Dim wbname As String
Dim Master, Slave As Worksheet
Dim fpath As String
Dim i, j As Integer
fpath = Application.ActiveWorkbook.Path & "\Agnes.xlsx"
Set owb = Application.Workbooks.Open(fpath)
wbname = Left$(owb.Name, InStrRev(owb.Name, ".") - 1)
Set Master = ThisWorkbook.Worksheets("Allocated")
Set Slave = owb.Worksheets("Work")
For j = 2 To 10 '(the master sheet)
'if column 2 of master does not contain the current username being
'updated then move to next record
If Master.Cells(j, 2).Value = wbname Then
For i = 2 To 10 '(the slave sheet)
'if ID cell is blank exit - ends loop when all updates are completed
If Trim(Slave.Cells(j, 1).Value2) = vbNullString Then Exit For
'if unique ID in column 1 matches slave from master then begin
'updating of required cells
If Master.Cells(i, 1).Value2 = Slave.Cells(j, 1).Value2 Then
Slave.Cells(j, 4).Resize(, 20).Copy
Master.Cells(i, 4).PasteSpecial Paste:=xlPasteValues
End If
End If
Next
Next
owb.Close SaveChanges:=False
End Sub