0
因此,我制作了一个相当简单的宏,它为列表添加了一条新记录。当字段为空时,我试图保持它不工作,所以我已经实现了对输入字段是否为空的检查。但是,我似乎无法让它工作。测试VBA宏中的空字段
下面的代码:
Sub AddRecord_Click()
Dim itemName As String
Dim itemPrice As Integer
Dim nextRow As Integer
Dim idNumber As Integer
nextRow = Sheets("REVENUES").Range("B" & Rows.Count).End(xlUp).Row + 1
itemName = Range("A2").Value
itemPrice = Range("B2").Value
idNumber = (Range("A" & (nextRow - 1)).Value) + 1
If Range("A2").Value <> "" And Range("B2").Value <> "" Then
Range("A" & nextRow).Value = idNumber
Range("B" & nextRow).Value = itemName
Range("C" & nextRow).Value = itemPrice
Range("D" & nextRow).Value = Date
Range("A2").Value = ""
Range("B2").Value = ""
End If
End Sub
所有的数据活动都发生在“收入”上吗?如果您正在处理多个工作表,我可以看到这是一个问题,因为所有代码默认都会引用活动工作表。 – Dustin 2014-12-03 03:32:32
我强烈建议使用.Cells(iRow,iCol)而不是Range(R1C1),因为使用变量而不是“A”,“B”等来循环和切换列更容易。 – peege 2014-12-03 03:54:33
“Can似乎没有得到它的工作“是如此广泛的投诉。谨慎缩小它? (我怀疑@Dustin已经钉了它)。 – 2014-12-03 04:03:46