我有一个问题。为了简化故事,我试图查看数据表的所有五列中存在的唯一标识符的数量(不只是在一列,两列,三列或四列中)。数据代表在不同时间段完成调查的受访者。我已经编写了VBA来计算显示在所有五列中的唯一标识符的数量,但是当我尝试使用输入框为我的变量赋值时出现问题,并且我的累加器出现了问题。当我使用inputbox时,我得到了运行时错误104,并在代码本身中为我的变量赋值(不是很好的做法,我知道)。如果我没有对它进行评论,我的累加器总数为11,但是使用ColorIndex属性,我在所有列中只计算五个唯一标识符。
我是编程新手,所以我们非常感谢其他指导。代码如下:
Option Explicit
Sub longitudinalCheck()
'activate the correct worksheet
Worksheets("Sheet7").Activate
'declare variables
Dim preStart, preEnd, postStart, postEnd, fourToSixStart, fourToSixEnd, sixStart, sixEnd, twelveStart, twelveEnd, accumulator As Integer
Dim preColumn, postColumn, fourToSixColumn, sixColumn, twelveColumn As Integer
Dim preLooper, postLooper, fourToSixLooper, sixLooper, twelveLooper As Integer
Dim accumulatorIncrementer As Integer
accumulatorIncrementer = 1
accumulator = 0
'user assigns values to variables based on dataset
'preStart = InputBox("Which row do the user ids for the precourse data start in?")
preStart = 3
'preEnd = InputBox("Which row does the user ids for the precourse data end in?")
preEnd = 427
'postStart = InputBox("Which row does the user ids for the postcourse data start in?")
postStart = 3
'postEnd = InputBox("Which row does the user ids for the postcourse data end in?")
postEnd = 427
'fourToSixStart = InputBox("Which row does the 4-6 data start in?")
fourToSixStart = 3
'fourToSixEnd = InputBox("Which row does the 4-6 data end in?")
fourToSixEnd = 44
'sixStart = InputBox("Which row does the 6 Month data start in?")
sixStart = 3
'sixEnd = InputBox("Which row does the 6 Month data end in?")
sixEnd = 38
'twelveStart = InputBox("Which row does the 12 Month data start in?")
twelveStart = 3
'twelveEnd = InputBox("Which row does the 12 Month data end in?")
twelveEnd = 46
'preColumn = InputBox("What column is the predata in?")
preColumn = 1
'postColumn = InputBox("What column is the postdata in?")
postColumn = 2
'fourToSixColumn = InputBox("What column is the 4-6 data in?")
fourToSixColumn = 3
'sixColumn = InputBox("What column is the 6 month data in?")
sixColumn = 4
'twelveColumn = InputBox("What column is the 12 month data in?")
twelveColumn = 5
'for loop runs to see how many folks went all the way through
For preLooper = preStart To preEnd
For postLooper = postStart To postEnd
If Cells(preLooper, preColumn) = Cells(postLooper, postColumn) Then
For fourToSixLooper = fourToSixStart To fourToSixEnd
If Cells(preLooper, preColumn) = Cells(fourToSixLooper, fourToSixColumn) Then
For sixLooper = sixStart To sixEnd
If Cells(preLooper, preColumn) = Cells(sixLooper, sixColumn) Then
For twelveLooper = twelveStart To twelveEnd
If Cells(preLooper, preColumn) = Cells(twelveLooper, twelveColumn) Then
'accumulator = accumulator + accumulatorIncrementer
Cells(preLooper, preColumn).Interior.ColorIndex = 37
Cells(postLooper, postColumn).Interior.ColorIndex = 37
Cells(fourToSixLooper, fourToSixColumn).Interior.ColorIndex = 37
Cells(sixLooper, sixColumn).Interior.ColorIndex = 37
Cells(twelveLooper, twelveColumn).Interior.ColorIndex = 37
ElseIf Cells(preLooper, preColumn) <> Cells(twelveLooper, twelveColumn) Then
End If
Next twelveLooper
ElseIf Cells(preLooper, preColumn) <> Cells(sixLooper, sixColumn) Then
End If
Next sixLooper
ElseIf Cells(preLooper, preColumn) <> Cells(fourToSixLooper, fourToSixColumn) Then
End If
Next fourToSixLooper
ElseIf Cells(preLooper, preColumn) <> Cells(postLooper, postColumn) Then
End If
Next postLooper
Next preLooper
'results displayed to me
MsgBox "The data has " & accumulator & " respondents that have taken all surveys."
End Sub