【发布时间】:2015-05-01 08:48:01
【问题描述】:
如果某列不为空,需要一些有关 Excel 的 VBA 脚本的帮助,以便将列中的数据转换为新行。如果列中的单元格不为空,则将几个主列中的初始数据复制到新行中,并将另一列中的数据复制/压缩到该新行中。我的文件有 1,000 条记录,我没有时间单独分开它们。最好在下面看到(抱歉没有足够的代表来发布图片)
这样开始。
Col1.......Col2.....Col3.....Col4
项目A.....$2.......................
项目B.....$2........$4.......
项目 C.....$6.......................
ItemD.....$2........$3....$5
项目E.....$9.......................
这样结束
Col1.......Col2
项目A.....$2
项目 B .....$2
项目 B .....$4
项目C.....$6
ItemD .....$2
ItemD .....$3
ItemD .....$5
项目E.....$9
这就是我在 vb 和 html 中处理记录集循环的方式。只需要关于确定记录集或范围以及如何从列开始的 excel 建议。
Dim Col1, Col2, Col3, Col4, RowData, CondenseData, FinalData
FinalData = ""
While ((RS.Items__numRows <> 0) AND (NOT RS.Items.EOF)) 'recordset loop how in Excel?
CondenseData = ""
Col1 = RS.Col1Data 'how to go from column to column in row in excel?
Col2 = RS.Col2Data
Col3 = RS.Col3Data
Col4 = RS.Col4Data
If Not IsNull(Col2) Then
CondenseData = Col1 & ", " & Col2
RowData = CondenseData & "<br />" ' create a new row with the revised data if not empty?
End If
If Not IsNull(Col3) Then
CondenseData = Col1 & ", " & Col3
RowData = CondenseData & "<br />"
End If
If Not IsNull(Col4) Then
CondenseData = Col1 & ", " & Col4
RowData = CondenseData & "<br />"
End If
FinalData = FinalData & RowData
RS.Items__index=RS.Items__index+1
RS.Items__numRows=RS.Items__numRows-1
RS.Items.MoveNext()
Wend
【问题讨论】:
-
欢迎来到 SO!阅读how to ask a good question 会让你更快得到答案。请记住,这不是代码编写服务,因此请发布您所拥有的内容,我们可以帮助您修复它。如果您不知道从哪里开始,请尝试使用宏记录器。
-
我听说你不是代码服务。我可以通过记录集循环和 if then 语句甚至 for 语句在睡眠中使用 VB 和 html 执行此操作,但我无法弄清楚如何在 excel 中创建“记录集”(我知道它是一个范围)。或转到下一个记录。如果有帮助,我可以轻松发布 vb 部分。