VB.net Excel-将单元格值更改为特定格式

时间:2018-10-31 15:24:54

标签: excel vb.net

这里的新功能:我正在VS2015中为Excel编写VB.net addIn,我需要一些帮助。我已经开始删除包含Mull和X或Z值的行,但是...

当第1列的行(x)包含两个值“ Mull”(介于0到10和“ Y”之间的某个数字)而不是“ _ Y”时,我想将其更改为Mull和迭代式#和_Y的格式。这是我要在其中执行的代码:

    Dim i As Integer = 1
    Do While i <= totalRows

        ' Dim newArray() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} *Better way below

        'Find all Cells that contain "Mull" for Mullion and get the individual characters in order to break it apart and 
        'format it correctly.

        r = Nothing
        If array.GetValue(i, 1).ToString.Contains("Mull") Then
            Dim strOld = array.GetValue(i, 1).ToString.Contains("Mull")

            Dim str As String = CStr(array.GetValue(i, 1))
            If str.Contains("Z") Then
                'Dim xlWorkSheets As Excel.Sheets = Nothing
                r = CType(sheet.Rows(i), Excel.Range)
                r.Cells.EntireRow.Delete()

            ElseIf array.GetValue(i, 1).ToString.Contains("X") = True Then
                MsgBox("Contains an X" & " index " & i)
                r = CType(sheet.Rows(i), Excel.Range)
                Dim countX As Integer
                countX = sheet.Rows.Count
                r.Cells.EntireRow.Delete()
            End If

            'Do Until sheet.Cells(i, 1).ToString.Contains("_Y")
            MsgBox("Mull and Y found")
            For Each str In array.GetValue(i, 1).ToString()
                If array.GetValue(i, 1).ToString.Contains("Mull") And Not array.GetValue(i, 1).ToString.Contains("_Y") Then
                    Dim num As Integer = 0
                    r = CType(sheet.Cells(i, 1), Excel.Range)
                    Dim strNew As String
                    strNew = Microsoft.VisualBasic.Left(str, Len(str) - 2)
                    Dim test1 As String = str
                    Dim result = Truncate(test1, 16)
                    Dim result2 As String
                    result2 = Truncate(test1, 10)
                    Dim finalText As Object = result2 & "_" & num & "Y"
                    r.Value = finalText
                    MsgBox(num)
                    num = num + 1
                Else
                    Return
                End If
                'Loop
            Next
        Else
        End If


        i = i + 1
    Loop

顺便说一句,我的Truncate函数如下:

Public Function Truncate(value As String, length As Integer) As String
    If length > value.Length Then
        Return value
    Else
        Return value.Substring(0, length)
    End If
End Function

**请注意,我正在导入... Interop.Excel

我真的可以为此提供一些帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

没关系,我是通过使用另一个字符串类型变量并将这些行写到Excel中的,例如包含“ Z”的行来获取的:

将finalText设为字符串=结果&i&“ _Z”                             r.Value = finalText