I have a lot of dates in column D. I need to find the student with the earliest date, and show the following information in a messagebox:
Sub Finddate()
Dim Mn As Integer
Mn = Application.Match(Application.Min(Range("D2:D18288")), Range("D2:D18288"), 0)
MsgBox ("For the student with the earliest date (" & Range("D" & Mn) & ") the following information applies: " & Range("k" & Mn) & ", " & Range("L" & Mn) & " and " & Range("M" & Mn))
End Sub
However when i run the Macro it shows the wrong date. The earliest date in the sheet is 31-08-1996, but it says the earliest date is 01-02-2010 and if i write =min(D2:D18288) in Excel it finds the right date. But i need it to work in VBA as well. And if i change min to max it also finds the wrong date. But if i instead write:
Mn = Application.Match(Application.Max(Range("D2:D18288")), Range("D2:D18288"))
It shows the right date but i need to find the min date not the max date and when i change max to min I get a type mismatch error. I really don’t know what is wrong really hope someone can help me!
Your indexing is off by 1 ................because the data starts out a D2 rather than D1, Mn points to the cell just above the minimum.
When something like this happens, try to replicate the result, using a small sample. E.g. this one, hoping to return Peter6 for the smallest info:
Option Explicit
Public Sub TestMe()
Dim dateRanges As Range
Set dateRanges = Range("D1:D11")
Dim mn As Variant
With Application
mn = .Match(.Min(dateRanges), dateRanges, 0)
End With
MsgBox Range("E" & mn).Value2
End Sub
Once it works, try to fix it with your big example.
You will probably notice that mn should not be Integer as far as Integer is up to 32767 and this parsed to a date is 16-September-1989, which is long time ago. In your case it is not an error, because you are not referencing mn directly to a date, but it may happen at a later.
Related
When looking at the possible PjAssignmentTimescaledData options (here), one that is missing that I need to extract is the Remaining Work field. Has anyone ever been able to figure out how to use VBA to extract out weekly assignments based on remaining work?
What I have is in a section of my VBA is:
ass.TimeScaleData(tsk.Start, tsk.Finish, pjAssignmentTimescaledActualWork, pjTimescaleWeeks)
but I would imagine i could replace
pjAssignmentTimescaledActualWork
with
pjAssignmentTimescaledRemainingWork
but that does not work.
Am I asking for something that just doesn't exist or looking at this whole operation in a backwards way?
Unfortunately, pjAssignmentTimescaledRemainingWork does not exist (which is annoying). To get the time phased remaining work, you need to take the value you get from pjAssignmentTimescaledWork and subtract the value you get from pjAssignmentTimescaledActualWork. Here's some sample code to get the time phased remaining work of selected tasks:
Public Sub GetTimeScaledRemainingValues()
Dim a As Assignment
Dim t As Task
Dim totalTSVs As TimeScaleValues
Dim actualTSVs As TimeScaleValues
Dim totalTSV As TimeScaleValue
Dim actualTSV As TimeScaleValue
Dim remainingValue As Double
For Each t In Application.ActiveSelection.Tasks
For Each a In t.Assignments
'get the total TSV values and store in a variable
Set totalTSVs = a.TimeScaleData(t.Start, t.Finish, pjAssignmentTimescaledWork, pjTimescaleWeeks)
'get the actual TSV values and store in a variable
Set actualTSVs = a.TimeScaleData(t.Start, t.Finish, pjAssignmentTimescaledActualWork, pjTimescaleWeeks)
'Loop through the total TSVs and try to find and substract the actual values
For Each totalTSV In totalTSVs
'Use this loop to find the actual TSV that has the same start date as the total TSV value we are currently looking at. These values will cover the same period
For Each actualTSV In actualTSVs
If actualTSV.StartDate = totalTSV.StartDate Then
'If the actual value is zero the property returns an empty string, so we have to check for this because we cannot subtract an empty string from a number, it will cause a VBA error.
If actualTSV.Value <> "" Then
'subtract the actual value from the total value to get the remaining
remainingValue = totalTSV.Value - actualTSV.Value
Else
remainingValue = totalTSV.Value
End If
'the Value property of TSV returns in minutes. divide by 60 to get the remaining hours
remainingValue = remainingValue / 60
Exit For
End If
Next actualTSV
'print out the remaining value information
Debug.Print "There are " & remainingValue & " remaining hours of " & a.ResourceName & " on " & t.Name & " between " & totalTSV.StartDate & " to "; totalTSV.EndDate
Next totalTSV
Next a
Next t
End Sub
Here's a sample of what my output looks like:
There are 16 remaining hours of Security Engineer on Create Security Requirements Traceability Matrix (SRTM) between 3/7/2021 to 3/14/2021
I have cell C4 with the value 1:05:20 and I'm trying to create a string TIME 1:05:20 and paste this string into another cell C5 using VBA but am not getting what I need. My code below returns
TIME 4.53703703703704E-02 instead.
Public Sub timer()
Range("C5").Value= "TIME" & " " & Range("C4").Value
End Sub
How can I get the required value?
Assuming it is a proper time you have to format it. To Excel dates and times are just numbers, it's the formatting that makes them look like dates or times. (A time is a fraction of a day, hence your answer.)
Range("C5").Value = "TIME" & " " & Format(Range("C4").Value, "hh:mm:ss")
I am trying to pull all data entries that are within a userform selected month and year. I can get the code to run fine when I hard code the year but I want the year to come off of a text box. I converted the Textbox value to an integer using Cint() and dim'd it to "Year" in my if statement. I can get it to work if I write Cdate("3/1/2016"), but I want see if there is a way to run it like: Cdate("3/1/Year"). I tried it this way and get a typematch error on the Cdate Im pretty new to VBA so excuse my stupidity.
Ignore the "Month" variable I was just using that to put a stop on the code and step it through to see if it would enter my if statement.
Thanks in advance.
My Code
Private Sub OKBtn_Click()
Dim Sales As Range
Dim Year As Integer
Dim Month As Integer
Dim i As Integer
Year = CInt(YearText.Value)
Set Sales = Worksheets("Sales").Range("A4")
i = 0
If Sales.Offset(i, 1).Value >= CDate("3/1/2016") And Sales.Offset(i, 1).Value <= CDate(" 3/31/2016 ") Then
Month = 1
End If
In order for the CDate to work, you need to seperate the stings inside the brackets to 2 parts
1.The constant, in your case "3/1/".
2.And the variable, CInt(YearText.Value).
Option Explicit
Private Sub OKBtn_Click()
Dim DDate As Date
DDate = CDate("3/1/" & CInt(YearText.Value))
' for debug only
MsgBox "Date entered is :" & DDate
End Sub
I am new to coding and programing and would appreciate any suggestions you can give me in creating my first real code.
What I want it to do is ask the user what the Ticker Name is of the stock and once that is input, a message box should display the current price of the stock.
I know the formula to extract the current stock price.
For Example: to find the current value of Netflix all you have to do in a spreadsheet is put "NFLX" in cell(A1) and in cell(A2) put the following formula:
=NUMBERVALUE(WEBSERVICE("http://finance.yahoo.com/d/quotes.csv?s="&A1&"&f=l1"))
Yesterday while learning how to define variables in VBA I was trying to experiment with this concept and came up with the following code which obviously doesn't work:-
Sub Declaring_Variables()
Dim TickerName As String
TickerName = InputBox("Ticker Symbol")
Dim CurrentPrice As Integer
CurrentPrice = Formula ="=NUMBERVALUE(WEBSERVICE("finance.yahoo.com/d/quotes.csv?s="&TickerName&"&f=l1"))
MsgBox (CurrentPrice)
End Sub
I am sure the problem is with the way I have declared the formula for CurrentPrice. I would really appreciate if you can teach me how to use normal formulas which we use on spreadsheet in VBA. I think this will be a good example for me to get better as it covers a very important concept.
Thank you for your time.
Regards,
InWoods
Give this a try:
Option Explicit
Sub Declaring_Variables()
Dim TickerName As String
TickerName = InputBox("Ticker Symbol")
Dim CurrentPrice As String
CurrentPrice = Application.WebService("finance.yahoo.com/d/quotes.csv?s=" & TickerName & "&f=l1")
MsgBox "Text Format: " & CurrentPrice & Chr(10) & _
"Number Format: " & Application.NumberValue(CurrentPrice)
End Sub
I'm getting a type mismatch error from the following code:
blattZFq3.Cells(month, siaw) = Application.WorksheetFunction.CountIfs(Worksheets(i).Range("AF10:AF290"), month, Year(Worksheets(i).Range("AE10:AE290")), minYear)
I'm guessing it's a problem with the second criteria, more specifically the Year function as criteria for a range since the code worked fine in a previous version with just the first criteria and using countif.
minYear is declared as Variant and has been assigned the value of 2012 by a previous function.
Basically I want the cell in the range blattZFq3 to contain the number of times a number matching month occurs in a column, but only if the year of a date in the same row but different column matches minYear.
Does anybody have any suggestions?
Thanks in advance....
You can't do this function to the array: Year(Worksheets(i).Range("AE10:AE290")) as it's expecting a range for the second area to check.
Also, I would avoid using the word Month as a variable name, as it's also the name of a function.
You will have to write the function with 3 criteria to get around the restriction, or write a formula into the target area.
Function with 3 criteria:
blattZFq3.Cells(MyMonth, siaw) = _
WorksheetFunction.CountIfs(Worksheets(i).Range("AF10:AF290"), MyMonth, _
Worksheets(i).Range("AE10:AE290"), ">=" & DateSerial(minYear, 1, 1), _
Worksheets(i).Range("AE10:AE290"), "<=" & DateSerial(minYear, 12, 31))
As a formula into the cell:
blattZFq3.Cells(MyMonth, siaw).Formula = _
"=SUMPRODUCT(--(SheetName!AF10:AF290=" & MyMonth & ")," & _
"--(YEAR(SheetName!AE10:AE290)=" & minYear & "))"
Work with date sometimes is tricky. Are you using english version?
You can try to write the same formula code in Excel and test it before you put it VBA. You can also try something like:
blattZFq3.Cells(month, siaw) = "=CONTIFS(.....)"