#171332 - 2006-12-12 12:11 AM
Re: read cell from excel then validate data
[Re: booey]
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
we need a script that will look at cell C6 in Excel and save that value as a variable.
Say I have another excel file to compare it to. In column A there is a list of names in A1, A2, etc and in column B there is a folder path for each value in column A. Can I compare the value from the first excel file to the second excel files column A and then move the file to the folder path located in column two of the second file? Just create 2 Excel workbooks. In your first workbook, on the first page, you put in cell C6 a value. In your second workbook, you put on the first page in column A, starting at A1, the values you want to compare to. in column B, starting at B1, you write the file paths where the file should be copied to. Now replace the lines $File1 = "D:\scripts\test\Workbook1.xls" and $File2 = "D:\scripts\test\WorkBook2.xls" in the example script and run it. $Values[1,$j] should return the value in column B of the second file that you were looking for. I changed my script because Excel was not properly closed after ending the script!!!
;Set script options If NOT @LOGONMODE Break On Else Break Off EndIf Dim $RC $RC = SetOption("Explicit","On") $RC = SetOption("NoVarsInStrings","On") $RC = SetOption("NoMacrosInStrings","On") $RC = SetOption("WrapAtEOL","On")
Dim $objExcel, $File1, $File2, $Value ;$Values is a 2-dimensional array Dim $Values[1,0], $i ;$i is a counter $i = -1 ;(Path and) filename of first Excel workbook $File1 = "D:\scripts\test\Workbook1.xls" ;(Path and) filename of second Excel workbook $File2 = "D:\scripts\test\Workbook2.xls"
;Create Excel object $objExcel = CreateObject("Excel.Application") ;If creation object failed, an @ERROR <> 0 is returned If @ERROR = 0
;Open first Excel workbook $RC = $objExcel.Workbooks.Open($File1) ;Catch value in cell C6 $Value = $objExcel.Range("C6").Value ;Close workbook $objExcel.Application.Quit ;Open second Excel workbook $RC = $objExcel.Workbooks.Open($File2) ;Loop as long as values are found in column A While NOT $objExcel.Range("A"+($i+2)).Value = "" ;Add 1 to counter $i $i = $i + 1 ;Resize 2-dimensional array $Values, preserving existing data ReDim preserve $Values[1,$i] ;Put value from column A in first dimension of array $Values[0,$i] = $objExcel.Range("A"+($i+1)).Value ;Put value from column B in second dimension of array $Values[1,$i] = $objExcel.Range("B"+($i+1)).Value Loop ;Close workbook $objExcel.Application.Quit Else
;Error handling ? "Error creating Excel object" ? "Error " + @ERROR + ": " + @SERROR ;End the KiXtart script Quit @ERROR
EndIf
;$j is a counter Dim $j For $j = 0 To $i Step 1 ;compare $Value with every value in the first dimension ;of the 2-dimensional array ;If values match, go into the if statement If $Values[0,$j] = $Value ;Here you can write your MOVE command Move $File1 $Values[1,$j] ;Error handling if move fails If @ERROR ? "Error moving file " + $File1 ? "Error " + @ERROR + ": " + @SERROR EndIf ;The value was found, end the for/next by making $j equal to $i $j = $i EndIf Next
|
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1343 anonymous users online.
|
|
|