[英]VBA *For Each Cell Loop* IF (Value in Column 1) = x AND IF (Equivalent Value In Column 2) >7 THEN
[英]VBA if Column(“A”).Cell(i).Value begins with “x” then Column(“G”).Cell(i).value = “String”
如何在VBA中編寫此代碼? 我知道這是很多標准,但是我努力做到透徹。
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"
這幾乎是一個代碼,您可以從中開始:
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
下一步,請確保正確設置my_cell
並按照所需的方式設置條件和結果。 請享用! :)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.