Sending excel objects to subroutines - vb.net

As it is right now I have a bit of code that kind of looks like this (a little paraphrased but Im sure you get the idea)
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
xlWorkSheet202.Activate()
xlWorkSheet202.Cells((AT + 2), 3) = TextBox1.Text
xlWorkSheet202.Cells((AT + 3), 2) = "PSS (kBs)"
xlWorkSheet202.Cells((AT + 3), 3) = "USS (kBs)"
xlWorkSheet202.Cells((AT + 3), 4) = "User %"
xlWorkSheet202.Cells((AT + 3), 5) = "Kernel %"
xlWorkSheet202.Cells((AT + 3), 6) = "Total %"
xlWorkSheet202.Cells((AT + 4), 1) = "Min:"
xlWorkSheet202.Cells((AT + 5), 1) = "Max:"
xlWorkSheet202.Cells((AT + 6), 1) = "Average:"
xlWorkSheet202.Cells((AT + 7), 1) = "Median:"
xlWorkSheet202.Cells((AT + 8), 1) = "Stan Dev:"
ElseIf ComboBox3.SelectedIndex = 2 Then
xlWorkSheet204.Cells((WT + 2), 3) = TextBox1.Text
xlWorkSheet204.Cells((WT + 3), 2) = "PSS (kBs)"
xlWorkSheet204.Cells((WT + 3), 3) = "USS (kBs)"
xlWorkSheet204.Cells((WT + 3), 4) = "User %"
xlWorkSheet204.Cells((WT + 3), 5) = "Kernel %"
xlWorkSheet204.Cells((WT + 3), 6) = "Total %"
xlWorkSheet204.Cells((WT + 4), 1) = "Min:"
xlWorkSheet204.Cells((WT + 5), 1) = "Max:"
xlWorkSheet204.Cells((WT + 6), 1) = "Average:"
xlWorkSheet204.Cells((WT + 7), 1) = "Median:"
xlWorkSheet204.Cells((WT + 8), 1) = "Stan Dev:"
This goes on 3 more times in a few different places... So now I am trying to refactor the code to make it cleaner and shorter.
What I would like to do is this:
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
Excelupdate(xlWorkSheet203, AT)
ElseIf ComboBox3.SelectedIndex = 2 Then
Excelupdate(xlWorkSheet204, WT)
Private sub ExcelUpdate(byref worksheet as object, byref update as string)
worksheet.Activate()
worksheet.Cells((update + 2), 3) = TextBox1.Text
worksheet.Cells((update + 3), 2) = "PSS (kBs)"
worksheet.Cells((update + 3), 3) = "USS (kBs)"
worksheet.Cells((update + 3), 4) = "User %"
worksheet.Cells((update + 3), 5) = "Kernel %"
worksheet.Cells((update + 3), 6) = "Total %"
worksheet.Cells((update + 4), 1) = "Min:"
worksheet.Cells((update + 5), 1) = "Max:"
worksheet.Cells((update + 6), 1) = "Average:"
worksheet.Cells((update + 7), 1) = "Median:"
worksheet.Cells((update + 8), 1) = "Stan Dev:"
end sub
I thought for sure the above would work but it still seems that I am missing something, when I open the excel sheet nothing was printed. This would cut down the lines of code that I have in half easily, so I would love to find a solution for this
Thanks Guys
.......................................................
Edit (Sorry those comment boxes are terrible for writing anything)
.......................................................
alright I tried changing these lines of code:
If ComboBox2.SelectedIndex = 1 Then
If ComboBox3.SelectedIndex = 0 Then
ExcelUpdate(xlWorkSheet202, AT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 1 Then
ExcelUpdate(xlWorkSheet203, GT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 2 Then
ExcelUpdate(xlWorkSheet204, WT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 3 Then
ExcelUpdate(xlWorkSheet205, OT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
End If
End If
Private Sub ExcelUpdate(ByVal Sheet As Object, ByVal update As Integer, ByVal pval As Double, ByVal uval As Double, ByVal user As Double, ByVal kernel As Double)
update = update + 1
Sheet.cells(update, 1) = timenow
Sheet.cells(update, 2) = pval
Sheet.cells(update, 3) = uval
Sheet.cells(update, 4) = user
Sheet.cells(update, 5) = kernel
Sheet.cells(update, 6) = cdbl(kernel + User)
end sub
But the excel sheets still do not update with the new information. Is there anything else im missing?

I would check/change a couple of things:
1) Change the ByRefs in the function to ByVal. You don't need to update the reference to the worksheet or modify the string, so ByRef is not need.
2) Determine the data type of the update parameter. You are mixing operation and types, which could result in an incorrect cells reference.
If the goal of the cell reference is:
worksheet.Cells(("A2"), 3)
then you should change your code to:
worksheet.Cells((update & "2"), 3)
If the goal of the cell reference is:
worksheet.Cells((12), 3)
then you should change the update parameter type:
update as integer

Related

VBA Format Number With Leading Zero on save files

I have the below code that works fine, however i am trying to amend th code to no avail so that it saves the files with the leading Zeros.
the number element are store numbers and it ranges from 1 - 168
ideally if possible can you advise how do i change the code so it saves the output files like the below example if a store number is 2 digits and the 3 digits etc.
0001
0010
0120
Sub GenerateOutput()
Dim i As Long
Dim iGradeRow As Long
Dim iGradeCol As Long
Dim iPosSeqRow As Long
Dim s(1 To 7) As String
Dim aGradeData() As Variant
Dim aPosSeq() As Variant
Dim aOutput(1 To 500000, 1 To 12) As Variant
Dim iNextOutputRow As Long
Dim ExportWorkbook As Workbook
Dim Site As String
Dim Department As String
Dim Category As String
Dim ArticleGrade As String
Dim dp As String
Dim ct As String
Dim posQty As Long
Dim y As Long
Dim lrStores As Long
Dim recordId As Long
Dim selId As Long
'------------------------
Application.ScreenUpdating = False
' Get arrays of data to loop round
With ws_Grades
aGradeData = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column).Value2
End With
With ws_PosSeq
aPosSeq = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 20).Value2
End With
s(1) = "( "
's(2) = iGradeRow - 3
s(3) = " / "
's(4) = UBound(aGradeData, 1) - 3
s(5) = " ) "
's(6) = "Collecting data for: "
's(7) = aGradeData(iGradeRow, 2)
'Application.StatusBar = Join(s)
'DoEvents: DoEvents
'check the departments and categories
For iGradeRow = 4 To UBound(aGradeData, 1)
's(1) = "( "
s(2) = iGradeRow - 3
's(3) = " / "
s(4) = UBound(aGradeData, 1) - 3
's(5) = " ) "
s(6) = "Collecting data for: "
s(7) = aGradeData(iGradeRow, 2)
Application.StatusBar = Join(s)
DoEvents: DoEvents
Application.ScreenUpdating = False
Erase aOutput
iNextOutputRow = 1
For iGradeCol = 3 To UBound(aGradeData, 2)
Site = aGradeData(iGradeRow, 1)
Department = aGradeData(1, iGradeCol)
Category = aGradeData(3, iGradeCol)
ArticleGrade = aGradeData(iGradeRow, iGradeCol)
If iNextOutputRow = 1 Then
recordId = 1
selId = 1
Else
recordId = aOutput(iNextOutputRow - 1, 1) + 1
selId = aOutput(iNextOutputRow - 1, 2) + 1
End If
'check the departments & categories in the opened workbook
For iPosSeqRow = 3 To UBound(aPosSeq, 1)
'if there is nil in the first column, go to the next loop
If aPosSeq(iPosSeqRow, 1) = 0 Then GoTo NextDepartment
'if the department name and category name matches:
If (Trim(LCase(aPosSeq(iPosSeqRow, 2))) = Trim(LCase(Department))) And (Trim(LCase(aPosSeq(iPosSeqRow, 3))) = Trim(LCase(Category))) Then
dp = aPosSeq(iPosSeqRow, 2)
ct = aPosSeq(iPosSeqRow, 3)
'check wether the grades match:
If Not Trim(LCase(aPosSeq(iPosSeqRow, 6))) = Trim(LCase(ArticleGrade)) Then GoTo NextValue
'check pos qty:
posQty = aPosSeq(iPosSeqRow, 12)
'check department: same like the last one?:
If Not iNextOutputRow = 1 Then
If Trim(LCase(aOutput(iNextOutputRow - 2, 7))) = Trim(LCase(Site)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 5))) = Trim(LCase(dp)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 6))) = Trim(LCase(ct)) Then GoTo Level3
If Trim(LCase(aOutput(iNextOutputRow - 2, 7))) = Trim(LCase(Site)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 5))) = Trim(LCase(dp)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 6))) <> Trim(LCase(ct)) Then GoTo Level2
If Trim(LCase(aOutput(iNextOutputRow - 2, 7))) = Trim(LCase(Site)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 5))) <> Trim(LCase(dp)) And _
Trim(LCase(aOutput(iNextOutputRow - 2, 6))) <> Trim(LCase(ct)) Then GoTo Level2
End If
Level1:
' Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
' SEL_ID
aOutput(iNextOutputRow, 2) = selId
' Front + Back
aOutput(iNextOutputRow, 3) = "F"
' Template_Type
aOutput(iNextOutputRow, 4) = "Store"
' Store No
aOutput(iNextOutputRow, 7) = Site
iNextOutputRow = iNextOutputRow + 1
' Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
' SEL_ID
aOutput(iNextOutputRow, 2) = selId
' Back
aOutput(iNextOutputRow, 3) = "B"
' Template_Type
aOutput(iNextOutputRow, 4) = "Store"
' Store No
aOutput(iNextOutputRow, 7) = Site
iNextOutputRow = iNextOutputRow + 1
Level2:
'Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
'SEL_ID
aOutput(iNextOutputRow, 2) = aOutput(iNextOutputRow - 1, 2) + 1
'Front_Back
aOutput(iNextOutputRow, 3) = "F"
'Template_Type
aOutput(iNextOutputRow, 4) = "Category"
'Department
aOutput(iNextOutputRow, 5) = dp
'Category
aOutput(iNextOutputRow, 6) = ct
'Store No
aOutput(iNextOutputRow, 7) = Site
iNextOutputRow = iNextOutputRow + 1
'Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
'SEL_ID
aOutput(iNextOutputRow, 2) = aOutput(iNextOutputRow - 1, 2)
'Front_Back
aOutput(iNextOutputRow, 3) = "B"
'Template_Type
aOutput(iNextOutputRow, 4) = "Category"
'Department
aOutput(iNextOutputRow, 5) = dp
'Category
aOutput(iNextOutputRow, 6) = ct
'Store No
aOutput(iNextOutputRow, 7) = Site
iNextOutputRow = iNextOutputRow + 1
Level3:
For i = 1 To posQty
'Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
'SEL_ID
If i = 1 Then
aOutput(iNextOutputRow, 2) = aOutput(iNextOutputRow - 1, 2) + 1
Else
aOutput(iNextOutputRow, 2) = aOutput(iNextOutputRow - 1, 2)
End If
'Front_Back
aOutput(iNextOutputRow, 3) = "F"
'Template_Type
aOutput(iNextOutputRow, 4) = "SEL"
'Department
aOutput(iNextOutputRow, 5) = dp
'Category
aOutput(iNextOutputRow, 6) = ct
'Store No
aOutput(iNextOutputRow, 7) = Site
'Barcode No
aOutput(iNextOutputRow, 8) = aPosSeq(iPosSeqRow, 8)
'Article Description
aOutput(iNextOutputRow, 9) = aPosSeq(iPosSeqRow, 7)
'WasWas
aOutput(iNextOutputRow, 10) = aPosSeq(iPosSeqRow, 13)
'Was
aOutput(iNextOutputRow, 11) = aPosSeq(iPosSeqRow, 14)
'Now
aOutput(iNextOutputRow, 12) = aPosSeq(iPosSeqRow, 16)
iNextOutputRow = iNextOutputRow + 1
'Record Id
aOutput(iNextOutputRow, 1) = iNextOutputRow
'SEL_ID
aOutput(iNextOutputRow, 2) = aOutput(iNextOutputRow - 1, 2)
'Front_Back
aOutput(iNextOutputRow, 3) = "B"
'Template_Type
aOutput(iNextOutputRow, 4) = "SEL"
'Department
aOutput(iNextOutputRow, 5) = dp
'Category
aOutput(iNextOutputRow, 6) = ct
'Store No
aOutput(iNextOutputRow, 7) = Site
'Barcode No
aOutput(iNextOutputRow, 8) = aPosSeq(iPosSeqRow, 8)
'Article Description
aOutput(iNextOutputRow, 9) = aPosSeq(iPosSeqRow, 7)
iNextOutputRow = iNextOutputRow + 1
Next i
End If
NextValue:
Next iPosSeqRow
NextDepartment:
Next iGradeCol
's(1) = "( "
's(2) = iGradeRow - 3
's(3) = " / "
's(4) = UBound(aGradeData, 1) - 3
's(5) = " ) "
s(6) = "Generating export for: "
's(7) = aGradeData(iGradeRow, 2)
Application.StatusBar = Join(s)
DoEvents: DoEvents
Application.ScreenUpdating = False
' Clean output data
For i = 1 To iNextOutputRow
aOutput(i, 1) = Format(aOutput(i, 1), "0000000")
aOutput(i, 2) = Format(aOutput(i, 2), "0000000")
aOutput(i, 7) = Format(aOutput(i, 7), "0000")
aOutput(i, 8) = "'" & aOutput(i, 8)
Next i
ws_Output.Cells(2, 1).Resize(ws_Output.UsedRange.Rows.Count, 12).ClearContents
ws_Output.Cells(2, 1).Resize(iNextOutputRow, 12).Value2 = aOutput
Application.ScreenUpdating = False
If ExportWorkbook Is Nothing Then
Set ExportWorkbook = Workbooks.Add
ThisWorkbook.Activate
End If
Application.ScreenUpdating = False
ExportWorkbook.Worksheets(1).Cells.Clear
ws_Output.UsedRange.Copy
ExportWorkbook.Worksheets(1).Cells(1, 1).PasteSpecial xlPasteAll
Application.CutCopyMode = False
ExportWorkbook.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & aGradeData(iGradeRow, 1) & "_" & aGradeData(iGradeRow, 2) & "_" & Format(Now(), "ddmmyyyy_hhmm") & ".xlsx"
ws_Output.Cells(2, 1).Resize(ws_Output.UsedRange.Rows.Count, 12).ClearContents
Next iGradeRow
EndingSub:
ExportWorkbook.Close False
Set ExportWorkbook = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox "Generated Workbooks.", vbInformation
End Sub
Appent 3 zeros to the left of the number element and use
Right() to get exact 4 digits
You should not post your entire code, but rather the relevant parts of it only, so that noone needs to search through the code, to find the importand parts.
To your question:
To fill your numbers with leading zeros you could do something like the following:
Sub test()
Dim numLen As Integer
Dim i As Integer
Dim test As String
numLen = 4 '4 is the lengh like in your example `0001`
'test = "1"
'test = "11" 'Some numbers to test the code
test = "111"
'Depending on the Lenght of the String, additional leading zeros will be added
For i = Len(test) To numLen - 1
test = "0" & test
Next
MsgBox (test)
End Sub

