I can't make this work
Dim StrtD As Long, EndD As Long
Dim StartDate As Date, EndDate As Date
Dim myVar As Variant
Dim stringaAppoggio As String
With ThisWorkbook.Sheets("Settimana")
StartDate = .TextBox1.Value
EndDate = .TextBox2.Value
End With
StrtD = Month(StartDate)
EndD = StrtD + DateDiff("m", StartDate, EndDate)
yearData = year(StartDate)
arr4 = Application.Transpose(.Evaluate("TEXT(DATE(yearData,ROW(" & StrtD & ":" & EndD & "),1), ""[$-0410]mmmm yyyy"")"))
For Each myVar In arr4
stringaAppoggio = myVar
StartDate is "01/01/2020"
EndDate is "01/10/2020"
The error is stringaAppoggio = myVar
type mismatch
I think the error is yearData in Application.Transpose because if I put 2020 it works!
Thank you
yeardata variable is not evaluated passed correctly to your string formula. You need to concatenate it directly into your formula string like what you did in StrtD and EndD variables. Try:
arr4 = Application.Transpose(.Evaluate("TEXT(DATE(" & yearData & _
",ROW(" & StrtD & ":" & EndD & "),1), ""[$-0410]mmmm yyyy"")"))
There are some things going wrong for you here:
Your code should not even compile in the first place, because (with the information you have shown) Application.Transpose(.Evaluate holds a unqualified .Evaluate method.
As I told you yesterday, Debug.Print is a great way of checking if what you intended to write, actually gets written in the .Evaluate. Currently you are using yeardata wrong within the formula. It's a variable holding a string value! Don't get confused with thinking that writing this word within another string automatically picks up you meant to use the variable.
To TRANSPOSE values from a worksheet into a 1D-array, you can do so directly through your formula.
I just assume your code sample above isn't complete and includes a Next below stringaAppoggio = myVar
Considering the above, I think your code can be rewritten to something like:
arr4 = Evaluate("TRANSPOSE(TEXT(DATE(" & yearData & ",ROW(" & StrtD & ":" & EndD & "),1), ""[$-0410]mmmm yyyy""))")
Related
I am having a problem with a particular line of code:
ActiveSheet.Range("A" & rowCount & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount)
Where alphabet is a string containing uppercase letters A to Z.
I keep getting the following error:
Run-time error '5':
Invalid Procedure call or argument
I tried creating a String "inRange" and changing the code to this:
inRange = "A" & rowCount & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount
curRange = ActiveSheet.Range(inRange)
But that did not help (as I thought it wouldn't). Any suggestions?
Although creating ranges like this is frowned upon in general, the way to do it is with the word SET (like #Gary McGill stated in the comments). Here is an example of how to do this:
Sub test()
Dim alphabet As String
Dim totHrdrLngth As Long
Dim belowRowCount As Long
Dim rowCount As Long
Dim inRange As Range
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
totHrdrLngth = 5
belowRowCount = 10
rowCount = 5
' Gives us A5:E10
Set inRange = Range("A" & rowCount & ":" & range2 & _
Mid$(alphabet, totHrdrLngth, 1) & belowRowCount)
End Sub
You are running this macro in the current range, so there should be no need to specify ActiveSheet.Range. I hope this helps get you toward what you are trying to achieve.
As far as I can tell, you're getting an error because your types don't match up. I imagine rowCount is an integer, as is belowRowCount. If you convert them to strings before concatenating them, you can fix it. str() will convert an integer to a string with a space before it, and LTrim() will remove the space. Try code as below:
Dim sRowCount As String
Dim sBelowRowCount As String
and later
sRowCount = LTrim(Str(RowCount))
sBelowRowCount = LTrim(Str(belowRowCount))
inRange = "A" & sRowCount & ":" & Mid(alphabet, totHdrLngth, 1) & sBelowRowCount
curRange = ActiveSheet.Range(inRange)
Hope this helps.
I am new to Macros below is what I wrote to be able to do a lookup through a function. When I debug all the values seem to work well except that at the end it gives error about circular reference.
I am making a call to below like: = GetCost("abc")
Public Function GetCost(arg1 As String)
'
' GetCost Macro
'
Dim fName As String
Dim searchItem As String
Dim result As Integer
fName = Range("B4").Value
searchItem = Chr(34) & arg1 & Chr(34)
expression = "=VLOOKUP(" & searchItem & "," & fName & ", 2, FALSE)"
ActiveCell.Value = ActiveSheet.Evaluate(expression)
result = Evaluate(expression)
GetBOMCOst = result
End Function
The circular reference is because you are trying to change the value of the cell containing the function:
remove ActiveCell.Value = ...
change result= Evaluate to result =application.caller.parent.Evaluate(expression)
Also fName=Range("B4").value should use a fully qualified reference (Sheet.Range)
And this function should be made volatile otherwise it will not recalculate whenever the value in Range("B4") changes
You need to remove your active cell reference as suggested by other.
Also, its looks like, your lookup range is incorrect. Change the lookup expression as below, in case, if you have your value in A and B column.
expression = "=VLOOKUP(" & searchItem & "," & "A:B" & ", 2, FALSE)"
And, it looks like you trying to return the value to your calling function. In VBA, to return a variable, you need to assign the variable to a function name. i.e. change your last line GetBOMCOst = result to GetCost = result
Try the below updated version of the code.
Public Function GetCost(arg1 As String) ' ' GetCost Macro ' Dim fName As String Dim searchItem As String Dim result As Integer
searchItem = Chr(34) & arg1 & Chr(34)
expression = "=VLOOKUP(" & searchItem & "," & "A:B" & ", 2, FALSE)"
result = Evaluate(expression)
GetCost = result
End Function
Hi I would like to put into a STRING the following formula
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio="=DAY(EOMONTH(DATEVALUE(01-'&myMonth&'-&YEAR(myMonth)),0))"
It doesn't really give me a syntax error, but I don't see the result of the formula in the String
Thank you
Well, as per my comment, there are a few mistakes here:
You have used single quotes instead of double quotes to seperate your variable from VBA formula syntax
You have forgotten the quotes at all around your second myMonth variable
You have created a formula that simply won't work
Keep in mind, your variable is not just a month but a string holding a month and year > "January 2020", therefor DATEVALUE won't need the 01- and YEAR(myMonth) to work. Let me explain:
=DATEVALUE("January 2020")
Will return Integer 43831, or in other words: 1-1-2020. Then secondly, EOMONTH will return the end of that same month as an Integer, whereas DAY will return the number of that day. So your formula would read:
=DAY(EOMONTH(DATEVALUE("January 2020"),0))
Now to write this in VBA:
Dim stringAppoggio As String
Dim myMonth As String
myMonth = "January 2020"
stringAppoggio = "=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))"
You can check that it works:
Debug.Print Evaluate("=DAY(EOMONTH(DATEVALUE(""" & myMonth & """),0))")
Note: See the triple quotes? That's because we need to feed DATEVALUE a string within quotes to work, otherwise it wouldn't be a string and will return an error
Dim stringAppoggio As String
Dim myMonth As String
myMonth = Chr(34) & "January 2020" & Chr(34)
stringAppoggio = "=DAY(EDATE(" & myMonth & ",1)-DAY(" & myMonth & "))"
I have to find the number of working days between two dates which should exclude weekends and National Holidays.
I am using function NETWORKDAYS in vba, this excludes weekends but I want to exclude Some National Holidays as well.
How to use this function NETWORKDAYS(startDate, endDate, [holidays]) for National holidays. It says it accepts [holidays] as a list. I have all the national holidays in an array. how can I use it with this function via VBA ??
Please find the code snippet.
Public Function dataFromInputSheetHolidayDates() As Variant
Dim holidayDates As Integer
Dim holidaydatesArray(20) As Variant
holidayDates = Sheets("InputSheet").Cells(Rows.Count, "D").End(xlUp).Row
For countDate = 0 To holidayDates - 1
holidaydatesArray(countDate) = Format(Sheets("InputSheet").Cells(countDate + 2, "D").Value, "DD-MMM-YY")
Next countDate
dataFromInputSheetHolidayDates = holidaydatesArray
End Function
holidayList = dataFromInputSheetHolidayDates()
Sheets("Estimation").Range("Z" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",Y" & taskcounter &","& holidayList & ")"
Sheets("Estimation").Range("AB" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",AA" & taskcounter &"," & holidayList & ")"
Change these 2 lines in your code:
Dim holidayDates As Long
holidaydatesArray(countDate) = Sheets("InputSheet").Cells(countDate + 2, "D")
In VBA Integer is up to 32767, which is not quite enough for dates. Furthermore, holidaydatesArray should have a numeric value and not some text format.
Pretty much similar problem as this one - workday holiday argument doesn't accept array
If you are trying to create a flexible formula through VBA, where the holidays are on different worksheet, try this solution:
Public Sub TestMe()
Dim holidayLists As Range
Set holidayLists = Worksheets(2).Range("D1:D10")
With Worksheets(1)
.Range("A1").Formula = "=NETWORKDAYS(B1, C1," & holidayLists.Parent.Name & "!" _
& holidayLists.Address & ")"
End With
End Sub
There the holidayLists.Parent.Name & "!" & holidayLists.Address would refer correctly to the worksheet's name of the holidays.
I've seen several questions asked around using INDEX MATCH on multiple criteria within VBA, but they all seem to revolve around pasting a formula into a cell.
I'm looping thru a ListObject and trying to do a lookup on 3 criteria on a diff sheet, but I want the value pasted into the cell not the formula. I've tried using various combos of Application.Index, Application.WorksheetFunction.Index, Application.Match, Application.WorksheetFunction.Match and Evaluate() but I'm still getting #Value! (when I don't get a an Error). Some of what I've tried below (it's prob a very simple mistake I'm making).
wsSrc has the following ranges as part of a ListObject and I want to paste the result onto wsDest.
rngDate = Range("Table24[date]") 'Date
rngFrom = Range("Table24[from]") 'String
rngTo = Range("Table24[to]") 'String
rngLookup = Range("Table24[lookup]") 'Double
Based on a combination of a Date, From and To I want to lookup a value in rngLookup.
I've tried:
Application.Index(rngLookup.Address, _
Application.Match(strDate & "USD" & strTo, _
rngDate.Address & rngFrom.Address & rngTo.Address))
I've also tried:
x = wsSrc.Evaluate("INDEX(" & rngLookup.Address & _
",MATCH(" & strDate & "USD" & strTo & "," & _
rngDate.Address & "&" & rngFrom.Address & "&" & rngTo.Address)
I've even tried converting the date using CStr(CDate()) which works on in Excel, but not in the VBA.
No joy on either, any thoughts? Again, to confirm I want just the value not the formula pasted into a cell.
This worked for me using a table and columns of the same name:
Dim sht As Worksheet, dt, sFrom, sTo
Set sht = ActiveSheet ' e.g.
dt = DateSerial(2017, 12, 18)
sFrom = "B"
sTo = "C"
Debug.Print sht.Evaluate( _
"INDEX(Table24[lookup],MATCH(1," & _
"(Table24[date]=" & (dt * 1) & ")*" & _
"(Table24[from]=""" & sFrom & """)*" & _
"(Table24[to]=""" & sTo & """),0))")
Assumes your table's dates are actual dates and not strings.