Nov 22, 2009

Ms Excel : Search A Sheet

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > VB Programming

Ms Excel : Search A Sheet

kvarnerexpress
Anybody out there good with VBA and Excel? I am trying to search a sheet in a workbook (Sheet1), find out how many times particular values exists (int 1 to 54), then create a new workbook with a list of the values checked for and how many times they appeared.
Been googling this, but everything I have found just confused me more, never done much with Office before. Thanks in advance for any help!!

By the way, I am using Excel 2003 if that matters...

Comment/Reply (w/o sign-up)

lava.alt
here is a little example of how to use excel for what you need ... this subrutine find how many times a value, that is between a range, appears ... i hope you find it useful ...

CODE
Private Sub LoadExcelFile()

   Dim FileDir   As String
   Dim Row, Col      As Integer
   
   Dim xlsApp    As Excel.Application
   Dim Book      As Excel.Workbook
   Dim Sheet1    As Excel.Worksheet
   Dim Sheet2    As Excel.Worksheet

On Error GoTo xError
   ' Assign object references to the variables. Use
   ' Add methods to create new workbook and worksheet
   ' objects.
   FileDir = App.Path & "\MyFile.xls"
   
   Set xlApp = New Excel.Application
   xlApp.Visible = True
   
   Set Book = xlApp.Workbooks.Open(FileName:=FileDir, ReadOnly:=False)
   Book.Activate
   
   Set Sheet1 = Book.Worksheets("Sheet1")
   Sheet1.Activate
   
   Set Sheet2 = Book.Worksheets("Sheet2")
   
   
   For Row = 1 To 10
       For Col = 1 To 5
           ' here we view is the value of the cell
           ' is between the range we need
           If (Val(Sheet1.Cells(Row, Col)) > 1) And _
              (Val(Sheet1.Cells(Row, Col)) < 10) Then
               
               Sheet2.Activate
               
               Sheet2.Cells(Sheet1.Cells(Row, Col), 1) = Sheet1.Cells(Row, Col)
               Sheet2.Cells(Sheet1.Cells(Row, Col), 2) = _
               Val(Sheet2.Cells(Sheet1.Cells(Row, Col), 2)) + 1
               
               Sheet1.Activate
               
           End If
       Next Col
   Next Row
   
   ' we save the changes
   Book.Save
   
   ' close the objects
   Book.Close
   xlApp.Quit
   
   ' clean them from memory
   Set xlApp = Nothing
   Set Book = Nothing
   Set Sheet = Nothing

Exit Sub
xError:
MsgBox "An error ocurred during the process"
End Sub


here is the matrix is use for the calc

the results appear in the second sheet

8 15 7 13 58
4 12 5 14 1
12 4 3 51 63
5 5 3 12 8
2 2 6 18 3
5 5 3 95 43
11 1 5 5 3
3 4 5 2 1
1 2 6 23 7
8 54 83 62 121

and the results i obtained are these

 

 

 


Comment/Reply (w/o sign-up)

lava.alt
sorry i forgot to put the results

2 8
3 12
4 6
5 16
6 4
7 4
8 6

i recomend you to debug the program so you undestand what is doing ...

Comment/Reply (w/o sign-up)

ARNEL
QUOTE(kvarnerexpress @ Jun 7 2005, 05:47 AM)
Anybody out there good with VBA and Excel? I am trying to search a sheet in a workbook (Sheet1), find out how many times particular values exists (int 1 to 54), then create a new workbook with a list of the values checked for and how many times they appeared.
Been googling this, but everything I have found just confused me more, never done much with Office before. Thanks in advance for any help!!

By the way, I am using Excel 2003 if that matters...
*



Lava.alt posted a nice script for your que, fore more info, try to look for windows help files from microsoft excel, there you will find a lot of tips with regards to your topics....

Comment/Reply (w/o sign-up)

dul
At the moment macro language is better /MS offices/. You can use any MS Office macro command via OLE /Object Embedding Language/. It is quickest and easiest way.

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


Searching Video's for ms, excel, search, sheet
See Also,
advertisement


Ms Excel : Search A Sheet

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com