2016-06-16 9 views
0

私のコードを修正してセル範囲を追加するのに役立つ必要があります。カラム範囲テーブル内の各セルについて

セルの値を間違って入力すると、正しい値に変更しようとしています。しかし、テーブルが追加されるので、柔軟なコードにする必要があります。コードは現在、エラーコード424の先頭のサブメニューで停止しています。私はVBAにはかなり新しいです。

Sub Consolidates() 
Dim datasheet As Worksheet 

Set datasheet = ThisWorkbook.Sheets("sheet1") 

lr = datasheet.Cells(Rows.Count, 9).End(xlUp).Row 
For x = 2 To lr 


If cell.Value = "B" Or "BR" Or " Then 
cell.Value = "BR" 
    ElseIf cell.Value = "CL" Or "CR" _ 
     Then cell.Value = "CR" 
       ElseIf cell.Value = "" Then 
       End If 
       Next x 
End Sub 

答えて

0

セルにはどのセルへの参照が必要です。また、そのようなまたはステートメントを使用することはできません。以下の簡単な方法でそれを実現します。

For x = 1 To lr 
    If Cells(x, 9).Value = "B" Or Cells(x, 9).Value = "BR" Then 
     Cells(x, 9).Value = "BR"   
    ElseIf Cells(x, 9).Value = "CL" Or Cells(x, 9).Value = "CR" Then 
     Cells(x, 9).Value = "CR" 
    End If 
Next x 

あなたはselect文に

For x = 1 To lr 
    Select Case Cells(x, 9).Value 
     Case "B", "BR" 
      Cells(x, 9).Value = "BR" 
     Case "CL", "CR" 
      Cells(x, 9).Value = "CR" 
    End Select 
Next x 

を考慮する必要があり、それは大文字と小文字が区別されますので、あなたがLCASEを追加することができますあなたにいくつかの時間を節約することができ

For x = 1 To lr 
    Select Case LCase(Cells(x, 9).Value) 
     Case "b", "br" 
      Cells(x, 9).Value = "BR" 
     Case "cl", "cr" 
      Cells(x, 9).Value = "CR" 
    End Select 
Next x 
0

を次のようにあなたが何かを使用することができます
Option Explicit 

Sub Consolidates() 
    Dim stringsToSearch As String, stringToSubstitute As String 
    Dim stringsToSearchArr As Variant, stringToSubstituteArr As Variant 

    ' here define the "table" 
    stringsToSearch = "B,CL" '<--| type here the strings to be searched for 
    stringToSubstitute = "BR,CR" '<--| type here the corresponding strings to change searched ones into 

    stringsToSearchArr = Split(stringsToSearch, ",") '<--| turn "stringsToSearch" into an array 
    stringToSubstituteArr = Split(stringToSubstitute, ",") '<--| turn "stringToSubstitute" into an array 

    With ThisWorkbook.Sheets("sheetTest") '<--| change "sheetTest" with your actual sheet name 
     With .Range("I2:I" & .Cells(.Rows.Count, 9).End(xlUp).Row) '<--| consider all cells in column "I" from row 2 to last non empty one 
      For i = LBound(stringsToSearchArr) To UBound(stringsToSearchArr) '<--| loop through the "table" 
       .Replace What:=stringsToSearchArr(i), Replacement:=stringToSubstituteArr(i), LookAt:=xlWhole, MatchCase:=True '<--| find current string and replace it with its corresponding replacement 
      Next i 
     End With 
    End With 
End Sub 
関連する問題