I found that there is a Vlookup function in Excel which returns the value from a column by comparing values from some other column. Followinf function also works like that. User have to enter following values
myfile = Excel File path
SheetName = Excel Sheet Name
Columnametocheck = Name of the source column
valtoCheck = Value to match
ColumnNameforReturnValues = Will return the value from this coulmn
Ex.
getCellValues (myfile,mySheetname, "myColumn1", "B","myColumn3")
will search "B" in mycolumn1 and return the correspoding value from myColumn3 that is "2"
myColumn1
|
myColumn2
|
myColumn3
|
A
|
X
|
1
|
B
|
Y
|
2
|
C
|
Z
|
3
|
Function getCellValues (Excelfile,Sheetname, Columnametocheck, valtocheck, ColumnNameforReturnValues )
returnvalue =""
'--connecting Excel---
set bdayExcel =CreateObject("Excel.Application")
bdayExcel.Visible = true
bdayExcel.Workbooks.Open(Excelfile)
'---connect with sheet --
Set xlSheet =bdayExcel.Sheets(Sheetname)
'-- get SourceColumn Number--
colnumber = xlSheet.Range("A1:z7").find(Columnametocheck).column
' ---- Get Return Coulumn Number---
returncolnumber = xlSheet.Range("A1:z7").find(ColumnNameforReturnValues).column
'--LOGIC---
UsedRows= xlSheet.UsedRange.rows.count
for i = 1 to usedRows
val=xlSheet.Cells(i,colnumber).value
if val = valtocheck then
if len(returnvalue) = 0 then
returnvalue = xlSheet.Cells(i,returncolnumber).value
Else
returnvalue = returnvalue & "," & xlSheet.Cells(i,returncolnumber).value
End If
End IF
Next
'---return found value---
getRowNumbers = returnvalue
End Function
returnvalue =""
'--connecting Excel---
set bdayExcel =CreateObject("Excel.Application")
bdayExcel.Visible = true
bdayExcel.Workbooks.Open(Excelfile)
'---connect with sheet --
Set xlSheet =bdayExcel.Sheets(Sheetname)
'-- get SourceColumn Number--
colnumber = xlSheet.Range("A1:z7").find(Columnametocheck).column
' ---- Get Return Coulumn Number---
returncolnumber = xlSheet.Range("A1:z7").find(ColumnNameforReturnValues).column
'--LOGIC---
UsedRows= xlSheet.UsedRange.rows.count
for i = 1 to usedRows
val=xlSheet.Cells(i,colnumber).value
if val = valtocheck then
if len(returnvalue) = 0 then
returnvalue = xlSheet.Cells(i,returncolnumber).value
Else
returnvalue = returnvalue & "," & xlSheet.Cells(i,returncolnumber).value
End If
End IF
Next
'---return found value---
getRowNumbers = returnvalue
End Function
No comments:
Post a Comment