vba Visual Basic 如果活动单元格包含文本“a”,则在右侧的单元格中插入 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Basic if active cell contains text "a" then insert a 1 into the cell to the right
提问by mhollander38
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.
任务是如果当前选定的单元格包含“a”,则在右侧的单元格中插入一个 1,并且(因为下面的数据)在下方插入一个新行并用 0 填充 1 下方的单元格。
So if the cell contains an "a" then when the macro runs it leaves; a 1 0
因此,如果单元格包含“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.
1 位于 a 右侧的单元格中,1 位于新行中,位于 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,
问题很明显,单元格的选择不起作用,我无法正确退出循环,
回答by Dr. belisarius
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
回答by GolezTrol
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.
你希望这段代码做什么?您首先为同一个字符串分配 4 个不同的值,然后将一个单元格与一个字符串进行比较,如果它们相同,则将两个单元格的值设置为您尚未初始化的两个字符串的值。
I think you meant to initialize text0, text1and text2as well, instead of initializing textfour times.
我认为你的意思是 initialize text0,text1而且text2,而不是初始化text四次。
I don't understand what you mean by 'edit loop'. There is no loop in your code.
我不明白你所说的“编辑循环”是什么意思。您的代码中没有循环。

