简体   繁体   中英

VBA if Column(“A”).Cell(i).Value begins with “x” then Column(“G”).Cell(i).value = “String”

How can I write this in VBA? I know it's a lot of criteria, but I tried to be thorough.

IF Cell.Value in Column "Job Number" (column "A") begins with (11*,12*,13*,14*) THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "AIRFRAME"

IF Cell.Value in Column "Job Number" (column "A") begins with "35W" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "WIRING"

IF Cell.Value in Column "Job Number" (column "A") begins with "34S" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "SOFTWARE"

IF Cell.Value in Column "Job Number" (column "A") begins with "32EB" OR "35EB" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "AVIONICS"

IF Cell.Value in Column "Job Number" (column "A") begins with "32EF" OR "35EF" BUT <> Cell.Value in Column "TEAM #" (column "H") = "Q3S251" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "AVIONICS"

IF Cell.Value in Column "Job Number" (column "A") begins with "23X" BUT <> Cell.Value in Column "TEAM #" (column "H") = "Q3S251" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "UTILITIES & SUBSYSTEMS"

IF Cell.Value in Column "TEAM #" (column "H") = "Q3S251" THEN cell.value in Column "TEAME NAME" (column "G").ROW(i) = "FUNCTIONAL TEST"

IF Cell.Value in Column "TEAME NAME" (column "G").ROW(i) = "PROPULSION" THEN Cell.Value = "UTILITIES"

Pretty much this is a code, which you can you to start:

Option Explicit

Public Sub TestMe()

    Dim my_cell As Range

    Set my_cell = ActiveSheet.Cells(1, 1)

    Select Case True

        Case Left(my_cell, 3) = 11 Or Left(my_cell, 2) = 12:
            my_cell.Offset(0, 5) = "new value 11"
        Case Left(my_cell, 3) = "34S":
            my_cell.Offset(0, 5) = "new value 34S"
        Case Else:
            my_cell.Offset(0, 5) = "case else"
    End Select

End Sub

As a next step, make sure that you set my_cell correctly and make the conditions and the results the way you want them. Enjoy! :)

Sub ScrubSheets()

Dim lastRow As Long
Dim myRow As Long

Application.ScreenUpdating = False

'   Find last row in column A
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Loop for all cells in column A from rows 2 to last row
For myRow = 2 To lastRow
'       First check value of column G
    If Cells(myRow, "G") = "PROPULSION" Then
        Cells(myRow, "G") = "UTILITIES"
    Else
'           Then check column H
        If Cells(myRow, "H") = "Q3S2531" Then
            Cells(myRow, "G") = "FUNCTIONAL TEST"
        Else
 '               Check four character prefixes
            Select Case Left(Cells(myRow, "A"), 4)
                Case "32EB", "35EB", "32EF", "35EF"
                    Cells(myRow, "G") = "AVIONICS"
                Case Else
 '                       Check 3 character prefixes
                    Select Case Left(Cells(myRow, "A"), 3)
                        Case "35W"
                            Cells(myRow, "G") = "WIRING"
                        Case "34S"
                            Cells(myRow, "G") = "SOFTWARE"

                        Case Else
 '                               Check 2 character prefixes
                            Select Case Left(Cells(myRow, "A"), 2)
                                Case "11", "12", "13", "14"
                                    Cells(myRow, "G") = "AIRFRAME"
                             Case "23"
                                    Cells(myRow, "G") = "UTILITIES" & " & " & "SUBSYSTEMS"
                            End Select
                    End Select
            End Select
        End If
    End If
Next myRow

Application.ScreenUpdating = True

'   Message that macro is complete
    MsgBox "Macro complete!", vbOKOnly

End Sub

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