Consulting

Results 1 to 5 of 5

Thread: Solved: VBA Offset help

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: VBA Offset help

    I am trying to do the following.

    I am copying data from one sheet to another and would like if in column B data starts with "DC" then Column C would be "Desktop" else "Laptop"

    I tried the following

    [VBA] If Left(Range("B7:B400").Value, 2) = "DC" Then

    Range("B7:B400").Offset(0, 1).Value = "Desktop"

    Else

    Range("B7:B400").Offset(0, 1).Value = "Laptop"

    End If[/VBA]

    But got an error.

    Any help would be great.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Try like:
    [VBA]Option Explicit

    Sub Example()
    Range("B1:B25").FormulaArray = "=IF(LEFT(A1:A25,2)=""DC"",""Desktop"",""Laptop"")"
    Range("B1:B25").Value = Range("B1:B25").Value
    End Sub[/VBA]

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    GTO that works but problem with it is if cell is blank it's putting "Laptop".
    Can we have it look at cell if value = "" then don't do anything?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Change the formula that we are temporarily plunking in.

    [VBA]Range("B1:B25").FormulaArray = "=IF(A1:A25="""","""",IF(LEFT(A1:A25,2)=""DC"",""Desktop"",""Laptop"")) "[/VBA]

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Perfect

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •