Joining String variables... struggling

maykinit

New Member
Joined
Jan 19, 2009
Messages
31
Dim Var1 As String, Var2 As String
Var1 = InputBox("Variable 1?")
Var2 = InputBox("Variable 2?")

:confused:If I have two string variables defined as:

sFirst = "ActiveCell.Value = Val(Var1)"
sSecond = "ActiveCell.Offset(, 1).Value = Val(Var2)"

The value of ActiveCell and ActiveCell.Offset matches that of Var1 and Var2

I wonder if someone could advise what I am doing wrong when the following will not work? What is it I am doing wrong? I get "run time error '13': Type Mismatch". Thanks so much.

If (sFirst) & " " & "And" & " " & (sSecond) Then
MsgBox "works"
End If
 
What (I think) you are doing will not work. You cannot build up strings of code like that and then execute them directly. If you really wanted to, you would have to actually edit a code module in code to write that code out, then call the routine you had created. I would not recomend this and cannot, offhand, think of a good reason for needing to do this; perhaps you could elaborate?
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This works for your example:
Code:
Sub x()
    Const nCol      As Long = 6
    Dim av(1 To nCol) As Variant
    Dim iRow        As Long
    Dim iCol        As Long
 
    av(1) = 10
    av(2) = 102
    av(3) = "S"
    av(4) = "S"
    av(5) = "02"
    av(6) = "600RJ"
 
    For iRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row
        For iCol = 1 To nCol
            If Not IsEmpty(Cells(iRow, iCol)) Then
                If Cells(iRow, iCol) <> av(iCol) Then Exit For
            End If
        Next iCol
        If iCol > nCol Then MsgBox Cells(iRow, nCol + 1)
    Next iRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top