这里是我的情况:VBA - 如果列B中的单元格不是空白,则列A = 1
列A为空。 B列是客人的房间号码 C列是该房间中客人的姓名
我想要统计有多少房间被占用。所以我把计数公式,但结果是0。我不知道为什么..
下面是代码:
Sheets("Champagne").Select
Range("B2").AutoFill Destination:=Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
LastRow = Range("A2").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A2:A" & LastRow & ")"
LRowA = [A4200].End(xlUp).Address
Range("A:A").Interior.ColorIndex = xlNone
Range("A2:" & LRowA).Interior.ColorIndex = 33
Range("A:A").HorizontalAlignment = xlCenter
所以后来我试图把一个公式,如果B柱说作为任何数字(房间号),它将在列A中算作1。然后在列A末尾加上一个总和。
这是我试图放置的代码,但它将123456放入C栏。
Sheets("Champagne").Select
For Each Cel In Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
If Cel.Value <> "" Then Cel.Offset(1, 0).Value = "123456"
Range("A2").AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).Row)
LastRow = Range("A2").End(xlDown).Row
Next
Range("B2").AutoFill Destination:=Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
LastRow = Range("A2").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A2:A" & LastRow & ")"
LRowA = [A4200].End(xlUp).Address
Range("A:A").Interior.ColorIndex = xlNone
Range("A2:" & LRowA).Interior.ColorIndex = 33
Range("A:A").HorizontalAlignment = xlCenter
如果你有第一个代码的答案,我把它以及....
一个简单的工作表'COUNTIF'似乎更合适。 – Jeeped
@Jeeped,你可以把它的代码在VBA的数量if..please ..作为一个答案.... – JohanEs
如果列A是空的列B&C可能会或可能不会被填充,那么没有确定将计数放在A列中的哪个位置。选择另一个填充房间数据范围的列,而不管房间是否已满。将样本数据与预期结果一起提供。房间号码**总是**号码还是一些文字(例如101A)? – Jeeped