繁体   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