2017-01-23 25 views
-1

我有一個數據集,包含對多個測試的項目響應。我想根據正確的響應重新編碼響應,可以從文件中讀取響應。測試的長度不同,記錄的數量可能因測試和應用的數據集而異。舉例來說,數據集可能有400條記錄,其中有50條閱讀條目,40條數學條目30條拼寫條目等。 目前我正在重新編碼每個項目的響應,如下面的代碼所示。我想閱讀我的50個正確答案,並將其應用於相應的50列(CV:ES)中的答覆,然後使用相同/相似的代碼結構應用於其他列中的其他測試答案。在VBA中使用數組應用密鑰來重新編碼項目響應

Sub readscore27() 
Dim readkey(1) As String 
readkey(1) = "A" 

Dim MyRange As Range 
Dim MyCell As Range 
Sheets("output2").Select 
'Step 2: Define the target Range. 
Set MyRange = Range("dv2", Range("dv" & Rows.Count).End(xlUp)) 
'Step 3: Start looping through the range. 
For Each MyCell In MyRange 
'Step 4: Do something with each cell. 
    If MyCell.Value = "^" Then 
    MyCell.Value =readkey(1) 
ElseIf MyCell.Value = "--" Then 
    MyCell.Value = "-" 
ElseIf IsEmpty(MyCell) Then 
    MyCell.Value = "" 
End If 
'Step 5: Get the next cell in the range 
Next MyCell 
End Sub 
+0

目前尚不清楚您當前的代碼與您想要做什麼的描述相關。如果您的流程的每個部分都包含示例數據,那麼您的問題會更容易回答。 –

+0

感謝蒂姆,當前的代碼是 – Paddy

+0

謝謝蒂姆,當前的代碼是我正在做的50倍以上。代碼示例是我的第27個變量。正確的響應被分配給'readkey',並且對recode的響應位於'輸出'的'dv'列' – Paddy

回答

0

通過向readscore添加一些參數,可以使代碼更通用。

下面的示例。

Sub Tester() 

    readscore "DV", "A" 
    readscore "DW", "B" 
    readscore "DX", "C" 

End Sub 



Sub readscore(colLetter, readkey) 
    Dim c As Range, myRange As Range, v 

    With Sheets("output2") 
     Set myRange = .Range(.Cells(2, colLetter), _ 
         .Cells(.Rows.Count, colLetter).End(xlUp)) 
    End With 

    For Each c In myRange 
     v = c.Value 
     If v = "^" Then 
      c.Value = readkey 
     ElseIf v = "--" Then 
      c.Value = "-" 
     ElseIf Len(v) = 0 Then 
      c.Value = "" 
     End If 
    Next c 
End Sub 
+0

謝謝蒂姆,作品一種享受,感謝您的時間:) – Paddy