我有VBA程序,允许用户在活动Excel工作表的第一行输入名称(整数/字符串/等)来编写标题。写入输入后,我需要在相邻的单元格中写入输出。
逐行逐行显示宏,我相信这是产生错误的问题行
运行时错误13:输入不匹配
Cells(1, counter + inputnum) = outputnum
这是相关功能:
Sub enteroutputs()
title = "K-Map Program"
outputnum = Application.InputBox("How many outputs?", title)
If IsNumeric(outputnum) = False Then
problem = "output"
Call notnum
End If
For counter = 1 To outputnum
outputnum = Application.InputBox("Enter output name.", title)
Cells(1, counter + inputnum) = outputnum
Next
Dim ok
ok = MsgBox("Enter outputs in " & ActiveSheet.Name & " .", vbOKOnly)
End Sub
inputnum在此函数之前执行的函数中定义:
Sub enterinputs()
title = "K-Map Program"
inputnum = Application.InputBox("How many inputs?", title)
If IsNumeric(inputnum) = False Then
problem = "input"
Call notnum
End If
For counter = 1 To inputnum
inputnum = Application.InputBox("Enter input name.", title)
Cells(1, counter) = inputnum
Next
Call enteroutputs
End Sub
答案 0 :(得分:2)
你只是错过了什么
Sub enterinputs()
title = "K-Map Program"
inputnum = Application.InputBox("How many inputs?", title)
If IsNumeric(inputnum) = False Then
problem = "input"
Call notnum
End If
' ~~~~ here inputnum is numeric ~~~~
For counter = 1 To inputnum
inputnum = Application.InputBox("Enter input name.", title) ' ~~~~ here inputnum is not! ~~~~
Cells(1, counter) = inputnum
Next
Call enteroutputs 'while inputnum is NOT numeric
Exit Sub
只需在Next
和Call enteroutputs
之间添加:
inputnum = counter - 1