jeudi 7 novembre 2019

Issue while fetching data by comparing IDs from two different sheets in Excel VBA. Gives incorrect data

I have written VBA code in excel to fetch driver weekly data in single master payment sheet. I use Driver ID has primary key to to fetch driver data. There are total 4 weeks reports MCMSSummaryReport(Week1), MCMSSummaryReport(Week2), MCMSSummaryReport(Week3),MCMSSummaryReport(Week4).

I am trying to fetch data in sheet "Monthly Payment Master2" by comparing driver ID. "Monthly Payment Master2" has list of driver id. I compare Monthly Payment Master2's driver id with other 4 weekly reports.

however when code does not find same id in weekly report which is present in Monthly Payment Master2 table it should return "" (blank) in column 'Week1'. It returns the blank where Ids does not match but after that loop skip a row and fetch data from 1+1 row.

unable to fix this issue in the code.

Below is the excel macro enable sheet link :

below is the code :

    Private Sub CommandButton1_Click()

Dim salary As String, fromdate As String
Dim lastcoluns As Long, lastrow As Long, erow As Long, ecol As Long, lastrow1 As Long
lastcoluns = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
lastrow1 = Sheet7.Cells(Rows.Count, 1).End(xlUp).Row + 1

MsgBox (lastrow1)


Dim i As Integer
i = 2
Do While i < lastrow1
    temp1 = Worksheets("Monthly Payment Master2").Cells(i, 1)
    For j = 2 To lastrow + 1
        temp2 = Worksheets("MCMSSummaryReport(week 1)").Cells(j, 1)
        If temp1 = temp2 Then
            salary = Sheet1.Cells(i, 18).Value
            Worksheets("Monthly Payment Master2").Cells(i, 7) = salary

        Else

        End If
    Next j
    i = i + 1
Loop
MsgBox ("Week-1 data submitted successfully, Please submit Week-2 Data.")
Application.CutCopyMode = False
Sheet6.Columns().AutoFit
Range("A1").Select

End Sub

Aucun commentaire:

Enregistrer un commentaire