简体   繁体   中英

How to Assign Cell's Text Contents to a Variable in Excel VBA?

I'm getting a Run-time error '13': Type mismatch on the Set tempChar = Range(Cells(iSourceRow, 31), Cells(iSourceRow, 31)) line in the following code:

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

You don't need to use the set command.

Use a string and read the Range().Value into it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM