Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


getObject() and createObject()


getObject() and createObject()

Author
Message
cloudet
cloudet
Forum God
Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)

Group: Forum Members
Posts: 10, Visits: 1

Dear all:

what is the difference between this two functions and how should i use them properly?

thanks.


johnboy891
johnboy891
Forum God
Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)

Group: Forum Members
Posts: 545, Visits: 1K

getobject is used to return an instance of an already running activex object.  creatobject is used to create an instance of an activex object.  an activex object is an object that can be automated with vb.  it's confusing to explain when you would want to use one or the other.. sometimes i get messed up over it.  give some examples of what you are trying to do and maybe we can help more.


cloudet
cloudet
Forum God
Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)

Group: Forum Members
Posts: 10, Visits: 1

dear all:

thanks for the concern. Now i have a big problem but can't solve it. I posted and asked for lots of examples and helps but still can't get over with it.. Now i'll post the problem here. Hope that u can help me. Lots of thanks...

there are 4 excel workbooks (eg: A,B,C,D) which contains lots of project records, together with other related info such as developement name, developement area, date start/completed, status and etc.

i want to do a search in VB to search a particular project info among the 4 workbooks based on the project ID. The project ID is unique so i can get the result i want. THen the VB will open the workbook which contain the data that i want, for example, i search for project P001. P001 is inside workbook B. Vb will open workbook B, and highlight the entire row of project P001.

The search also can based on the project area. For example, i want to search for project in area_A, VB will retrieve all the project that under area_A in 4 workbooks A,B,C,D, then it generates a new workbook called 'result.xls' to display all the project under area_A with the format just like workbook A,B,C,D.

This program can also direct-link to those 4 workbooks. There are 4 buttons(A,B,C,D) which, when clicked, will open the respective workbooks.

I know this might seem easy for u all....but i'm new in VBA and this is my 1st project...Can someone help me with this? THanks so much....


johnboy891
johnboy891
Forum God
Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)

Group: Forum Members
Posts: 545, Visits: 1K

attached is a working example.  what you will need to do is:

  1. create a new workbook and past the below code into it.
  2. create A.xls and B.xls and store them in "c:\testproject" or edit the wbkPath string in the code to another location
  3. create a table like this in both workbooks:

      A          B          C          D
    1 Name      Gender     Age        Hair Color
    2 Mary Hook Female     35         Blue
    3 John Bob  Male       25         Red
    4 Sue Blow  Female     51         Gray
    5 Jay Hay   Male       19         Blonde

  4. run the macro and it will display what it has found, what workbook it found it in, who the record belongs to, and what column is was found in.

i did the easy part for you.  it's up to you to write the code that is displayed with the msgbox to another excel file or even a listbox.  the reason i did all this in the first place was because i already wrote it before for another deal i was working on.  have fun.


Sub goopy()

Dim fArray() As Range, wbkArray()
Dim sString As String, wbkPath As String
Dim find1st As Range, curRange As Range, fRange As Range
Dim fCount As Long

wbkArray = Array("a.xls", "b.xls")
wbkPath = "c:\testproject\"
sString = InputBox("Enter Search")

If Trim(sString) = "" Then
    MsgBox "Enter something to search for!"
    Exit Sub
End If

For i = 0 To UBound(wbkArray)
    Workbooks.Open (wbkPath & wbkArray(i))
    Set fRange = Workbooks(wbkArray(i)).Sheets(1).Cells
    Set find1st = fRange.Find(sString, LookIn:=xlFormulas, lookat:=xlWhole)
    Set curRange = find1st
    If Not curRange Is Nothing Then
        Do
            ReDim Preserve fArray(0 To fCount)
            Set fArray(fCount) = curRange
            MsgBox "Found " & sString & " in " & wbkArray(i) & _
                " for " & fRange(curRange.Row, 1) & " under " & _
                fRange(1, curRange.Column) & "."
            fCount = fCount + 1
            Set curRange = fRange.FindNext(after:=curRange)
        Loop Until curRange Is Nothing Or curRange.Address = find1st.Address
    End If
    Workbooks(wbkArray(i)).Close
Next i

End Sub



cloudet
cloudet
Forum God
Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)

Group: Forum Members
Posts: 10, Visits: 1

dear expert:

it really works!!! thanks for the code!! u did help me a lot.

But i want to highlight the retrieved result instead of popping them out in a message box. I've done the coding for highlighting, based on your codes. But i can only highlight the 1st column of the retrieved result. Actually i want to highlight the entire columns of the selected row(s).

Here is my code (actually it's yours):

--------------------------------------------------------------

Sub test1()
Dim fArray() As Range, wbkArray()
Dim sString As String, wbkPath As String
Dim find1st As Range, curRange As Range, fRange As Range
Dim fCOunt As Long

wbkArray = Array("a.xls", "b.xls")
wbkPath = "c:\WINDOWS\Desktop\"
sString = InputBox("enter search")

If Trim(sString) = "" Then
MsgBox "enter code to choose"
Exit Sub
End If

For i = 0 To UBound(wbkArray)
Workbooks.Open (wbkPath & wbkArray(i))
Set fRange = Workbooks(wbkArray(i)).Sheets(1).Cells
Set find1st = fRange.Find(sString, LookIn:=xlFormulas, lookat:=xlWhole)
Set curRange = find1st
If Not curRange Is Nothing Then
Do
ReDim Preserve fArray(0 To fCOunt)
Set fArray(fCOunt) = curRange
Do
fRange(curRange.row, 1).Interior.ColorIndex = 36
Loop Until Workbooks(wbkArray(i)).Sheets(1).Cells = ""
fCOunt = fCOunt + 1
Set curRange = fRange.FindNext(after:=curRange)
Loop Until curRange Is Nothing Or curRange.Address = find1st.Address
End If
'Workbooks(wbkArray(i)).Close 'i don't want the workbook to be
Next i                                  'closed.

End Sub

--------------------------------------------------------------

Thanks again. pls, anyone can help me?


johnboy891
johnboy891
Forum God
Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)

Group: Forum Members
Posts: 545, Visits: 1K

check it out.. i know you were asking for something a little different.  i acknowledged your code, but don't have time now to try it cuz i am in a rush.  i worked on this last night.  the main module code has changed.  update it.. then follow directions afterwards to create a listbox.

i just editted this post today (4/27) to help explain what certain parts of the code do and what you will need to edit to suit your project


Sub goopy()

Dim fArray() As String
Dim sString As String, wbkPath As String
Dim find1st As Range, curRange As Range, fRange As Range
Dim fCount As Long

wbkArray = Array("a.xls", "b.xls") 'see explanation #5
wbkPath = "c:\testproject\"
sString = InputBox("Enter Search")

If Trim(sString) = "" Then
    MsgBox "Enter something to search for!"
    Exit Sub
End If

For i = 0 To UBound(wbkArray)
    Workbooks.Open (wbkPath & wbkArray(i))
    Set fRange = Workbooks(wbkArray(i)).Sheets(1).Cells
    Set find1st = fRange.Find(sString, LookIn:=xlFormulas, lookat:=xlWhole) '#6
    Set curRange = find1st
    If Not curRange Is Nothing Then
        Do
            fCount = fCount + 1
            ReDim Preserve fArray(1 To 4, 1 To fCount) 'see explanation #1
            fArray(1, fCount) = wbkArray(i)
            fArray(2, fCount) = fRange(curRange.Row, 1).Value 'explanation #2a
            fArray(3, fCount) = fRange(1, curRange.Column).Value '#2b
            fArray(4, fCount) = Replace(curRange.Address, "$", "")
            Set curRange = fRange.FindNext(after:=curRange)
        Loop Until curRange Is Nothing Or curRange.Address = find1st.Address
    End If
    Workbooks(wbkArray(i)).Close '#3
