Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I posted this last week but the only person who showed an interest
didn't suggest any specific improvements. I was mildly surprised because I would have though this was a common use of Excel VBA. My data is organised in rows of columns (like a database table). I want to look for the first occurrence of a value in a certain column and delete that row. Below is my attempt. In case someone is in the know, I'm trying to implement a simplified version of the SQL DELETE command. Can anyone suggest improvements to my code? Private Function DeleteByKey(ExcelRange As Excel.Range, _ ByVal KeyColumn As Long, _ ByVal KeyValue As Variant) As Boolean Dim vntArray As Variant Dim lngRows As Long Dim lngCounterRows As Long vntArray = ExcelRange.Value lngRows = UBound(vntArray, 1) ' Loop through rows and try to match key For lngCounterRows = 1 To lngRows If vntArray(lngCounterRows, KeyColumn) = KeyValue Then ' Match ExcelRange.Rows(lngCounterRows).Delete xlShiftUp DeleteByKey = True Exit Function End If Next ' No match DeleteByKey = False End Function Public Sub TestDeleteByKey() If DeleteByKey(Range("Data"), 2, "Hip") Then MsgBox "Row Successfully deleted." Else MsgBox "Failed", vbCritical End If End Sub Many thanks. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Repost of find and move values<0 | Excel Worksheet Functions | |||
find and delete | Excel Worksheet Functions | |||
Find and delete | Excel Worksheet Functions | |||
macro to delete lines based on a value - Repost | Excel Discussion (Misc queries) |