Sub Macro()
If (Range("B1").Value = "X") Then
Range("C1").Value = Range("A1").Value + 30
ElseIf (Range("B1").Value = "Y") Then
Range("C1").Value = Range("A1").Value + 10
ElseIf (Range("B1").Value = "Z") Then
Range("C1").Value = Range("A1").Value
End If
End Sub
いろいろな書き方がありますけれど、なるべく元の式を崩さない方向で作りました
Sub Macro1() Dim lasrRow As Integer lastRow = Range("A65536").End(xlUp).Row For i = 1 To lastRow If (Range("B" & i).Value = "X") Then Range("C" & i).Value = Range("A" & i).Value + 30 ElseIf (Range("B" & i).Value = "Y") Then Range("C" & i).Value = Range("A" & i).Value + 10 ElseIf (Range("B" & i).Value = "Z") Then Range("C" & i).Value = Range("A" & i).Value End If Next i End Sub
いろいろな書き方がありますけれど、なるべく元の式を崩さない方向で作りました
Sub Macro1() Dim lasrRow As Integer lastRow = Range("A65536").End(xlUp).Row For i = 1 To lastRow If (Range("B" & i).Value = "X") Then Range("C" & i).Value = Range("A" & i).Value + 30 ElseIf (Range("B" & i).Value = "Y") Then Range("C" & i).Value = Range("A" & i).Value + 10 ElseIf (Range("B" & i).Value = "Z") Then Range("C" & i).Value = Range("A" & i).Value End If Next i End Sub
こちらは参考になるでしょうか。Excel 2007で実行確認しました。
Sub myMacro() Dim i As Integer Dim sheetobj As Worksheet Set sheetobj = ThisWorkbook.Worksheets(1) With sheetobj For i = 1 To LastRow(sheetobj, 1) If (.Cells(i, 2).Value = "X") Then .Cells(i, 3).Value = .Cells(i, 1).Value + 30 ElseIf (.Cells(i, 2).Value = "Y") Then .Cells(i, 3).Value = .Cells(i, 1).Value + 10 ElseIf (.Cells(i, 2).Value = "Z") Then .Cells(i, 3).Value = .Cells(i, 1).Value End If Next i End With MsgBox "OK!" End Sub '最終行を求める関数 Function LastRow(sheetobj As Worksheet, C) LastRow = sheetobj.Cells(sheetobj.Rows.Count, C).End(xlUp).Row End Function
コメント(0件)