Consulting

Results 1 to 5 of 5

Thread: Solved: VBA code for "or" + "and" functions

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location

    Solved: VBA code for "or" + "and" functions

    Dear all,

    I´m trying to create a code to import for a new sheet the values of a centain cell with a condition. For example, i would like to import the value of cell A1 if B1 is fill with certain values (S, T1, T2, T3, T4). I thought in use the function "OR" but don´t know how to write than.

    The code i wrote is:

    ...
    For i = 8 To 55
    If (cont = 0) Then

    If (Cells(i, 28) = "S" Or "T1" Or "T2" Or "T3" Or "T4") Then

    'copia status
    Range("C" & i).Copy
    ActiveWindow.ActivateNext
    Range("D" & k).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    ActiveWindow.ActivateNext
    ...

    Any suggestion to help me?

    Thanks and a good Weekend to all

    Alan

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    If Cells(i, 28).Value = "S" Or Cells(i, 28).Value = "T1" Or _
    Cells(i, 28).Value = "T2" Or Cells(i, 28).Value = "T3" Or _
    Cells(i, 28).Value = "T4" Then
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    2 suggestions:
    1. change
    [vba]If (Cells(i, 28) = "S" Or "T1" Or "T2" Or "T3" Or "T4") Then[/vba] to [vba]If (Cells(i, 28) = "S" Or Cells(i, 28) = "T1" Or Cells(i, 28) = "T2" Or Cells(i, 28) = "T3" Or Cells(i, 28) = "T4") Then[/vba]
    2. Use
    [vba]Select Case Cells(i, 28)
    Case "S", "T1", "T2", "T3", "T4"
    'code which does stuff
    End Select
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBA wants you to ask individual questions instead of "if this = that or that or that"
    Try the line below in your code
    [VBA]If Cells(i, 28) = "S" Or Cells(i, 28) = "T1" Or Cells(i, 28) = "T2" Or Cells(i, 28) = "T3" Or Cells(i, 28) = "T4" Then[/VBA]

  5. #5
    VBAX Regular
    Joined
    Sep 2007
    Posts
    14
    Location
    Dear All,

    Many thanks for all the help and support. With your help I managed to solve this issue.

    I wish a nice day to everyone.

    Best Regards,
    Alan

Posting Permissions

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