VBS:在2D数组(行和列)中找到最大str长度

  1. 需要在2D数组(行和列)中找到最大字符串长度,以计算最小列宽。
  2. 如何根据变量值定义固定的数组大小(可以从工具返回)?
'So now here we have:

Dim arr()
Dim nRowsMax,nColumnmax
nRows = oTool.MaxRowCount
nColumns = oTool.MaxColCount

ReDim arr(nRows,nColumns)

Dim sCellValue : sCellValue = oTool.Value(nRow,nCol)
Dim nCellWidth = Len(sCellValue)

Dim nRow,nCol
For nRow = 1 To nRows
    For nColumn = 1 To nColumns
        If arr(nRow,nCol) < nmin Then nmin = arr(nRow,nCol)
        If arr(nRow,nCol) > nmax Then nmax = arr(nRow,nCol)
    Next ' Column
Next ' Row  

Hilda_Chen 回答:VBS:在2D数组(行和列)中找到最大str长度

根据您更新的问题和评论,以下是调整后的答案:

Dim iRows,iColumns
Dim iRowCounter,iColumnCounter
Dim sValue
Dim iLength
Dim iMaxLength
Dim arrMaxLength()

iRows = oTool.MaxRowCount
iColumns = oTool.MaxColCount

' Set array dimensions based on values from oTool
ReDim arrMaxLength(iColumns)

For iColumnCounter = 1 To iColumns

    arrMaxLength(iColumnCounter - 1) = 0

    For iRowCounter = 1 To iRows

        ' Get value and compare negth with previous iMaxLength
        sValue = oTool.Value(iRowCounter,iColumnCounter)
        iLength = Len(sValue)
        If iLength > iMaxLength Then iMaxLength = iLength
        If iLength > arrMaxLength(iColumnCounter - 1) Then arrMaxLength(iColumnCounter - 1) = iLength

    Next ' Row

Next ' Column
本文链接:https://www.f2er.com/3143082.html

大家都在问