简体   繁体   English

如果活动单元格包含文本“ a”,则为Visual Basic,然后在右侧的单元格中插入1

[英]Visual Basic if active cell contains text “a” then insert a 1 into the cell to the right

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,然后(由于下面的数据)在下方插入新行,并在1下方的单元格中填充0。

So if the cell contains an "a" then when the macro runs it leaves; 因此,如果单元格包含“ a”,则当宏运行时它将离开; a 1 0 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, 问题很明显,单元格的选择不起作用,我无法正确退出循环,

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. 首先,为同一字符串分配4个不同的值,然后将一个单元格与一个字符串进行比较,如果它们相同,则将两个单元格的值设置为尚未初始化的两个字符串的值。

I think you meant to initialize text0 , text1 and text2 as well, instead of initializing text four times. 我认为您也打算初始化text0text1text2 ,而不是初始化text四次。

I don't understand what you mean by 'edit loop'. 我不明白您所说的“编辑循环”是什么意思。 There is no loop in your code. 您的代码中没有循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM