我想知道一个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
'======================
的更新 的
通过Mistake我没有在输出表列中显示Type-3,Test-C,Prog-C。但他们应该在那里。
答案 0 :(得分: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