Option Explicit
Sub Controls()
' hiker95, 08/11/2013
' http://www.mrexcel.com/forum/excel-questions/718358-number-columns-needs-find-out-within-range.html
Dim wsCheck As Worksheet, wsCheck1 As Worksheet, wsCheck2 As Worksheet
Dim wsCheck3 As Worksheet, wsCheck4 As Worksheet, wsCheck5 As Worksheet
Dim lc As Long, lc1 As Long, lc2 As Long, lc3 As Long, lc4 As Long
Dim a As String, p As String, x As String, y As String, ac As String
On Error Resume Next
Set wsCheck = Sheets("Input JE Data")
If wsCheck Is Nothing Then
MsgBox ("Missing 'Input JE Data' Sheet/Tab")
Exit Sub
Else
Sheets("Input JE Data").Select
lc = Cells(1, Columns.Count).End(xlToLeft).Column
If WorksheetFunction.CountA(Range(Cells(1, 1), Cells(1, lc))) <> 30 Then
MsgBox "Input JE Data Sheet/Tab doesn't have standard number of colums. Please check if there are any colums deleted or added", vbInformation
Exit Sub
End If
End If
'************************************************************
'Check Sheets("Input JE Data") for the following columns:
'A "Final Index #"
'P "Charge out in local currencyC = (A)*(B)"
'X "Cost Center(Expense)"
'Y "WBS Code(Expense)"
'AC "JV cost details"
With Sheets("Input JE Data")
a = "": p = "": x = "": y = "": ac = ""
If InStr(.Cells(1, 1), "Final Index #") = 0 Then
a = "Final Index #"
End If
If InStr(.Cells(1, 16), "Charge out" & vbLf & "in local currency" & vbLf & "C = (A)*(B)") = 0 Then
p = "Charge out in local currency C = (A)*(B)"
End If
If InStr(.Cells(1, 24), "Cost Center" & vbLf & "(Expense)") = 0 Then
x = "Cost Center(Expense)"
End If
If InStr(.Cells(1, 25), "WBS Code" & vbLf & "(Expense)") = 0 Then
y = "WBS Code(Expense)"
End If
If InStr(.Cells(1, 29), "JV cost details") = 0 Then
ac = "JV cost details"
End If
If a = "" And p = "" And x = "" And y = "" And ac = "" Then
'do nothing
Else
MsgBox "The following titles in row 1 are missing/not correct: " & vbCrLf & vbCrLf & _
a & vbCrLf & _
p & vbCrLf & _
x & vbCrLf & _
y & vbCrLf & _
ac & vbCrLf & ""
Exit Sub
End If
End With
'************************************************************
Set wsCheck1 = Sheets("Master Data for CoCo")
If wsCheck1 Is Nothing Then
MsgBox ("Missing 'Master Data for CoCo' Sheet/Tab")
Exit Sub
End If
Set wsCheck2 = Sheets("Relief CC")
If wsCheck2 Is Nothing Then
MsgBox (" Missing 'Relief CC' Sheet/Tab")
Exit Sub
Else
Sheets("Relief CC").Select
lc1 = Cells(1, Columns.Count).End(xlToLeft).Column
If WorksheetFunction.CountA(Range(Cells(1, 1), Cells(1, lc1))) <> 47 Then
MsgBox "'Relief CC' Sheet/Tab doesn't have standard number of colums. Please check if there are any colums deleted or added", vbInformation
Exit Sub
End If
End If
Set wsCheck3 = Sheets("Expense WBS")
If wsCheck3 Is Nothing Then
MsgBox ("Missing 'Expense WBS' Sheet/Tab")
Exit Sub
Else
Sheets("Expense WBS").Select
lc2 = Cells(1, Columns.Count).End(xlToLeft).Column
If WorksheetFunction.CountA(Range(Cells(1, 1), Cells(1, lc2))) <> 18 Then
MsgBox "'Expense WBS' Sheet/Tab doesn't have standard number of colums. Please check if there are any colums deleted or added", vbInformation
Exit Sub
End If
End If
Set wsCheck4 = Sheets("Expense CC")
If wsCheck4 Is Nothing Then
MsgBox ("Missing 'Expense CC' Sheet/Tab")
Exit Sub
Else
Sheets("Expense CC").Select
lc3 = Cells(1, Columns.Count).End(xlToLeft).Column
If WorksheetFunction.CountA(Range(Cells(1, 1), Cells(1, lc3))) <> 47 Then
MsgBox "'Expense CC' Sheet/Tab doesn't have standard number of colums. Please check if there are any colums deleted or added", vbInformation
Exit Sub
End If
End If
Set wsCheck5 = Sheets("Expense IO")
If wsCheck5 Is Nothing Then
MsgBox ("Missing 'Expense IO' Sheet/Tab")
Exit Sub
Else
Sheets("Expense IO").Select
lc4 = Cells(1, Columns.Count).End(xlToLeft).Column
If WorksheetFunction.CountA(Range(Cells(1, 1), Cells(1, lc4))) <> 47 Then
MsgBox "'Expense IO' Sheet/Tab doesn't have standard number of colums. Please check if there are any colums deleted or added", vbInformation
Exit Sub
End If
End If
On Error GoTo 0
End Sub