Next i

If fCount > 0 Then
    UserForm1.ListBox1.ColumnCount = 4 '#4
    UserForm1.ListBox1.Column = fArray
    UserForm1.Show
End If

End Sub


now create a form with a listbox.  change the columncount property to 4.  in the listbox1_change event add this code


Private Sub ListBox1_Change()

Dim tString As String

For i = 1 To Workbooks.Count
    tString = tString & Workbooks(i).Name & ","
Next i

If InStr(tString, ListBox1.Column(0)) = 0 Then _
    Workbooks.Open ("c:\testproject\" & ListBox1.Column(0))

Workbooks(ListBox1.Column(0)).Activate
Rows(Range(ListBox1.Column(3)).Row).Select

End Sub


it works real nice.  every time you click a record, it opens the specified workbook if it is not already open, switches to it, and selects the proper row.  the listbox displays the specific location where the data was found so you can home in on the record you want.  pretty swift if i do say so myself =).

explanations

  1. this line builds a 2D dynamic array that acquires all the found data that will eventually populate listbox1.  the integer 4 represents the number of possible record categories that data could be found in.
  2. these two lines record the location of found data. the integer value 1 for 2a represents the column where your name headings are located (ie, john, mary, etc).  2b is the row location where your record heads are (ie, name. hair color).  change these to match the layout of your own workbooks.
  3. this line closes the workbooks after the program is finished scanning records.  they will later be opened with the listbox event.  delete this line if you want all workbooks to remain open and it will not change the process.
  4. this line defines the number of columns in the listbox, one column for each possible record.  make sure this integer is the same as the one defined in expl. #1.
  5. this line creates an array of all workbooks you want to search.  the process is dynamic, so feel free to add string values defining names of other workbooks you want included in the overall inquiry.
  6. the constant xlWhole will only return a successful search on an item in a cell if the ultimate calulated value is equal to the search string.  searching for "cdefg" will fail against a cell containing "abcdefg".  if you want to be able to find "cdefg" within "abcdefg" then change the constant to xlPart.

cloudet
cloudet
Forum God
Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)

Group: Forum Members
Posts: 10, Visits: 1

dear KZSteele:

thanks so much for your help!!

our group is really very thankful and appreciate it. The changes u made is excellent.But there is one more problem which i want to bring out: when the user click on a data in the listbox, the userform only bring the user to the workbook where the data is, but the user cannot update the data. User can only update the data after the userform is closed. It is not so convinience if dealing with huge data updating. So, can u do me one more favour? We want to switch to the workbook and do the updating without closing the userform, so that user can always refer back to the userform to go to the next related data.

We've tried to add a userform.hide after Rows(Range(ListBox1.Column(3)).Row).Select, but the userform will just disappear and we can only update one data, and have to start over again the search in order to look for another data.

Thanks again for your help. really thankful.


johnboy891
johnboy891
Forum God
Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)Forum God (80K reputation)

Group: Forum Members
Posts: 545, Visits: 1K

if you are using excel versions later than 97 then there is a userform property called ShowModal.  this property can only be set in the userform propeties from the visual basic editor, not by code executed within a procedure.

change ShowModal to false.  this will allow you to make changes to your data in the background while the userform remains displayed onscreen.


cloudet
cloudet
Forum God
Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)Forum God (3K reputation)

Group: Forum Members
Posts: 10, Visits: 1

Bravo!!!

thanks man!! It works!! u really help us a lot. thanks again!!


dannyrose12
dannyrose12
Forum God
Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)Forum God (5.6K reputation)

Group: Forum Members
Posts: 13, Visits: 5
if you are using excel versions subsequent than 97 then there is a userform concept titled ShowModal. this dance can exclusive be set in the userform propeties from the seeable rudimentary application, not by encrypt executed within a machine.


Remote Control Cars | HGH supplements | Dissertation Help
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search