设置变量VBA时出现运行时错误91

时间:2018-07-20 17:33:49

标签: excel vba excel-vba runtime-error

当我在Excel VBA中运行以下子例程时,出现此错误:

  

运行时错误'91':对象变量或未设置块变量

发生错误的行正在尝试将字母(特别是引用列)转换为该列的相应编号。我在程序的前面成功地使用了完全相同的方法,但使用了不同的变量,这就是为什么我对这个错误感到困惑。任何输入表示赞赏,谢谢!

Sub Merge_Duplicate_Columns()

MsgBox ("The summed values of the two columns will automatically be placed in the firstCol selection, so choose accordingly.")

'Dim firstCol As Integer
'Dim secondCol As Integer
Dim firstLetter As String
Dim secondLetter As String

firstLetter = InputBox("Please enter the letter of the first column you would like to merge:", "First Choice")
secondLetter = InputBox("Please enter the letter of the second column you would like to merge:", "Second Choice")

'firstCol = source.Range(firstLetter & 1).Column         'need to set variables somehow
'secondCol = source.Range(secondLetter & 1).Column

'firstCol = source.Columns(firstLetter).Column
'secondCol = source.Columns(secondLetter).Column

Dim i As Integer
For i = rowStart To rowEnd
    source.Cells(i, firstLetter).Value = source.Cells(i, firstLetter).Value + source.Cells(i, secondLetter).Value
Next i
source.Columns(secondLetter).EntireColumn.Delete

End Sub

code screenshot

0 个答案:

没有答案