I need a code that gives a message depending the different values of some particular cells in a Project, I have already used VBA in Excel, but never in Project, so I don´t know how to do it
I did a code in VBA Excel that do the same thing that I need to do in Project, but I need the code but with the infomation of a Project, but the functions that I use in Excel, are not defined in Project, so I don´t know how to addecuate the code
Option Explicit
Private Sub Avance()
Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim i As Integer
Dim ListaTT As String
For i = 3 To 40
FechaPI = Round(Cells(i, 2).Value, 2)
FechaPF = Cells(i, 3).Value
FechaRI = Cells(i, 4).Value
FechaRF = Cells(i, 5).Value
PtjR = Round(Cells(i, 6).Value, 2)
PtjP = Round(Cells(i, 7).Value, 2)
If PtjR < PtjP Then
ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & Cells(i, 1).Value & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"
If FechaPF - DateValue(Now) < 0 Then
ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
ElseIf FechaPF - DateValue(Now) <= 7 Then
ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
End If
End If
Next i
MsgBox ListaT, vbCritical, "Advertencia"
End Sub
I am not sure if the way how I get the values of the cells are the only thing that will change about the code, but knowing how to do that, will be a big help
To get the values of a task, use the Task object. In this case you'll want to loop through all of the tasks using the Tasks object (collection of all tasks). It is unclear what task fields you need, but this should get you started:
Private Sub Avance()
Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim t As Task
For Each t In ActiveProject.Tasks
FechaPI = t.Start
FechaPF = t.Finish
FechaRI = IIf(t.BaselineStart = "NA", 0, t.BaselineStart)
FechaRF = IIf(t.BaselineFinish = "NA", 0, t.BaselineFinish)
PtjR = t.PercentComplete
PtjP = t.PhysicalPercentComplete
If PtjR < PtjP Then
ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & t.Name & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"
If FechaPF - DateValue(Now) < 0 Then
ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
ElseIf FechaPF - DateValue(Now) <= 7 Then
ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
End If
End If
Next t
MsgBox ListaT, vbCritical, "Advertencia"
End Sub
Note that the Percent Complete and Physical Percent Complete properties return a value from 0 to 100, so don't multiply by 100 later on.
Related
I am using Ms Access forms and I have created an on click event that locates a folder location but now I want to locate the folder location based on different criteria but when I add the if statement it expects a sub,function or property. Below is some demo code. I really hope someone can explain what is missing?
Private Sub Open_Email_Click()
Dim stAppName As String
Dim stAppNameA As String
Dim stAppNameB As String
stAppName = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B " & Me.BC & " " & Me.UC & "\"
stAppNameA = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\A\B " & Me.BC & " " & Me.UC & "\"
stAppNameB = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B\B " & Me.BC & " " & Me.UC & "\"
If (Me.BC = "60") And Me.UC Like "REF123*" Then stAppNameA
ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then stAppNameB
Else: stAppName
End If
Call Shell(stAppName, 1)
End Sub
I think the logic of your function could be reduced to the following, which may be more readable with fewer repeating expressions:
Private Sub Open_Email_Click()
Dim strTmp As String
If Me.BC = "60" Then
If Me.UC Like "REF123*" Then
strTmp = " DEMO\A\B "
Else
strTmp = " DEMO\B\B "
End If
Else
strTmp = " DEMO\B "
End If
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub
Alternatively, using a Select Case statement:
Private Sub Open_Email_Click()
Dim strTmp As String
Select Case True
Case Me.BC <> "60"
strTmp = " DEMO\B "
Case Me.UC Like "REF123*"
strTmp = " DEMO\A\B "
Case Else
strTmp = " DEMO\B\B "
End Select
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub
To test the resulting path, change:
Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
To:
Debug.Print "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\"
I think your If block is just a bit messy in terms of where you have newlines, and continuation characters (:). Try reformatting your code like this:
If (Me.BC = "60") And Me.UC Like "REF123*" Then
stAppName =stAppNameA
ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then
stAppName = stAppNameB
Else
stAppName =stAppName
End If
Call Shell(stAppName, 1)
This code is designed to detect the columns of start and finish of a shape which is used and displayed onto the caption of the shape itself. The following code is the problematic code:
Sub Take_Baseline()
Dim forcast_weeksStart() As String
Dim forcast_weeksEnd() As String
Dim forcastDate As String
Dim shp As Shape
Dim split_text() As String
'cycle through all the shapes in the worsheet and enter the forcast date for all the projects into their respective boxes
For Each shp In ActiveSheet.Shapes
'initialize forcast date by parsing
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
forcast_weeksEnd = Split(shp.BottomRightCell.Column.Text, " ")
forcastDate = forcast_weeksStart(1) & "-" & forcast_weeksEnd(1)
temp = shp.OLEFormat.Object.Object.Caption
If InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") Then
split_text = Split(shp.OLEFormat.Object.Caption, " ")
For i = 0 To (i = 3)
shp.TextFrame.Characters.Caption = split_text(i) & vbNewLine
Next i
ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "/" & "actualDate"
' ElseIf InStr(temp, "/-/") > 0 & InStr(temp, "In Prog") = 0 Then
'split_text = Split(shp.OLEFormat.Object.Object.Caption, " ")
' For i = 0 To (i = 2)
' shp.OLEFormat.Object.Caption = split_text(i) & vbNewLine
' Next i
'ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption & vbNewLine & "In Prog" & vbNewLine & ActiveSheet.Cells(4, AShape.TopLeftCell.Column).Text & " - " & ActiveSheet.Cells(4, AShape.BottomRightCell.Column).Text & vbNewLine & "dates: " & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & forcast_weeksStart(1) & " - " & forcast_weeksEnd(1) & "/" & "actualDate"
End If
Next shp
'For testing purposes
Sheet4.Range("A20").Value = forcast_weeksStart(1)
Sheet4.Range("A21").Value = forcast_weeksEnd(1) End Sub
The error is an
"invalid qualifier"
message which occurs on line
forcast_weeksStart = Split(shp.TopLeftCell.Column.Text, " ")
Right on the "column" word. I don't get why this is happening since the actual drop down menu has the column operation which i can select. I have tried everything from changing it to the OLEformat.Object.Caption etc etc. But nothing has worked. I am still relatively new to vba so any help will be appreciated. Thanks
I have connected VBA & SQL Database in order to pull information.
I have written a script that returns exactly what I want but I would like to make it dynamical (Change years used etc.) and I am here running into problems.
I need to have a special line in my SQL Query which only has 1 thing that changes between the lines (Number of lines need to change and the Case when y.Date_Year = )
I get an Error message in the below code saying that there is a Type mismatch at the " & " sign right above my "period ()" array.
Sub test()
Dim SQLDB As ADODB.Connection
Dim sQuery As String
Dim info()
Dim Start_D As String
Dim End_D As String
Dim Numerator_Used As String
Dim Denominator_Used As String
Dim Number_Years As Integer
Dim period()
Numerator_Used = Range("Numerator")
Denominator_Used = Range("Denominator")
Start_D = Range("Start_Date")
End_D = Range("End_Date")
Range("A11:J100000").Cells.ClearContents
Number_Years = End_D - Start_D
ReDim period(Number_Years + 1)
For i = 0 To Number_Years
period(i + 1) = ",sum(case when y.date_year = " & Start_D + i & " then n." & Numerator_Used & " end) / sum(case when y.date_year = " & Start_D + i & " then s." & Denominator_Used & " end) as '" & Numerator_Used & "/" & Denominator_Used & " " & Start_D + i & "' & _ "
Next i
' Get Margin Expectation Changes
sQuery ="select m.date_month" & _
" m.date_month " & _
period() & _
" from " & Numerator_Used & " as n" & _
" inner join " & Denominator_Used & " as s on s.company_id = n.company_id" & _
" and s.date_month_id = n.date_month_id" & _
" and s.date_year_id = n.date_year_id" & _
" inner join date_year as y on y.date_year_id = n.date_year_id" & _
" inner join date_month as m on m.date_month_id = n.date_month_id" & _
" where y.date_year between " & Start_D & " and " & End_D & " " & _
" and n." & Numerator_Used & " <> 0" & _
" and s." & Denominator_Used & " <> 0" & _
" group by m.date_month;"
Set rs = Common.SQL_Read(SQLDB, sQuery)
ThisWorkbook.Worksheets("Sheet1").Range("A11").CopyFromRecordset rs
Set SQLDB = Common.SQL_Close(SQLDB)
End Sub
As i mentioned in the ocmment to the question, you can not explicity convert period() data into string as it is an array of variant data type (each undefined variable is treated as variant data type). You have to loop through the array data, i.e.:
For i = LBound(period()) To UBound(period())
sQuery = sQuery & period(i) & "...."
Next
'finally:
sQuery = "SELECT ... " & sQuery & " ...."
Change the code as i mentioned above and let me know if it works.
Running the code below on access 2013 I get a system resource error. My understanding is that the program can take more time but the system resource error does not make sense. My laptop has 8gb RAM and core i3. Is there a better way to do this? Values used:
me.yearsback= 1
me.valdate = 5/31/2016
me.period = "monthly"
UPDATE: Used 0.5M rows first, producing the system resource error. However, when I reduce the number of rows, it runs fine.
:
Option Compare Database
Private Sub Calculate_Click()
Dim db As Database
Dim rs As Recordset
Dim x As Integer
Dim y As Integer
Dim Months As Integer
Dim WPmonthly As String ' field name for monthly written premium
Dim UPRmonthly As String ' field name for monthly unearned premium
Dim EPmonthly As String ' field name for monthly earned premium
Dim runningDate As Date
Dim runningDate2 As Date
Dim useDateLower As Date
Dim useDateUpper As Date
Months = Me.YearsBack * 12 + Month(Me.ValDate)
If Me.Period = "monthly" Then
Set db = CurrentDb
For x = 1 To Months
runningDate = Format(DateAdd("m", -x + 1, Me.ValDate), "mm yyyy")
WPmonthly = "WP M" & Month(runningDate) & " " & Year(runningDate)
EPmonthly = "EP M" & Month(runningDate) & " " & Year(runningDate)
UPRmonthly = "UPR M" & Month(runningDate) & " " & Year(runningDate)
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & WPmonthly & "] STRING;"
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & EPmonthly & "] STRING;"
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & UPRmonthly & "] STRING;"
If x = Months Then
runningDate = Format(DateAdd("m", -x, Me.ValDate), "mm yyyy")
UPRmonthly = "UPR M" & Month(runningDate) & " " & Year(runningDate)
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & UPRmonthly & "] STRING;"
End If
Next
For y = 1 To Months
runningDate2 = Format(DateAdd("m", -y + 1, Me.ValDate), "mm yyyy")
useDateLower = runningDate2
useDateUpper = Format(DateAdd("m", -y + 2, Me.ValDate), "mm yyyy")
WPmonthly = "WP M" & Month(runningDate2) & " " & Year(runningDate2)
EPmonthly = "EP M" & Month(runningDate2) & " " & Year(runningDate2)
UPRmonthly = "UPR M" & Month(runningDate2) & " " & Year(runningDate2)
Set rs = db.OpenRecordset("tblEPdata", dbOpenDynaset, dbSeeChanges)
Do Until rs.EOF
'Written Premium Calculation
If rs!issueDate < useDateUpper And rs!issueDate >= useDateLower Then
rs.Edit
rs.Fields(WPmonthly) = rs!grossPremium
rs.Update
End If
'UPR Calculation
If rs!issueDate < Me.ValDate Then
If rs!expiryDate < useDateUpper Then
rs.Edit
rs.Fields(UPRmonthly) = 0
rs.Update
ElseIf rs!effectiveDate < useDateUpper Then
rs.Edit
rs.Fields(UPRmonthly) = (rs!expiryDate - useDateUpper + 1) / (rs!expiryDate - rs!effectiveDate + 1) * rs!grossPremium
rs.Update
ElseIf rs!effectiveDate >= useDateUpper Then
rs.Edit
rs.Fields(UPRmonthly) = rs!grossPremium
rs.Update
Else:
rs.Edit
rs.Fields(UPRmonthly) = rs!grossPremium
rs.Update
End If
End If
rs.MoveNext
Loop
rs.Close
Next
End If
db.Close
End Sub
I do not get any syntax errors in the code below, but when I compile the code I get the Msgbox("cant post status") instead of the output from main.p(x).status(0). The x and 0 can be any number and I always get the cant post status box. What bugs me most is that I have a debug form with a richtextbox that I load all the data from all seven occurrences of the array: p into when the program starts, and it works just fine. I've included that code as well at the very bottom. Before I run the logging sub, I run an initialization sub that puts a default value into every variable. When I don't have the try/catch in the main_loop, I do not get an error, but all execution stops. My computer doesn't freeze, but actions that should take place after that sub do not. Does anyone know why I can't make a call to main.p(x).status(0) inside this sub?
'Main Class'
Public p(6) as structs.player
Public Shared Sub main_loop()
For x As Integer = 0 To (Main.p.Count - 1) Step 1
If Main.check_act(x) = False Then
MsgBox("past check act")
If Main.p(x).pos >= 1 And Main.p(x).pos <= 3 Then
MsgBox("past pos; pre death")
Try
MsgBox(Main.p(x).status(0))
Catch ex As Exception
MsgBox("cant post status")
End
End Try
End If
End If
Next
End Sub
'Structs Class'
Public Structure player
Dim name As String
Dim type As String
Dim pos As Integer
Dim wait As Integer
Dim mhp As Integer
Dim chp As Integer
Dim mmp As Integer
Dim cmp As Integer
Dim map As Integer
Dim cap As Integer
Dim atk As Integer
Dim def As Integer
Dim mak As Integer
Dim mdf As Integer
Dim spd As Integer
Dim acc As Integer
Dim eva As Integer
Dim crt As Integer
Dim status() As Integer
Dim stats() As Integer
Dim statr() As Integer
Dim elems() As Integer
Dim elemr() As Integer
Dim abl() As Boolean
End Structure
'Debug Class'
Public Shared Sub log(p As player)
'Stats'
Debug.log.Text += ">>> " & p.name.ToString & " <<<" & Chr(10)
Debug.log.Text += "Type: " & p.type.ToString & Chr(10)
Debug.log.Text += "Pos: " & p.pos.ToString & Chr(10)
Debug.log.Text += "Wait: " & p.wait.ToString & Chr(10) & Chr(10)
Debug.log.Text += "HP: " & p.mhp.ToString & _
"/" & p.chp.ToString & Chr(10)
Debug.log.Text += "MP: " & p.mmp.ToString & _
"/" & p.cmp.ToString & Chr(10)
Debug.log.Text += "AP: " & p.map.ToString & _
"/" & p.cap.ToString & Chr(10) & Chr(10)
Debug.log.Text += "ATK: " & p.atk.ToString & Chr(10)
Debug.log.Text += "DEF: " & p.def.ToString & Chr(10)
Debug.log.Text += "MAK: " & p.mak.ToString & Chr(10)
Debug.log.Text += "MDF: " & p.mdf.ToString & Chr(10)
Debug.log.Text += "SPD: " & p.spd.ToString & Chr(10)
Debug.log.Text += "ACC: " & p.acc.ToString & Chr(10)
Debug.log.Text += "EVA: " & p.eva.ToString & Chr(10)
Debug.log.Text += "CRT: " & p.crt.ToString & Chr(10) & Chr(10)
'Status And Elements'
For x As Integer = 0 To (p.status.Count - 1) Step 1
Debug.log.Text += p.status(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.stats.Count - 1) Step 1
Debug.log.Text += p.stats(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.statr.Count - 1) Step 1
Debug.log.Text += p.statr(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.elems.Count - 1) Step 1
Debug.log.Text += p.elems(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
For x As Integer = 0 To (p.elemr.Count - 1) Step 1
Debug.log.Text += p.elemr(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
'Abilities'
For x As Integer = 0 To (p.abl.Count - 1) Step 1
Debug.log.Text += p.abl(x).ToString & Chr(10)
Next
Debug.log.Text += Chr(10)
End Sub
I quickly debugged the code an determined that status(0) is the issue.
The following screen image shows the error ...