Link to home
Start Free TrialLog in
Avatar of greenee
greenee

asked on

VBA Excel If without else error

Having an issue with a excel VBA module.
If then else statement is not working.
Please help.
Code is attached.
Thanks,
Eric
Do While ActiveCell <> ""  'Loops until the active cell is blank.
    
    If ActiveCell.Offset(0, -2).Value = "1320" Then _
      ActiveCell.Offset(0, 1).FormulaR1C1 = _
         "https://testlink1320=" & " " & _
    ActiveCell.Offset(0, 0)
      ActiveCell.Offset(1, 0).Select
    Else
      ActiveCell.Offset(0, 1).FormulaR1C1 = _
         "https://testlink1330=" & " " & _
         ActiveCell.Offset(0, 0)
      ActiveCell.Offset(1, 0).Select
    End If
    
   Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gtgloner
gtgloner
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is the 1320 value a text or a number. If it's a number you need to put =1320 without the inverted comas.

Also, since you have a if then else end if structure, you need to remove the underscore after the then on line 3

Thomas
Avatar of jet46
jet46

What is the problem you are having?  Also, try taking out the _ at the end of the Then
Avatar of Ardhendu Sarangi
You have an error in your code.

"  If ActiveCell.Offset(0, -2).Value = "1320" Then _ " is incorrect.

Change this to "  If ActiveCell.Offset(0, -2).Value = "1320" Then"

See below,

- Ardhendu

    Do While ActiveCell <> ""  'Loops until the active cell is blank.
        If ActiveCell.Offset(0, -2).Value = "1320" Then
            ActiveCell.Offset(0, 1).FormulaR1C1 = _
            "https://testlink1320=" & " " & _
            ActiveCell.Offset(0, 0)
            ActiveCell.Offset(1, 0).Select
        Else
            ActiveCell.Offset(0, 1).FormulaR1C1 = _
            "https://testlink1330=" & " " & _
            ActiveCell.Offset(0, 0)
            ActiveCell.Offset(1, 0).Select
        End If
    Loop

Open in new window

hi Gtgloner,

I didn't see ur post. sorry abt that. You got it right first.

- Ardhendu
Eric, gtgloner got it first in the code he posted, even if he didn't detail the way he addressed your problem. I've requested reopening of the question should you want to reassign the points.

Thomas
Thanks, all.
I realize that the question has already been answered, but recorded macros are quite inefficient because of all the screen flickering and Select statements. I rewrote the macro to avoid both issues, so it should run faster and less annoyingly as a result.

Brad
Sub CellLooper()
Dim cel As Range, rg As Range
Dim sTestLink As String
Application.ScreenUpdating = False  'Eliminates screen flicker in Excel 2003 and earlier. Macro runs faster.
Set rg = Range("C2")    'First cell to be processed. Change this address to suit.
If rg.Offset(1, 0) <> "" Then Set rg = Range(rg, rg.End(xlDown))

For Each cel In rg.Cells
    sTestLink = IIf(cel.Offset(0, -2).Value = "1320", "1320=", "1330=")
    cel.Offset(0, 1).Formula = "https://testlink" & sTestLink & " " & cel
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Avatar of greenee

ASKER

sorry all, got the other email first for some reason.