仅在空白(空)单元格中按数字顺序计数

时间:2017-08-14 13:35:21

标签: loops for-loop counter next

我试图使用VBA自动填充(1,2,3,....)列" A"跳过非空/空行。例如,如果" A3"中有文本/数据。和" A5",代码计算如下:

"A1" = 1              "B1" = text/data
"A2" = 2              "B2" = text/data
"A3" = text/data      "B3" = text/data
"A4" = 3              "B4" = text/data
"A5" = text/data      "B5" = text/data
"A6" = 4              "B6" = text/data
"A7" = 5              "B7" = text/data

......等等

到目前为止,我只能跳过以前输入文字的行,但这个计数仍在继续,好像它没有跳过任何单元格一样。

请注意:我在#34; B"栏中使用.End(xlDown).Count确定柜台应该走多远。

这是我到目前为止所拥有的

Sub Counter()

Dim NoF As Long
Dim Test As Long

NoF = Range("B1", Range("B1").End(xlDown)).Count

For i = 1 To NoF

    If Cells(i, 1) = "" Then
        ActiveSheet.Cells(i, 1).Value = i
    ElseIf Cells(i, 1) <> "" Then
        ActiveSheet.Cells(i, 1).Offset(i + 1, 1).Select
    End If

Next i

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用第二个变量来计算带有文本的行。将它在for循环外部初始化为零,如果有一些文本则添加1。接下来,您只需要从i中减去它。

   func prepareMail(data:[Double]) {
    // Compose the mail
    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self
    mailComposer.setToRecipients(["mail@mail.com"])
    mailComposer.setSubject("subject")
    mailComposer.setMessageBody("Hello ", isHTML: false)

    // Name data files (accelerometer + label)
    let fileName = "file"

    if let dataToAttach = data.map({String($0)}).joined(separator: "\n").data(using: .utf8)
    {
        mailComposer.addAttachmentData(dataToAttach, mimeType: "text/plain", fileName: "\(fileNames[i])")
        self.present(mailComposer, animated: true, completion: nil)
    }

}

您不需要j = 0 For i = 1 To NoF If Cells(i, 1) = "" Then ActiveSheet.Cells(i, 1).Value = i - j ElseIf Cells(i, 1) <> "" Then ActiveSheet.Cells(i, 1).Offset(i + 1, 1).Select j = j + 1 End If Next i ,这也适用

Offset