Link to home
Start Free TrialLog in
Avatar of Aamir Hussain
Aamir Hussain

asked on

Separately Copy data from MAJOR SHEET for boys and girls to attendance workbook by condition of group and gender.

As one expert give me a solution of my question now I want that by click button in MASTER Sheet data of boys and girls separately send to a  new attendance workbook by the apply condition of group and gender.
student-record-workbook_v4.xlsm
ET-Attendance---Marks-sheets-Boys--.xlsx
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Aamir,

I have created a Test Workbook to test. (attached).
Create a Test Folder on your desktop and download both the attached workbook.
You can open Test Workbook and check few sheets, I have cleared all the data in all the sheet for testing purpose.
Close the Test Workbook.
Open Student Record Workbook.
Click Copy Data, it will prompt you to select which workbook you need to update, for testing select Test Workbook.
It will copy all the roll numbers and student's names from Student Record Workbook to Test Workbook's respective sheets, will save the Test Workbook and close.
Open again Test Workbook and check all the data has been copied.
Below is the code:
Sub UpdateAttendance()
Dim FolderPath As String, Filter As String, Caption As String, fName As String
Dim SourceBook As Workbook, TargetBook As Workbook
Dim LastRow1 As Long, LastRow2 As Long
Dim TargetSh As Worksheet, Ws As Worksheet
Dim i As Long, a As Long

'Disable Events
With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "!!! Please Be Patient...Updating Records !!!"
    .EnableEvents = False
    .Calculation = xlManual
End With

'Set Variables
FolderPath = Application.ThisWorkbook.Path
ChDir FolderPath
Set TargetBook = Application.ThisWorkbook
Filter = "Text files (*.xl*),*.xl*"
Caption = "Please Select an input file "
fName = Application.GetOpenFilename(Filter, , Caption)
Set SourceBook = Application.Workbooks.Open(fName)
Set TargetSh = TargetBook.Worksheets("MASTER Sheet")
LastRow1 = TargetSh.Range("B" & Rows.Count).End(xlUp).Row
TargetSh.Range("AB5:AB" & LastRow1).FormulaR1C1 = "=IF(RC16=""Female"",LOWER(RC3)&""G"",LOWER(RC3)&""B"")"
TargetSh.Range("AB5:AB" & LastRow1).Value = TargetSh.Range("AB5:AB" & LastRow1).Value

    'Copy Paste Data
    For Each Ws In SourceBook.Worksheets
        For a = 5 To LastRow1
            LastRow2 = Ws.Range("A" & Rows.Count).End(xlUp).Row
            i = LastRow2 + 1
                If TargetSh.Cells(a, 28) = Ws.Name Then
                    Ws.Cells(i, 1).Value = TargetSh.Cells(a, 2).Value
                    Ws.Cells(i, 2).Value = TargetSh.Cells(a, 4).Value
                    i = i + 1
                End If
        Next a
    Next Ws
    
'Close Source Workbook
Application.DisplayAlerts = False
SourceBook.Close SaveChanges:=True
Application.DisplayAlerts = True

TargetSh.Columns("AB:AB").Delete

'Enable Events
With Application
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .StatusBar = False
    .EnableEvents = True
    .Calculation = xlAutomatic
End With
End Sub

Open in new window

Hope this helps.
Test.xlsx
student-record-workbook_v5.xlsm
Avatar of Aamir Hussain
Aamir Hussain

ASKER

It's amazing :-) No expert did comment on my question.I thought it's impossible .But you did it :-) Surprise it's doing in same way which I  want :-)
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's a complete working now :-) Same output as I want :-) Again thanks for giving precious time for me :-) Greate thanks for you.