My counting variable will not increment beyond the value of 1 vba [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My apologies for the confusion. Here goes an attempt to clarify:
Pseudo code:
For each item in list one (i) --> set counting variable to zero
Check each item in list two (j)
If list one item matches list two item then increment counter AND store value
If count == 1 then do stuff (list one item found in list two ONE time)
If count ==2 then do other stuff (list one item found in list two TWO times)
Next item
My problem is that the "ticker" variable gets incremented inside of the equation, but the value of the variable never changes from 1 to 2. Thus, the statement "If count = 2" never fires. My picture shows where I have broken the code at each iteration of the j For loop to monitor the value of the "ticker" variable. On the iteration when the "ticker" should change from 1 to 2 the equation inside of the IF statement shows that it is incremented, but the variable's value, shown in the locals window, does not change.
Code:
For i = LBound(SAPanArray) To UBound(SAPanArray)
anString = Trim(Split(SAPanArray(i), " ")(0))
ticker = 0
Set a = FindNextEmpty(Sheets("SAPdata").Range("F4"))
If IsInArrayC(anString, SAPanArray) = 0 Then
Sheets("SAPdata").Cells((a.Row), (a.Column)).Value = Sheets("SAPdata").Cells((i + 3), 9).Value
Sheets("SAPdata").Cells((a.Row), 7).Value = (Sheets("SAPdata").Cells((i + 3), 10).Value) * 2000
Sheets("SAPdata").Cells((a.Row), 7).Interior.Color = RGB(255, 192, 0)
Else
For j = LBound(SAPbulkArray) To UBound(SAPbulkArray)
SAPbulkArray = WorksheetFunction.Transpose(Sheets("SAPdata").Range("F4:F" & (Sheets("SAPdata").Range("F" & Cells.Rows.Count).End(xlUp).Row)))
If InStr((SAPbulkArray(j)), anString) > 0 Then
ticker = (1 + ticker)
dupArray(ticker) = Sheets("SAPdata").Cells((j + 3), 7).Value
End If
If ticker = 1 Then
If ((Sheets("SAPdata").Cells((i + 3), 10).Value) * 2000) <> (Sheets("SAPdata").Cells((j + 3), 7).Value) Then
Sheets("SAPdata").Cells((a.Row), (a.Column)).Value = Sheets("SAPdata").Cells((i + 3), 9).Value
Sheets("SAPdata").Cells((a.Row), 7).Value = (Sheets("SAPdata").Cells((i + 3), 10).Value) * 2000
Sheets("SAPdata").Cells((a.Row), 7).Interior.Color = RGB(255, 192, 0)
End If
ElseIf ticker = 2 Then
summer = (dupArray(1)) + ((Sheets("SAPdata").Cells((i + 3), 10).Value) * 2000)
If summer <> dupArray(2) Then
If Not IsEmpty(Sheets("SAPdata").Cells((j + 3), 11)) Then
Sheets("SAPdata").Cells((j + 3), 7).Value = Sheets("SAPdata").Cells((i + 3), 11).Value
End If
Sheets("SAPdata").Cells((i + 3), 11).Value = Sheets("SAPdata").Cells((j + 3), 7).Value
Sheets("SAPdata").Cells((i + 3), 11).Font.Color = vbWhite
Sheets("SAPdata").Cells((j + 3), 7).Value = ((Sheets("SAPdata").Cells((i + 3), 10).Value) * 2000) + Sheets("SAPdata").Cells((j + 3), 7).Value
Sheets("SAPdata").Cells((j + 3), 7).Interior.Color = RGB(255, 192, 0)
End If
End If
Next
End If
Next
For i = LBound(SAPanArray) To UBound(SAPanArray)
anString = Trim(Split(SAPanArray(i), " ")(0))
ticker = 0
Set a = FindNextEmpty(Sheets("SAPdata").Range("F4"))
If IsInArrayC(anString, SAPanArray) = 0 Then
Else
For j = LBound(SAPbulkArray) To UBound(SAPbulkArray)
If InStr((SAPbulkArray(j)), anString) > 0 Then
ticker = (1 + ticker)
End If
If ticker = 1 Then
ElseIf ticker = 2 Then
OK Forgive me for hack/slashing everything but this helps me read your logical order.
Ticker only really evers needs to be 1 or 0 due to your line of reasoning. The
ElseIf ticker =2
Should be
Elseif ticker = 0
OR
Else
'some more code
So yoyu could save yourself the the trouble UNLESS ticker needs to be greater than 1 ever. So far by what you posted it doesnt need to be.

How to merge a number of loops into one in a VBA program?

I am fairly inexperienced with VBA, and I can't figure out how to make this loop. I set up 4 separate statements and it works this way, but I want to make this one statement.
i = 1
Do Until i > combos
Range(Cells(i, 10), Cells(i + Defrepeat - 1, 10)) = Range(Cells(3, 4), Cells(3, 4))
i = i + TErepeat
Loop
w = 4
Do Until w > combos
Range(Cells(w, 10), Cells(w + Defrepeat - 1, 10)) = Range(Cells(4, 4), Cells(4, 4))
w = w + TErepeat
Loop
p = 7
Do Until p > combos
Range(Cells(p, 10), Cells(p + Defrepeat - 1, 10)) = Range(Cells(5, 4), Cells(5, 4))
p = p + TErepeat
Loop
k = 10
Do Until k > combos
Range(Cells(k, 10), Cells(k + Defrepeat - 1, 10)) = Range(Cells(6, 4), Cells(6, 4))
k = k + TErepeat
Loop
Dim c As Range, i As Long, n As Long
Set c = Cells(3, 4)
For n = 1 To 10 Step 3
i = n
Do Until i > combos
Range(Cells(i, 10), Cells(i + Defrepeat - 1, 10)) = c.Value
i = i + TErepeat
Loop
Set c = c.Offset(1, 0)
Next n

index match return NA value

I have a vba code that find the value at the intersection of columns and rows.
It works well with all my data except for one : it returns NA.
The value i want to return is the same as usual, it just doesn't work with this intersection.
Can you help me figure out why?
Thank you
With Perftitres
Set VMt = Data1.Range("U:U")
Set Ticker = Data1.Range("H:H")
End With
' Calculs de perf
For Each sht In Perftitres.Worksheets
If sht.Visible = True Then
If sht.Cells(1, 1) = "" Then
sht.Cells(1, 1) = "Date"
sht.Cells(1, 2) = "Code du placement"
sht.Cells(1, 3) = "Valeur marchande t"
sht.Cells(1, 4) = "Valeur marchande t-1"
sht.Cells(1, 5) = "Valeur des achats"
sht.Cells(1, 6) = "Valeur des ventes"
sht.Cells(1, 7) = "Facteur"
sht.Cells(1, 8) = "Rendement 1 mois"
End If
LastRowsht = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
LastColumnsht = sht.Cells(1, sht.Columns.Count).End(xlToLeft).Column
sht.Cells(LastRowsht + 1, 1) = 20 & Left(Dateupdate, 2) & "-" & Right(Dateupdate, 2)
sht.Cells(LastRowsht + 1, 2) = sht.Name
sht.Cells(LastRowsht + 1, 3) = Application.Index(VMt, Application.Match(sht.Cells(LastRowsht + 1, 2), Ticker))
End If
Next sht
Data1.Visible = True
Data2.Visible = True
This line doesn't work as expected for only one sheet. For every other one it works.
sht.Cells(LastRowsht + 1, 3) = Application.Index(VMt, Application.Match(sht.Cells(LastRowsht + 1, 2), Ticker))
I just found the answer :
sht.Cells(LastRowsht + 1, 3) = Application.Index(VMt, Application.Match(sht.Cells(LastRowsht + 1, 2), Ticker,0),0)
I had to had 0 and 0 for exact matches

Finding the latest in grouping

I have several hundred cells. I want to find the latest in the grouping. For instance i have the following data:
233400-003-02
233400-002-03
233400-002-02
233400-002-01
233400-001-04
233400-001-03
233400-001-02
233400-001-01
The last number defines the revision. I want to keep only the greatest number or the latest revision. so far I have
For j = 9 To i Step 1
Dim Idstring As String
If Len(Cells(j, 1)) = 13 Then
Idstring = Left(Cells(j, 1), 10)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 16 Then
Idstring = Left(Cells(j, 1), 10)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 17 Then
Idstring = Left(Cells(j, 1), 14)
Cells(j, 5) = Idstring
ElseIf Len(Cells(j, 1)) = 20 Then
Idstring = Left(Cells(j, 1), 14)
Cells(j, 5) = Idstring
End If
If Cells(j, 5) = Cells(j - 1, 5) Then
If Len(Cells(j, 1)) = 16 Then
Cells(j, 5).EntireRow.Delete
ElseIf Len(Cells(j, 1)) = 20 Then
Cells(j, 5).EntireRow.Delete
ElseIf Right(Cells(j, 1), 1) < Right(Cells(j + 1, 1), 1) Then
Cells(j, 5).EntireRow.Delete
ElseIf Right(Cells(j, 1), 1) > Right(Cells(j + 1, 1), 1) Then
Cells(j + 1, 5).EntireRow.Delete
j = j + 1
End If
End If
Next j
What am I doing wrong? Thank you for your help.
I think your comparing to Cells(j-1) before you fill Cells(j-1). But if I'm wrong about that, you need to loop backward through the range when you delete rows or Excel loses track of where you are.
Public Sub DeleteAllButLatest()
Dim i As Long
For i = 9 To 3 Step -1
If Base(Cells(i, 1).Value) = Base(Cells(i - 1, 1).Value) Then
Cells(i, 1).EntireRow.Delete
End If
Next i
End Sub
Public Function Base(ByVal sCode As String) As String
Select Case Len(sCode)
Case 13, 17
Base = Left(sCode, Len(sCode) - 3)
Case 16, 20
Base = Left(sCode, Len(sCode) - 6)
End Select
End Function
Based on your sample data in A2:A9. Only need to go to Row 3 because Row 2 will have to be good so no need to check it. I made a function to return the "base" of each number so you can compare the base of the current cell to the cell above it. If they're the same, delete. If not, assume it's the latest.