You are on page 1of 4

Option Explicit

'Function to remove all special characters from the string


Function cleanString(text As String) As String
Dim output As String
Dim c As Variant
Dim i As Integer
For i = 1 To Len(text)
c = Mid(text, i, 1)
If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <=
"Z") Then
output = output & c
Else
output = output & ""
End If
Next i
cleanString = output
End Function

Sub Connection_Data_Checking()

'Declaration of all Variables


Dim source_workbook As Workbook, target_workbook As Workbook
Dim source_worksheet As Worksheet, target_worksheet As Worksheet
Dim source_rows As Long
Dim i As Integer, j As Integer, k As Integer, x As Integer
Dim row_no As Integer, sor_cable_col_no As Integer, dummy As Integer
Dim source_path As String, target_path As String
Dim source_cable As String, source_end1 As String, source_end2 As String
Dim acell As Range, target_columns As Integer

'Selecting the source and target files


source_path = Application.GetOpenFilename _
(Title:="Please choose the Source File", _
FileFilter:="Excel Files *.xls* (*.xls*),")

target_path = Application.GetOpenFilename _
(Title:="Please choose the Target File", _
FileFilter:="Excel Files *.xls* (*.xls*),")

Set source_workbook = Workbooks.Open(source_path)


Set source_worksheet = source_workbook.Sheets(2)
Set target_workbook = Workbooks.Open(target_path)

'Finding the column number of cable name


Set acell = source_worksheet.Rows.Find(What:="Cable")
If Not acell Is Nothing Then
sor_cable_col_no = acell.Column
End If

'Counting total number of used rows in source sheet


source_rows = source_worksheet.Range(Mid(source_worksheet.Cells(1,
sor_cable_col_no).Address, 2, 1) & Rows.Count).End(xlUp).Row

'Initially making all the rows red in source sheet


For i = 1 To source_rows
For j = sor_cable_col_no - 1 To sor_cable_col_no + 19
If InStr(source_worksheet.Cells(i, sor_cable_col_no + 1), ":") <> 0 Then
source_worksheet.Cells(i, j).Interior.ColorIndex = 3
End If
Next j
Next i

'Comparing the cells in both source and target sheets


For i = 1 To source_rows
If InStr(source_worksheet.Cells(i, sor_cable_col_no + 1), ":") <> 0 Then
source_cable = "=+-" & split(source_worksheet.Cells(i, sor_cable_col_no +
1).Value, ":")(0)

For k = 1 To target_workbook.Sheets.Count

If source_cable = target_workbook.Sheets(k).Name Then


source_worksheet.Cells(i, sor_cable_col_no).Interior.ColorIndex = 4
source_worksheet.Cells(i, sor_cable_col_no + 1).Interior.ColorIndex
= 4
Set target_worksheet = target_workbook.Sheets(k)
dummy = 3

For j = sor_cable_col_no + 10 To sor_cable_col_no + 19


If cleanString(LCase(source_worksheet.Cells(i, j).text)) =
cleanString(LCase(target_worksheet.Cells(3, dummy).text)) Then
source_worksheet.Cells(i, j).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, j).Interior.ColorIndex = 3
End If
dummy = dummy + 1
Next j

For j = 1 To 36
If split(source_worksheet.Cells(i, sor_cable_col_no + 1).Value,
":")(1) = j Then
Set acell = target_worksheet.Columns(3).Find(What:=j, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
MatchCase:=False)
If Not acell Is Nothing Then
row_no = acell.Row
End If

source_end1 = source_worksheet.Cells(i, sor_cable_col_no +


2) & source_worksheet.Cells(i, sor_cable_col_no + 3) & source_worksheet.Cells(i,
sor_cable_col_no + 4)
source_end1 = cleanString(source_end1)
source_end2 = source_worksheet.Cells(i, sor_cable_col_no +
6) & source_worksheet.Cells(i, sor_cable_col_no + 7) & source_worksheet.Cells(i,
sor_cable_col_no + 8)
source_end2 = cleanString(source_end1)

If source_end1 = cleanString(target_worksheet.Cells(row_no,
5).text) Or source_end1 = cleanString(target_worksheet.Cells(row_no, 7).text) Then
source_worksheet.Cells(i, sor_cable_col_no +
2).Interior.ColorIndex = 4
source_worksheet.Cells(i, sor_cable_col_no +
3).Interior.ColorIndex = 4
source_worksheet.Cells(i, sor_cable_col_no +
4).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, sor_cable_col_no +
2).Interior.ColorIndex = 3
source_worksheet.Cells(i, sor_cable_col_no +
3).Interior.ColorIndex = 3
source_worksheet.Cells(i, sor_cable_col_no +
4).Interior.ColorIndex = 3
End If

If source_worksheet.Cells(i, sor_cable_col_no + 5).text =


target_worksheet.Cells(row_no, 6).text Or source_worksheet.Cells(i,
sor_cable_col_no + 5).text = target_worksheet.Cells(row_no, 8).text Then
source_worksheet.Cells(i, sor_cable_col_no +
5).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, sor_cable_col_no +
5).Interior.ColorIndex = 3
End If

If source_end2 = cleanString(target_worksheet.Cells(row_no,
5).text) Or source_end2 = cleanString(target_worksheet.Cells(row_no, 7).text) Then
source_worksheet.Cells(i, sor_cable_col_no +
6).Interior.ColorIndex = 4
source_worksheet.Cells(i, sor_cable_col_no +
7).Interior.ColorIndex = 4
source_worksheet.Cells(i, sor_cable_col_no +
8).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, sor_cable_col_no +
6).Interior.ColorIndex = 3
source_worksheet.Cells(i, sor_cable_col_no +
7).Interior.ColorIndex = 3
source_worksheet.Cells(i, sor_cable_col_no +
8).Interior.ColorIndex = 3
End If

If source_worksheet.Cells(i, sor_cable_col_no + 9).text =


target_worksheet.Cells(row_no, 8).text Or source_worksheet.Cells(i,
sor_cable_col_no + 5).text = target_worksheet.Cells(row_no, 8).text Then
source_worksheet.Cells(i, sor_cable_col_no +
9).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, sor_cable_col_no +
9).Interior.ColorIndex = 3
End If

If source_worksheet.Cells(i, sor_cable_col_no - 1).text =


target_worksheet.Cells(row_no, 4).text Then
source_worksheet.Cells(i, sor_cable_col_no -
1).Interior.ColorIndex = 4
Else
source_worksheet.Cells(i, sor_cable_col_no -
1).Interior.ColorIndex = 3
End If

End If
Next j
End If
Next k
End If
Next i
MsgBox ("Task Completed")

End Sub

You might also like