简体   繁体   English

Excel VBA-如何在列中找到最大的子字符串值

[英]Excel VBA - how to find the largest substring value in a column

I have a column in a spreadsheet. 我在电子表格中有一列。 The format of the data in each cell is aa-0001-xx. 每个单元格中的数据格式为aa-0001-xx。 I need to examine the whole column to find the highest value of the sequence number. 我需要检查整列以找到序列号的最大值。 this would be the substring from column4 thru column7. 这将是column4到column7的子字符串。 I can find the sequence number using Mid(ActiveWorkbook.Sheets("Sheet1").Range("B2:B2"), 4, 4) But I need to find the max sequence in the whole column. 我可以使用Mid(ActiveWorkbook.Sheets(“ Sheet1”)。Range(“ B2:B2”),4,4)找到序列号,但是我需要在整列中找到最大序列。 I am doing this in VBA. 我正在VBA中执行此操作。 Any help would be appreciated. 任何帮助,将不胜感激。

Here is my code so far: Private Sub CommandButton1_Click() 到目前为止,这是我的代码:Private Sub CommandButton1_Click()

Dim sQuoteNumber As String
Dim sFileName As String
Dim sPathName As String
Dim checkit As String

'Log the Quote

'First, open the log file and determine the next sequential log number.

sPathName = "C:\Users\Rich\Documents\Bryan\BigProject\"
sFileName = "QuoteLog2016.xlsx"
ControlFile = ActiveWorkbook.Name

Workbooks.Open Filename:=sPathName & sFileName


'Create the new Quote Number
checkit = Mid(ActiveWorkbook.Sheets("Sheet1").Range("B2:B2"), 4, 4) ' This is a temp test line

 If Mid(ActiveWorkbook.Sheets("Sheet1").Range("B2:B2"), 4, 4) = "" Then
    sQuoteNumber = "16-0001"
Else
    'find the biggest number

    'Here I was looking to like pass the mid function to a Max function  of some sort.

    sQuoteNumber = "16-0002"
End If

MsgBox ("The new Quote Number is: " + sQuoteNumber)


'Save the log entry

Workbooks(sFileName).Close

All of the comments made to your answer would work well for you. 对您的答案所作的所有评论都将很适合您。 It's also true that there's no evidence in your code at having attempted something, however rudimentary, and this is why answers to a rather trivial task are not forthcoming for you. 确实,您的代码中没有证据表明您尝试过任何内容(无论多么初级),这就是为什么您无法获得答案的原因。 Perhaps, in future, have a go at some kind of solution ( even if it feels more guesswork than anything) and people on this site will be much more supportive of you. 也许将来有某种解决方案(即使感觉比任何事情都要多),该站点上的人员会为您提供更多支持。

To set you on your way, you could make use of the Split() function which converts a String into a String array , separated by a nominated value - in the case of your quotations, you could use "-" as your separator. 为方便起见,您可以使用Split()函数,该函数将String转换为String array ,并以指定的值分隔-如果使用引号,则可以使用“-”作为分隔符。 This might be easier than your Mid function and will deal with the case of different sized quotations. 这可能比Mid函数更容易,并且可以处理不同大小的引用。

The code below will get you started but you'd want some error handling in there to test, for example, that each cell splits appropriately or that any cells aren't blank. 下面的代码可以帮助您入门,但是您需要在其中进行一些错误处理以进行测试,例如,每个单元格都适当拆分,或者任何单元格都不为空。 I'll leave all of that to you. 我将所有这些留给您。

Option Explicit
Private mLastQuote As Long

Public Sub Test()
    Initialise 'call this routine just once at the start of your project
    MsgBox GetNextQuote(16) 'use the GetNextQuote() function to get next number
    MsgBox GetNextQuote(16)
    MsgBox GetNextQuote(16)
End Sub

Private Function GetNextQuote(prefix As Integer) As String
    mLastQuote = mLastQuote + 1
    GetNextQuote = CStr(prefix) & "-" & _
                   Format(mLastQuote, "000#")
End Function

Private Sub Initialise()
    Const PATH_NAME As String = "C:\Users\Rich\Documents\Bryan\BigProject\"
    Const FILE_NAME As String = "QuoteLog2016.xlsx"
    Const QUOTE_COL As String = "B"

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim v As Variant
    Dim r As Long
    Dim parts() As String
    Dim num As Long

    Application.ScreenUpdating = False
    Set wb = Workbooks.Open(PATH_NAME & FILE_NAME, True, True)
    Set ws = wb.Worksheets("Sheet1")

    'Read quote values into variant array
    With ws
        v = .Range(.Cells(2, QUOTE_COL), _
            .Cells(.Rows.Count, QUOTE_COL).End(xlUp)) _
            .Value2
    End With

    'Find max quote
    For r = 1 To UBound(v, 1)
        parts = Split(v(r, 1), "-") 'splits quote into 3 parts
        num = CLng(parts(1)) 'index (1) is the middle part
        If num > mLastQuote Then mLastQuote = num
    Next

    wb.Close False
    Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM