Visual Basic if active cell contains text “a” then insert a 1 into the cell to the right
-
27-10-2019 - |
سؤال
The task is if the currently selected cell contains an "a" then insert a 1 in the cell to the right and (because of data below) insert a new row below and fill the cell below the 1 with a 0.
So if the cell contains an "a" then when the macro runs it leaves; a 1 0
With the 1 being in the cell to the right of the a and the 1 being in a new row, in the cell directly below the 1.
The code I currently have is;
Sub ChangeAToCells()
Dim text As String
text = "a"
Dim text0 As String
text = "b"
Dim text1 As String
text = "0"
Dim text2 As String
text = "1"
If ActiveCell = text Then
ActiveCell.Formula = text1
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Offset(1, 0).Select
ActiveCell.Formula = text2
End If
End Sub
The problems are clear, the selection of cells isn't working and I can't get the loop exit right,
المحلول
Sub ChangeAToCells()
If ActiveCell = "a" Then
ActiveCell.Offset(0, 1) = 1
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Offset(1, 1) = 0
End If
End Sub
نصائح أخرى
What do you expect that code to do? You first assign 4 different values to the same string, then compare a cell with a string, and if they're the same, you set the value of two cells to the values of two strings you haven't initialized.
I think you meant to initialize text0
, text1
and text2
as well, instead of initializing text
four times.
I don't understand what you mean by 'edit loop'. There is no loop in your code.