0

我想知道一个VBscript,我可以通过它移动一侧的空行值和另一侧的非空值保持数据描述完整。这可以使用循环技术来完成。但是如果可以使用 VBscript 实现的话,我想要一些更快的过程。

输入表

Code                Error-I                          Error-II                          Error-III



           Type-1    Type-2    Type-3        Test-A      Test-B    Test-C          Prog-A  Prog-B  Prog-C   



Code-A               Yes         No                                  Yes              X              Z     

Code-B                           No                        Yes       Yes                      Y      Z

Code-C              Yes                       Yes                     No                             Z

输出表

Code                Error-I                          Error-II                          Error-III



           Type-1    Type-2                   Test-A      Test-B                    Prog-A  Prog-B   



Code-A        Yes       No                     Yes                                     X     Z     

Code-B        No                               Yes        Yes                          Y     Z

Code-C        Yes                              Yes         No                          Z

更新:移位后,如果发现组中的列不包含单个数据,则应将该列从工作表中删除。

我为所有列集编写了以下代码,但它产生了不正确的数据移位。你能说我哪里错了吗?

Option Explicit

Dim objExcel1
Dim strPathExcel1
Dim objSheet1
Dim row,col1,col2
Dim TotlColumnSet : TotlColumnSet =3
Dim AssColmuns : AssColmuns=3
Dim EachColumnSet, ColStart, ColEnd

Set objExcel1 = CreateObject("Excel.Application")
strPathExcel1 = "D:\VA\Copy of Test.xlsx"
objExcel1.Workbooks.open strPathExcel1
Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)

ColStart = 2
For EachColumnSet = 1 To TotlColumnSet

  For row = 3 To 5
    ' find the first empty cell in the row
    col1 = ColStart'2
    ColEnd = ColStart + AssColmuns
    Do Until IsEmpty(objSheet1.Cells(row, col1)) Or col1 > ColEnd-1'4
      col1 = col1 + 1
    Loop

    ' collapse right-hand cells to the left
    If col1 < ColEnd-1 Then '4
      ' proceed only if first empty cell is left of the right-most cell
      ' (otherwise there's nothing to do)
      col2 = col1 + 1
      Do Until col2 > ColEnd-1'4
        ' move content of a non-empty cell to the left-most empty cell, then
        ' increment the index of the left-most empty cell (the cell right of
        ' the former left-most empty cell is now guaranteed to be empty)
        If Not IsEmpty(objSheet1.Cells(row, col2).Value) Then
          objSheet1.Cells(row, col1).Value = objSheet1.Cells(row, col2).Value
          objSheet1.Cells(row, col2).Value = Empty
          col1 = col1 + 1
        End If
        col2 = col2 + 1
      Loop
    End If
  Next

  ColStart = ColEnd

Next

'=======================
objExcel1.ActiveWorkbook.SaveAs strPathExcel1
objExcel1.Workbooks.close
objExcel1.Application.Quit
'======================    

更新:

由于错误,我没有在输出表列 Type-3、Test-C、Prog-C 中显示。但他们应该在那里。

4

1 回答 1

2

如果我对您的理解正确,您想折叠左侧的每一列。如果是这样,结果中的列标题确实具有误导性。

工作表是否总是有 3 行,每组 3 列,每组 3 列?在这种情况下,您可以简单地使用单元格的绝对位置。第一组列的示例:

filename = "..."

Set xl = CreateObject("Excel.Application")
xl.Visible = True

Set wb = xl.Workbooks.Open(filename)
Set ws = wb.Sheets(1)

For row = 3 To 5
  ' find the first empty cell in the row
  col1 = 2
  Do Until IsEmpty(ws.Cells(row, col1)) Or col1 > 4
    col1 = col1 + 1
  Loop

  ' collapse right-hand cells to the left
  If col1 < 4 Then
    ' proceed only if first empty cell is left of the right-most cell (otherwise
    ' there's nothing to do)
    col2 = col1 + 1
    Do Until col2 > 4
      ' move content of a non-empty cell to the left-most empty cell, then
      ' increment the index of the left-most empty cell (the cell right of the
      ' former left-most empty cell is now guaranteed to be empty)
      If Not IsEmpty(ws.Cells(row, col2).Value) Then
        ws.Cells(row, col1).Value = ws.Cells(row, col2).Value
        ws.Cells(row, col2).Value = Empty
        col1 = col1 + 1
      End If
      col2 = col2 + 1
    Loop
  End If
Next
于 2012-12-10T19:14:05.963 回答