目前我正在编写VBS以从excel报告表中获取值。 试图使用DO直到循环来获取所有矩阵数据。 但是我的脚本只能通过一个循环得到结果(只有一行X col),但不能进入下一个循环。 可以请指教我错过任何值让脚本在下一个col上工作吗?
非常感谢。
Set Args = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
WScript.echo "cYear," & "cMonth," & "cEntity," & "cAccount,"& "cAmount"
cFile = "C:\XXX.xlsx"
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(cFile)
Set objFile = fso.GetFile(cFile)
cFilename = fso.GetFileName(objFile)
intRow = 21
intCol = 8
'Read xlsx data
Do Until objExcel.Cells(13, intCol).Value = ""
Do Until objExcel.Cells(intRow, 2).Value = ""
cYear = objExcel.Cells(2, 2)
cMonth = objExcel.Cells(3, 2)
cEntity = objExcel.Cells (13, intCol)
cAccount = objExcel.Cells(intRow, 2)
cAmount = objExcel.Cells(intRow, intCol)
WScript.echo cYear &","& cMonth&"," & cEntity&"," & cAccount&"," & cAmount
exit do
intRow= intRow + 1
Loop
intCol = intCol + 1
loop
答案 0 :(得分:1)
请尝试从代码中删除“退出操作”行。 我猜它有效。但是,为了更好地理解问题,请粘贴您的Excel样本
答案 1 :(得分:0)
当Dinesh迭代时,Exit do肯定是导致你的代码只产生1行数据的罪魁祸首。
cFile = "C:\XXX.xlsx"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(cFile)
Set objFile = fso.GetFile(cFile)
cFilename = fso.GetFileName(objFile)
intRow = 21
intCol = 8
'Read xlsx data
Do Until objExcel.Cells(13, intCol).Value = ""
Do Until objExcel.Cells(intRow, 2).Value = ""
cYear = objExcel.Cells(2, 2)
cMonth = objExcel.Cells(3, 2)
cEntity = objExcel.Cells (13, intCol)
cAccount = objExcel.Cells(intRow, 2)
cAmount = objExcel.Cells(intRow, intCol)
WScript.echo cYear &","& cMonth&"," & cEntity&"," & cAccount&"," & cAmount
'Will cause the do to terminate after the first row value.
'exit do
intRow= intRow + 1
Loop
intCol = intCol + 1
Loop
所以我对自己进行了测试,这是输出。使用一个小的宏来填充100行×20列的电子表格。
Cell B Row 2,Cell B Row 3,Cell H Row 13,Cell B Row 21,Cell H Row 21
Cell B Row 2,Cell B Row 3,Cell H Row 13,Cell B Row 22,Cell H Row 22
Cell B Row 2,Cell B Row 3,Cell H Row 13,Cell B Row 23,Cell H Row 23
Cell B Row 2,Cell B Row 3,Cell H Row 13,Cell B Row 24,Cell H Row 24