简体   繁体   中英

VB.net Excel - change cell value to specific formatting

New here: I'm writing a VB.net addIn for Excel in VS2015 that I need a bit of help with. I've got the part working where I delete rows containing a value of both Mull and X or Z, but...

When column 1, row(x) contains a value of both "Mull", some number between 0 and 10 and "Y" And Not "_Y", I want to change it to have the formatting of Mull & iterative # & _Y. Here's the code I'm trying to do this in:

    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

BTW, my Truncate function looks like this:

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

**Note I'm importing ...Interop.Excel

I could really use some help on this one.

Thanks!

Never mind, I got it by using another string type variable and writing the lines out to Excel like this for ones that contain "Z" for instance:

Dim finalText As String = result & i & "_Z" r.Value = finalText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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