Link data in sheet to individual - VBA

helplessnoobatexcel

New Member
Joined
Dec 15, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hi All,
Below is a code that is meant to link data in a sheet to an individual based on the column of individual names. However, this code does not run as the error is "sub not defined". Any kind soul willing to take a look at this code and help me troubleshoot it? It will be greatly apreciated ;D.
Sub LinkDataToIndividual()
Dim dataSheet As Worksheet
Dim linkedSheet As Worksheet
Dim lastDataRow As Long
Dim individualColumn As Long
Dim individualList As Range
Dim individual As Variant
Dim filterCriteria As Variant

'Set your data sheet and linked sheet'
Set dataSheet = Worksheets("Sheet5") 'Replace with your actual datasheet name'
Set linkedSheet = Worksheets("Sheet9") 'Replace with your actual linked sheet name'

'Set the column containing individual names or IDs'
individualColumn = 1 'Assuming the individual names or ID are in column 1'

'Find the last row with data in the data sheet'
last DataRow = dataSheet.Cells(dataSheet.Rows.Count, individualColumn).End(x1Up).Row

'Set the range of individual names or IDs'
Set individualList = dataSheet.Range(dataSheet.Cells(2, individualColumn), dataSheet.Cells(lastDataRow, individualColumn))

'Loop through each individual and link data'
For Each individual In individualList
'Set filter criteria based on the individual'
filterCriteria = individual.Value

'Apply filter to the data sheet'
dataSheet.Rows(1).AutoFilter Field:=individualColumn, Criteria1:=filterCriteria

'Copy visible cells (excluding header) to the linked sheet
dataSheet.UsedRange.Offset(1, 0).SpecialCells(x1CellTypeVisible).Copy linkedSheet.Cells(linkedSheet.Rows.Count, 1).End(x1Up).Offset(1, 0)

' Clear filter in the data sheet'
dataSheet.AutoFilterMode = False
Next individual

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You have made quite a number of posts and are still not using the VBA Code tags.
You are more likely to get a response if the person helping doesn't have to clean up your copied code first.
Just click on the VBA button and paste your code between the tags

1703567035713.png


Start with these changes.

Your code has:
last DataRow =
the actual variable name does not have a space in it and it should be:
lastDataRow =

Rich (BB code):
'Find the last row with data in the data sheet'
'last DataRow = dataSheet.Cells(dataSheet.Rows.Count, individualColumn).End(xlUp).Row
lastDataRow = dataSheet.Cells(dataSheet.Rows.Count, individualColumn).End(xlUp).Row

Your code has 4 occurences of x1 eg x1Up x1CellTypeVisible
Where did you get your code and how did copy paste it into here ?
You had to fix this same issue in your previous thread that @Flashbond helped you with.
Hence my question as to how and where you are getting the code.
Do a replace all for x1 to xl (lower case L).

Once you have made the above changes please advise on what error message you are still getting and on what line, if you are still getting a message.
 
Upvote 0
You have made quite a number of posts and are still not using the VBA Code tags.
You are more likely to get a response if the person helping doesn't have to clean up your copied code first.
Just click on the VBA button and paste your code between the tags

View attachment 103997

Start with these changes.

Your code has:
last DataRow =
the actual variable name does not have a space in it and it should be:
lastDataRow =

Rich (BB code):
'Find the last row with data in the data sheet'
'last DataRow = dataSheet.Cells(dataSheet.Rows.Count, individualColumn).End(xlUp).Row
lastDataRow = dataSheet.Cells(dataSheet.Rows.Count, individualColumn).End(xlUp).Row

Your code has 4 occurences of x1 eg x1Up x1CellTypeVisible
Where did you get your code and how did copy paste it into here ?
You had to fix this same issue in your previous thread that @Flashbond helped you with.
Hence my question as to how and where you are getting the code.
Do a replace all for x1 to xl (lower case L).

Once you have made the above changes please advise on what error message you are still getting and on what line, if you are still getting a message.
Ahh, I'm so sorry I didn't know about the VBA code tags I will take note of it in my future posts. Thanks for notifying me about it Alex.
I got this code from ChatGPT and manually copied it down since the organization restricts access to ChatGPT's website which was why there were so many typo mistakes. I have made the following changes as you have suggested and the code now runs smoothly except the dataset is repeated twice
 
Upvote 0
What are you trying to do ?
It is using column A on Sheet 5 as the criteria for the filter, and the same column to apply the filter to.
This not only means you will get all the data on the sheet but that each occurrence of a particular name will cause the rows for that name to be copied again.
 
Upvote 0
What are you trying to do ?
It is using column A on Sheet 5 as the criteria for the filter, and the same column to apply the filter to.
This not only means you will get all the data on the sheet but that each occurrence of a particular name will cause the rows for that name to be copied again.
I would like for data in the sheet to be linked to a user each. I don't want the data to be repeated due to repeated occurrences of the user's name. Any ideas on how I can achieve that?
 
Upvote 0
What do you mean by linked to a user ? There is nothing in that code that links sheets.
It filters the data sheet using Column A and copies it to another sheet. There is no "linking".
Please explain or if possible show what you want to finish up with as the final output.
 
Upvote 0
What do you mean by linked to a user ? There is nothing in that code that links sheets.
It filters the data sheet using Column A and copies it to another sheet. There is no "linking".
Please explain or if possible show what you want to finish up with as the final output.
Hi Alex,
Sorry for the confusion. Nevermind I was able to rectify the problem by myself. Thanks for your help!
 
Upvote 0
If you post what you did to rectify it you can mark your own post as a solution and it may help others.
 
Upvote 0
VBA Code:
Sub Merge_Sheets()

Dim startRow, startCol, lastRow, lastCol As Long
Dim headers As Range

'Set Master sheet for consolidation
Set mtr = Worksheets("MasterSheet")

Set wb = ThisWorkbook
'Get Headers
Set headers = Application.InputBox("Select the Headers", Type:=8)

'Copy Headers into master
headers.Copy mtr.Range("A1")
startRow = headers.Row + 1
startCol = headers.Column

Debug.Print startRow, startCol
'loop through all sheets
For Each ws In ActiveWindow.selectedSheets
     'except the master sheet from looping
     If ws.Name <> "Master" Then
        ws.Activate
        lastRow = Cells(Rows.Count, startCol).End(xlUp).Row
        lastCol = Cells(startRow, Columns.Count).End(xlToLeft).Column
        'get data from each worksheet and copy it into Master sheet
        Range(Cells(startRow, startCol), Cells(lastRow, lastCol)).Copy _
        mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1)
           End If
Next ws

Worksheets("MasterSheet").Activate

End Sub
This is the code to merge sheets
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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