2015-10-23 156 views
1

我得到一個運行時錯誤「13」:在下面的代碼Set tempChar = Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31))行類型不匹配:如何在Excel VBA中將單元格的文本內容分配給變量?

Sub GetStratGoalResponses() 
' 
' GetStratGoalResponses Macro 
' 
' Keyboard Shortcut: Ctrl g 
' 
    Dim iSourceRow As Integer 
    Dim iTargetRow As Integer 
    Dim tempChar As Characters 

    iTargetRow = 1 

    For iSourceRow = 2 To 28 
     Worksheets("Survey_Responses_Oct_12,_2015").Activate 
     If Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31)) = "" Then End 
     Set tempChar = Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31)) 
MsgBox "About to process Strategic Goal Response ", tempChar 
     Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31)).Select 
     Selection.Copy 
     Worksheets("Strategic Goal Parsed").Activate 
     Range("A2").Select 
     ActiveSheet.Paste 
     Set tempChar = Range("A2") 
MsgBox "Just pasted response of ", tempChar 
     Range("A4:A9").Select 
     Selection.Copy 
     Range(Cells(iTargetRow, 53), Cells(iTargetRow, 53)).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 
     Application.CutCopyMode = False 
     Range("A2:A2").Select 
     Selection.Clear 
     iTargetRow = iTargetRow + 6 
    Next iSourceRow 
End Sub 
+0

你宣佈'tempChar'爲'character'不'range'閱讀範圍()值。 – findwindow

回答

2

你並不需要使用set命令。

使用的字符串,並把它

Dim tempChar as string 
tempChar = Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31)).Value 
msgbox (tempChar) 
+0

謝謝,馬修,這很好! – GeorgeInNC

+0

如果此答案已解決您的問題,請考慮單擊複選標記以接受此問題。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – MatthewD

相關問題