Hi all,
I am trying to write a vba code that attempts to index and match between 2 spreadsheet and finally loop through rows and columns to input the match data.
Unfortunately nothings happens and no error by visual basic.
Appreciate if someone can help.
For info. the following are named columns inside the "Database" worksheet.
"Leavecode" , "Employeecode" , "M:AG" , "crew"
Sub LookupLeaveCodes()
'Declare variables
Dim wsData As Worksheet
Dim wsOutput As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim i As Long
Dim j As Long
Dim empCode As Variant
Dim leaveCode As Variant
'Set worksheet variables
Set wsData = ThisWorkbook.Worksheets("Database")
Set wsOutput = ThisWorkbook.Worksheets("Output")
'Find last row and column of data in worksheet
LastRow = wsOutput.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = wsOutput.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
'Loop through each row in the specified range
For i = 18 To LastRow
'Loop through each column in the specified range
For j = 25 To LastCol
'Check if the cell is empty
If Not IsEmpty(wsOutput.Cells(i, j).Value) Then
'Get the employee code and leave code for the current cell
empCode = wsOutput.Cells(i, 1).Value
leaveCode = Application.Index(wsData.Range("Leavecode"), _
Application.Match(1, (wsData.Range("Employeecode") = empCode) * (wsData.Range("M:AG") = wsOutput.Cells(17, j).Value) * (wsData.Range("crew") = wsOutput.Cells(14, j).Value), 0))
'Check if a leave code was found
If Not IsError(leaveCode) Then
'Populate the leave code into the current cell
wsOutput.Cells(i, j).Value = leaveCode
Else
'If no leave code was found, clear the current cell
wsOutput.Cells(i, j).ClearContents
End If
End If
Next j
Next i
'Inform user that the code has finished running
MsgBox "Leave code lookup complete.", vbInformation
End Sub
I am trying to write a vba code that attempts to index and match between 2 spreadsheet and finally loop through rows and columns to input the match data.
Unfortunately nothings happens and no error by visual basic.
Appreciate if someone can help.
For info. the following are named columns inside the "Database" worksheet.
"Leavecode" , "Employeecode" , "M:AG" , "crew"
Sub LookupLeaveCodes()
'Declare variables
Dim wsData As Worksheet
Dim wsOutput As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim i As Long
Dim j As Long
Dim empCode As Variant
Dim leaveCode As Variant
'Set worksheet variables
Set wsData = ThisWorkbook.Worksheets("Database")
Set wsOutput = ThisWorkbook.Worksheets("Output")
'Find last row and column of data in worksheet
LastRow = wsOutput.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = wsOutput.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
'Loop through each row in the specified range
For i = 18 To LastRow
'Loop through each column in the specified range
For j = 25 To LastCol
'Check if the cell is empty
If Not IsEmpty(wsOutput.Cells(i, j).Value) Then
'Get the employee code and leave code for the current cell
empCode = wsOutput.Cells(i, 1).Value
leaveCode = Application.Index(wsData.Range("Leavecode"), _
Application.Match(1, (wsData.Range("Employeecode") = empCode) * (wsData.Range("M:AG") = wsOutput.Cells(17, j).Value) * (wsData.Range("crew") = wsOutput.Cells(14, j).Value), 0))
'Check if a leave code was found
If Not IsError(leaveCode) Then
'Populate the leave code into the current cell
wsOutput.Cells(i, j).Value = leaveCode
Else
'If no leave code was found, clear the current cell
wsOutput.Cells(i, j).ClearContents
End If
End If
Next j
Next i
'Inform user that the code has finished running
MsgBox "Leave code lookup complete.", vbInformation
End Sub