簡體   English   中英

VB.net Excel-將單元格值更改為特定格式

[英]VB.net Excel - change cell value to specific formatting

新增功能:我正在VS2015中為Excel編寫VB.net插件,需要一些幫助。 我已經刪除了同時包含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

我真的可以為此提供一些幫助。

謝謝!

沒關系,我是通過使用另一個字符串類型變量並將這些行寫到Excel中的,例如包含“ Z”的行來獲得的:

Dim finalText As String =結果&i&“ _Z” r.Value = finalText

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM