Re: My VBA does not work!!
- From: "NickHK" <TungCheWah@xxxxxxxxxxx>
- Date: Fri, 29 Jun 2007 18:19:30 +0800
Robert,
You have too many and too few objects here:
test = xlApp.ActiveWorkbook.Application.WorksheetFunction.VLookup(TaskAct,
Sheets("test1").Range("C:G"), 5, False)
xlApp.WorksheetFunction.VLookup is sufficient.
Also, using automation, always make sure all your object reference go
through xlApp so you avoid unqualified references. So you have something
like:
Dim WB as workbook
Dim WS as work***
Dim test as variant
set wb=xlapp.workbooks.open(<Path&Filename>)
set ws=wb.worksheets("test1")
'Not sure what TaskAct refers to
test = xlApp.WorksheetFunction.VLookup(<TaskAct>, WS.Range("C:G"), 5, False)
'etc
NickHK
"Robert Heuveling" <RobertHeuveling@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:1A068C52-E3CA-4447-8DAC-EC92387417CF@xxxxxxxxxxxxxxxx
In my following VBA (in ms project) i open a excel file and with a vlookupi
search for info. but not every vlookup will end up with a result thus itwill
give error 1004.
so far not a problem,
here is the part of the code which handles the error
Set test = Nothing
On Error Resume Next
test =
xlApp.ActiveWorkbook.Application.WorksheetFunction.VLookup(TaskAct,
Sheets("test1").Range("C:G"), 5, False)
If Err.Number = 1004 Then
TempPercent = 0
ProjectTaskT.Notes = "activiteit niet gevonden"
Else
TempPercent =
xlApp.ActiveWorkbook.Application.WorksheetFunction.VLookup(TaskAct,
Sheets("test1").Range("C:G"), 5, False)
End If
When i run the macro for the first time it will do the trick. but the 2nd
time it gives the result TempPercent = 0 every time also when there is a
match.
It looks like that the Err.Number hangs..
Does anybody have a clue how to solve this problem.
greetings
Robert Heuveling
Here is the complete code:
Sub Update_Percentage2()
' Deze macro haalt de technische voortgangscijfers uit een excel file en
plaatst deze in Fysiek percentage voltooid.
Dim xlApp As Excel.Application
Dim FilesParent, ProjectTasks As Tasks
Dim FileT, ProjectTaskT As Task
Dim Proj As MSProject.Application
Dim SpreadsheetName, XLSNameWithPath, TaskAct, Perccheck As String
Dim TempPercent As Integer
Dim test As Variant
Set Proj = GetObject(, "MSProject.Application")
' Hier moet de filenaam van de excelfile worden gegeven
XLSNameWithPath = InputBox("geef de filename (+ pad) van de update file")
'
' Hier wordt de excel file geopend
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open FileName:=XLSNameWithPath
' Hier wordt per activiteit gekeken of deze bestaat in excel en plaatst
vervolgens de percentage in het projectbestand
For Each ProjectTaskT In Proj.Application.ActiveProject.Tasks
' Gebruikt VLookUp (Verticaal zoeken om het % techn complete te vinden
If ProjectTaskT.Summary = False Then
TaskAct = ProjectTaskT.Text2
Set test = Nothing
On Error Resume Next
test =
xlApp.ActiveWorkbook.Application.WorksheetFunction.VLookup(TaskAct,
Sheets("test1").Range("C:G"), 5, False)
If Err.Number = 1004 Then
TempPercent = 0
ProjectTaskT.Notes = "activiteit niet gevonden"
Else
TempPercent =
xlApp.ActiveWorkbook.Application.WorksheetFunction.VLookup(TaskAct,
Sheets("test1").Range("C:G"), 5, False)
End If
ProjectTaskT.PhysicalPercentComplete = TempPercent
End If
Next ProjectTaskT
' Sluit MS Excel
xlApp.Visible = False
xlApp.Workbooks.Close
xlApp.Quit
Set xlApp = Nothing
End Sub
.
- Follow-Ups:
- Re: My VBA does not work!!
- From: Robert Heuveling
- Re: My VBA does not work!!
- References:
- My VBA does not work!!
- From: Robert Heuveling
- My VBA does not work!!
- Prev by Date: Re: Help deleting fields in SQL table
- Next by Date: Re: Unable to set the Display Alerts property of the application class
- Previous by thread: Re: My VBA does not work!!
- Next by thread: Re: My VBA does not work!!
- Index(es):