最初,excel文件中C列上的值根本没有值。这是我想要做的逻辑,如果值B在列B上为num1,在列C上设置为100,否则如果值在列B上为num2,在列C上设置为200,在列C上设置为300.我的当前代码现在只能只做一行。
那就是为什么我需要一个循环,逐行去做,它会停止,直到它到达第11行,这是最后一行值。我已经粘贴了我在底部使用循环代码尝试的内容,但它不起作用。请帮助我纠正我的循环,或者我的公式有一些问题,因为我不知道我是否正确地在公式中插入计数器变量而不是。
使用我当前工作代码在C列中生成值的当前输出。
我希望使用代码实现的理想输出。
我的当前代码为上面第一个屏幕截图的C列生成值
Set objShell = CreateObject("Wscript.Shell")
Set objExcel = CreateObject("Excel.Application")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objWorkbook2 = objExcel.Workbooks.Open("D:\Shared\tempname\test\test2.xlsx")
'********** Instantiate the Workbook.Worksheets **********
' object to open a particular
' Excel Worksheet
Set objWorksheet2 = objWorkbook2.Worksheets(c_FirstWksht)
Dim cellObj
set cellObj = objWorksheet2.cells(3, 3)
cellObj.formula = "=IF(B3=""NUM1"",""100"",IF(B3=""NUM2"",""200"",""300""))"
Set objWorkbook2 = nothing
objExcel.Quit
'********** Exit the Script **********
Wscript.Quit
我试图循环代码并且无法正常工作
Set objShell = CreateObject("Wscript.Shell")
Set objExcel = CreateObject("Excel.Application")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objWorkbook2 = objExcel.Workbooks.Open("D:\Shared\tempname\test\test2.xlsx")
'********** Instantiate the Workbook.Worksheets **********
' object to open a particular
' Excel Worksheet
Set objWorksheet2 = objWorkbook2.Worksheets(c_FirstWksht)
Dim cellObj
Dim counter
counter = 3
Do Until counter = 13
set cellObj = objWorksheet2.cells(counter, 3)
cellObj.formula = "=IF("& counter &"=""NUM1"",""100"",IF("& counter &"=""NUM2"",""200"",""300""))"
Loop
Set objWorkbook2 = nothing
objExcel.Quit
'********** Exit the Script **********
Wscript.Quit
我使用我尝试过的循环代码来解决此错误。
关注user3907047代码后出错
答案 0 :(得分:1)
这有点基本......但是应该这样做:
Sub test()
Dim nrows, ncols As Single
Dim CWS, CWB As String
CWB = ActiveWorkbook.Name
CWS = Workbooks(CWB).ActiveSheet.Name
nrows = 2
ncols = 2
Do Until Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols) = ""
If Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols) = "NUM1" Then
Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols + 1) = "100"
ElseIf Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols) = "NUM2" Then
Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols + 1) = "200"
Else
Workbooks(CWB).Sheets(CWS).Cells(nrows, ncols + 1) = "300"
End If
nrows = nrows + 1
Loop
End Sub
我希望这会有所帮助,我试图用我认为应该有效的第二个例子来实现我的循环逻辑:
Set objShell = CreateObject("Wscript.Shell")
Set objExcel = CreateObject("Excel.Application")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objWorkbook2 = objExcel.Workbooks.Open("D:\Shared\tempname\test\test2.xlsx")
'********** Instantiate the Workbook.Worksheets **********
' object to open a particular
' Excel Worksheet
Set objWorksheet2 = objWorkbook2.Worksheets(c_FirstWksht)
Dim nrows, ncols
nrows = 2
ncols = 2
Do Until Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols) = ""
If Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols) = "NUM1" Then
Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols + 1) = "100"
ElseIf Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols) = "NUM2" Then
Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols + 1) = "200"
Else
Workbooks(objWorkbook2).Sheets(objWorksheet2).Cells(nrows, ncols) = "300"
End If
nrows = nrows + 1
Loop
Set objWorkbook2 = nothing
objExcel.Quit
'********** Exit the Script **********
Wscript.Quit
答案 1 :(得分:1)
我修改过:
Set objShell = CreateObject("Wscript.Shell")
Set objExcel = CreateObject("Excel.Application")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objWorkbook2 = objExcel.Workbooks.Open("e:\0\xx\Fax.xlsx")
Set objWorksheet2 = objWorkbook2.Worksheets(1)
Dim cellObj
Dim counter
counter = 3
Do Until counter = 12
set cellObj = objWorksheet2.cells(counter, 3)
cellObj.formula = "=IF(B"& counter &"=""NUM1"",""100"",IF(B"& counter &"=""NUM2"",""200"",""300""))"
counter = counter + 1
Loop
objWorkbook2.save
Set objWorkbook2 = nothing
objExcel.Quit
Wscript.Quit
发现错误: - )
1)在公式中,您忘记了列的名称" B"
2)你没有增加柜台
3)您没有保存工作簿
4)直到它,这不是一个好工作。如果更改行数,则需要更改脚本
对于第4点,我更喜欢像:
Do Until counter = 1200
if objWorksheet2.range("B" & counter).value = "" then
counter = 1200
else
set cellObj = objWorksheet2.cells(counter, 3)
cellObj.formula = "=IF(B"& counter &"=""NUM1"",""100"",IF(B"& counter &"=""NUM2"",""200"",""300""))"
counter = counter + 1
end if
Loop
它更实用......