Visual Basic 如果活动单元格包含文本“a”然后在右边的单元格中插入1
任务是如果当前选定的单元格包含“a”,则在右侧的单元格中插入 1,并(因为下面的数据)在下面插入一个新行,并用 0 填充 1 下面的单元格。
因此,如果单元格包含一个“a”,然后当宏运行时它离开; 一个 1 0
1 位于 a 右侧的单元格中,1 位于新行中,位于 1 正下方的单元格中。
我当前拥有的代码是;
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 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您希望该代码做什么?首先为同一个字符串分配 4 个不同的值,然后将一个单元格与一个字符串进行比较,如果它们相同,则将两个单元格的值设置为两个尚未初始化的字符串的值。
我认为您的意思是同时初始化
text0
、text1
和text2
,而不是初始化text
四次。我不明白你所说的“编辑循环”是什么意思。您的代码中没有循环。
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
andtext2
as well, instead of initializingtext
four times.I don't understand what you mean by 'edit loop'. There is no loop in your code.