Re: Indefinite Number Of Arguments In A Udf
Good deal. Much thanks!
Re: Indefinite Number Of Arguments In A Udf
Good deal. Much thanks!
I would like to build a UDF that takes 2 or more arguements. Sometimes it could be as many as 10 arguments. I would like to avoid using the optional statement. I remember reading a way to pass them in using something like the params method but cant find it again.
Thanks in advance![hr]*[/hr] Auto Merged Post;[dl]*[/dl]Never mind. I just found it in the help. I thought I would check one more time & there it was.
In excel 2003 the search you can use to find the info is: Understanding Parameter Arrays.
Re: Counting Number Of Multiple Entries
Will do.
Re: Counting Number Of Multiple Entries
I have been using this array formula for a while. It finds anything that occurs more than once. The # it returns is the # of times more than once the data occurs.
=SUM(IF($H2=$H$2:$H$500,1,0))-1
$H$2:$H$500 is the array you want to search. $H2 is the line you are on currently. The only way this works is if it is on each line that has a value you want to check for duplicates.
Does anyone know how to use a wildcard in string comparison in a formula rather than in VBA?
Example imaginary formula that does not work:
=if(A1="*set*","ok","not ok")
Re: Copy/paste Formulas Only
As far as your absolute reference question goes you just need a sequence column to be be able to get them back in the original order. Check out the new column.
Re: Copy/paste Formulas Only
Open this workbook to see if it is what you are looking for. Once you sort ascending in the sorter column you can copy and paste your formula update to to just the ones with an "F" beside them.
Re: Copy/paste Formulas Only
I don't know if I am reading this right, but I think I can help you get where you are heading. You want to be able to update just cells that are formulas, right? If so, here is a UDF that I wrote to mark them as formula or value. Create a column for use only as a sorter. Then put the formulaOrNot UDF in it referencing the column you want to update. I sort by the column that I use the UDF in. That allows me to group ones that are formulas with ones that are formulas for the copy / paste update.
Function formulaOrNot(rangeInQuestion As Range)
For Each one In rangeInQuestion
i = i + 1
Next
For Each cell In rangeInQuestion
If cell.Formula Like "=*" Then j = j + 1
Next
If i = j Then formulaOrNot = "F" Else formulaOrNot = "N"
End Function
Display More
It can take either a cell reference or a range reference.
HTH's
Re: Code Does Not Find A Match
Quote from GrumbleDisplay MoreThanks,
It seems to be 'throwing dodgy results' , however it is working some of the time now so a step in the right direction.
Do you think then that this code may unreliable?
If so i'm gonna need to find another way
Thanks again.
The code is definitely solid. I use this type of thing in my main spreadsheet on upwards of 2000 lines of code without problems. The real question is "How is it dodgy?".
Re: Code Does Not Find A Match
Using the cells method without .value or .text will lead to haphazzard results. To tell which on you should use a handy tool is highlighting the full test (like " Cells(theRow, 1) <> Val(TextBox4) ") then hitting shift f9. Then if that is false and you think it should be true highlight each side of the test to see what the value of each part is and why it is throwing an unexpected result.
Mybest guess is you need to update the below:
HTH's.
Re: Code Execution Has Been Interrupted
bump
I keep geting the error msg "Code execution has been interrupted" after I add a do loop to my code. Keep in mind I have about 60 modules in this project and about 40 additions to events. So this is a heavily coded workbook. Also noteworthy is the fact that I can cancel the error by encapsulating the previously working and notibly unrelated code in:
application.enablecancelkey = xldisabled
(my unrelated, previously working code)
application.enablecancelkey = xlinterupt
If that is not enough, the code that is represented by (my unrelated, previously working code even in other subroutines) can change at a moments notice and then I have to encapsulate that to get error to stop.
The error comes up with 4 buttons:
[continue][end][debug][help]
When I hit continue many times the code just finishes it's normal functions. But why does it keep happening. Below is a copy of the module. Look for the "do until" loop...
Sub findVendor()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ChDrive "L"
ChDir "L:\Lift Vendors"
For Each wb In Workbooks
If wb.Name = "Lift Vendor Information.xls" Then
workbookIsAlreadyOpenFlag = 1
End If
Next
If workbookIsAlreadyOpenFlag <> 1 Then
Call Workbooks.Open("Lift Vendor Information.xls", 0)
End If
Set massh = Workbooks("equipment rental.xls").Sheets("em by site")
Set liftVendors = Workbooks("Lift Vendor Information.xls").Sheets("sheet1")
massh.Activate
Dim LVI_zipCodeIndex()
i = 0
For Each LVI_zipCode In liftVendors.Range(liftVendors.Cells(2, 9), liftVendors.Cells(1795, 9))
ReDim Preserve LVI_zipCodeIndex(i)
LVI_zipCodeIndex(i) = LVI_zipCode
i = i + 1
Next
Set zipCodes = massh.Range( _
massh.Cells(massh.Range("start_of_active").Row + 1, massh.Range("Store_Zip").Column), _
massh.Cells(massh.Range("end_of_active").Row - 1, massh.Range("Store_Zip").Column))
massh.Activate
Dim distance As Double
If distance = Empty Then
distance = 50
End If
For Each zipCode In zipCodes
breakLoop = False
Dim aVendorsFound() As locationInformation
ReDim aVendorsFound(0)
Do While _
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = Empty _
And breakLoop = False
If ( _
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value >= 40 _
Or massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = "" _
) _
And LCase(massh.Cells(zipCode.Row, massh.Range("status").Column).value) = "order" _
And massh.Cells(zipCode.Row, massh.Range("potential_vendor").Column).value = "" Then
zipCodesFoundInProximityArray = ZipCodesWithinDistance(zipCode.value, distance)
Dim findCount As Integer
findCount = 0
For i = 1 To UBound(zipCodesFoundInProximityArray)
For j = 0 To UBound(LVI_zipCodeIndex)
If zipCodesFoundInProximityArray(i, 1) = LVI_zipCodeIndex(j) Then
ReDim Preserve aVendorsFound(0 To UBound(aVendorsFound) + 1)
aVendorsFound(UBound(aVendorsFound) - 1).Address = liftVendors.Cells(j + 2, liftVendors.Range("LVI_Address").Column).value
aVendorsFound(UBound(aVendorsFound) - 1).City = liftVendors.Cells(j + 2, liftVendors.Range("LVI_City").Column).value
aVendorsFound(UBound(aVendorsFound) - 1).Company = liftVendors.Cells(j + 2, liftVendors.Range("LVI_Company").Column).value
aVendorsFound(UBound(aVendorsFound) - 1).distToCust = Format(zipCodesFoundInProximityArray(i, 2) * 1.5, 0)
aVendorsFound(UBound(aVendorsFound) - 1).liftVendorID = liftVendors.Cells(j + 2, liftVendors.Range("LVI_WLVID").Column).value
aVendorsFound(UBound(aVendorsFound) - 1).State = liftVendors.Cells(j + 2, liftVendors.Range("LVI_State").Column).value
aVendorsFound(UBound(aVendorsFound) - 1).Phone = liftVendors.Cells(j + 2, liftVendors.Range("LVI_Phone").Column).value
If UBound(aVendorsFound) < 2 Then
massh.Cells(zipCode.Row, massh.Range("potential_vendor").Column).value = _
aVendorsFound(UBound(aVendorsFound) - 1).distToCust & _
" Miles: (" & aVendorsFound(UBound(aVendorsFound) - 1).liftVendorID & ") " & _
aVendorsFound(UBound(aVendorsFound) - 1).Company & _
" " & aVendorsFound(UBound(aVendorsFound) - 1).Phone & " " & _
aVendorsFound(UBound(aVendorsFound) - 1).Address & " " & _
aVendorsFound(UBound(aVendorsFound) - 1).City & ", " & _
aVendorsFound(UBound(aVendorsFound) - 1).State
Else
massh.Cells(zipCode.Row, massh.Range("potential_vendor").Column).value = _
massh.Cells(zipCode.Row, massh.Range("potential_vendor").Column).value & _
Chr(10) & _
aVendorsFound(UBound(aVendorsFound) - 1).distToCust & _
" Miles: (" & aVendorsFound(UBound(aVendorsFound) - 1).liftVendorID & ") " & _
aVendorsFound(UBound(aVendorsFound) - 1).Company & _
" " & aVendorsFound(UBound(aVendorsFound) - 1).Phone & " " & _
aVendorsFound(UBound(aVendorsFound) - 1).Address & " " & _
aVendorsFound(UBound(aVendorsFound) - 1).City & ", " & _
aVendorsFound(UBound(aVendorsFound) - 1).State
End If
End If
Next
Next
If massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = "" _
And massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = "" Then
If aVendorsFound(0).Company = "HTZ" And aVendorsFound(0).distToCust < 30 Then
For j = 0 To UBound(aVendorsFound) - 1
If aVendorsFound(j).Company <> "HTZ" And aVendorsFound(j).distToCust < 30 Then
massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = aVendorsFound(j).liftVendorID
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = aVendorsFound(j).distToCust
Exit For
End If
Next
End If
For i = 0 To UBound(aVendorsFound) - 1
If aVendorsFound(i).Company = "RSC" And aVendorsFound(i).distToCust < 30 Then
massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = aVendorsFound(i).liftVendorID
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = aVendorsFound(i).distToCust
Exit For
End If
Next
End If
If massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = "" _
And massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = "" Then
massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = aVendorsFound(0).liftVendorID
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = aVendorsFound(0).distToCust
End If
If UBound(aVendorsFound) < 1 Then
massh.Cells(zipCode.Row, massh.Range("mileage").Column).value = Empty
massh.Cells(zipCode.Row, massh.Range("lift_vendor_id").Column).value = Empty
distance = distance + 25
End If
findCount = Empty
Else
breakLoop = True
End If
Loop
Next
Set LVI = Workbooks("lift vendor information.xls")
LVI.Close savechanges:=True
Set LVI = Nothing
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Display More
Re: User Defined Events?
Ur right. I read that copy / paste do not fire worksheet_change after it did not work in my master spreadsheet. After reading your reply showing it does work in xl2003 I tried it in my original test spreadsheet. The event fires there. The funny thing is that in my master spreadsheet it is firing all except the copies / pastes and undo redo. I think I'll reboot and see if anything changes.
Re: User Defined Events?
It does not capture pastes in Excel 2003. I think it might in Excel 2000.
Can you create user-defined events in excel? I ask because the worksheet_change event does not capture pastes or undo/redo.
Re: To Get Fines To Automatically Update
If you could post a sample workbook, that would help us understand the problem tremendously.
Re: If Statement Causing Loop To Fail
There is a way to work with merged ranges that is easier than other ways if you don't have a choice; you name them. (See Excel 2003 help topic "Define named cell references or ranges" > "Name a cell or a range of cells" for exact instructions.)
Once named you can access the cell's value via the below code.
You can set the cell value and a host of other things equal to what ever you want. No matter where you move the cell or anything else in the workbook, excel keeps up with it's location and updates it however you want.
Though, beware. The admins on this site do not like a thread to morph. With a new topic, it is best to start a new thread.
Re: If Statement Causing Loop To Fail
Exactly. And there are no hard and set rules. You will find what works best for you as time goes along, but what you have outlined will definitely help your future coding efficiency 10 fold.
Re: Mappoint 2006 Vba Support
I don't have access to MapPoint 2006 and Excel 2003 and wonder if the below code works? I am on a bit of a time crunch.
If you don't have MapPoint 2006, here's the link.
(It's a gig download)
Below is a reference work I found online:
QuoteDisplay MoreAutomating MapPoint
In Automation, User Defined Functions, Excel Experts E-Letter by Dick Kusleika
EEE#21: Excel User Defined Functions That Incorporate MapPoint Functionality
By David Hager
---Note: Excel 2003 and MapPoint 2004 were used in the development of this technique and its use with other versions has not been tested.---
The integration that Microsoft provides between Excel and MapPoint for importing and exporting data provides a powerful method for visual data analysis. As it turns out, there is another way to obtain information from MapPoint to Excel that is not as well-known.
User-defined functions (UDF) are used in Excel worksheets is the same way that native Excel functions are used. The VBA code for an Excel UDF is written in a standard module in the Excel VBE and called by entering the function in a formula in a worksheet cell. In order to incorporate MapPoint automation in an Excel UDF, open the Visual Basic Environment (VBE) by using the Alt-F11 keys. Then, under Tools|References check the Microsoft MapPoint 11.0 Object Library (North America).
A simple example of an Excel UDF that incorporates MapPoint functionality is shown below.CodeFunction StraightLineDist(strPoint1, strPoint2) Dim objApp As New MapPoint.Application Set objMap = objApp.ActiveMap Set objLocate1 = objMap.FindResults(strPoint1).Item(1) Set objLocate2 = objMap.FindResults(strPoint2).Item(1) StraightLineDist = Application.Round(CStr(objLocate1.DistanceTo(objLocate2)), 5) End Function
It is quite rare to find an example where the VBA code for an Excel UDF successfully instantiates an application. In this case, an instance of MapPoint is created with the statement
Dim objApp As New MapPoint.Application
that allows the Excel UDF access to a valuable source of location information.
The FindResults method works in the same way that the Place/Data tab in the dialog box opened from Edit|Find of the MapPoint main menu does. The Item(1) property returns the best choice based on the available information. As an example, type the following in an Excel worksheet.
A1: Beaumont, Tx
A2: Houston, Tx
A3: =StraightLineDist(A1, A2)
The result returned in cell A3 is 77.11092, which is the number of miles as the crow flies between Beaumont and Houston Texas as calculated by the DistanceTo method.
In order to improve performance of the StraightLineDist function, the statement
can be used in the code to limit function calls to only those times when the input data is changed. It might be also advantageous to set the MapPoint objects to nothing in order to remove them from memory.
The next example returns the distance for a multiple point route and contains an element that is perhaps unique among UDF?s.CodeDisplay MoreFunction MPRouteDist(iMPType As Integer, ParamArray WPoints()) Dim objApp As New MapPoint.Application Set objMap = objApp.ActiveMap With objMap.ActiveRoute For Each wpoint In WPoints .Waypoints.Add objMap.FindResults(wpoint).Item(1) Next .Waypoints.Item(1).SegmentPreferences = iMPType .Calculate MPRouteDist = Application.Round(CStr(.Distance), 5) End With objMap.Saved = True End Function
The iMPType variable is the type of route to be returned:
0 = Travel using the quickest route
1 = Travel using the shortest route
2 = Travel on preferred roads as set in the Preferred Roads dialog box
The ParamArray WPoints() statement allows for an arbitrary number of waypoint function arguments along the route.
By entering the following formula, the distance for the quickest round trip route from Houston is returned (882.89018 miles).
Cell formula:[where:
A1: Houston, Tx
A2: Dallas, Tx
A3: San Antonio, Tx
A4: Corpus Christi, Tx
This function actually makes changes on the active MapPoint map (not visible) and a prompt appears asking to save those changes unless the statement
is included in the code. This writer does not know of another example where an Excel UDF modifies another application. This unusual behavior serendipitously provides a simple way to generate route maps from Excel. If the following formula is entered without the objMap.Saved = True statement in the code, the route map from Houston to Dallas is generated and can be saved. Thus, generation of route maps from MapPoint is now as easy as entering an address in a worksheet cell.
Cell formula:
In summary, Excel UDFs provide easy access to the rich store of information in MapPoint and these examples just scratch the surface of what is possible. For example, I have made Excel UDFs that draw geometric shapes around MapPoint locations. This and other challenges are left to the reader.
Any help anyone can give me would be great!