在VBA代码

时间:2016-02-05 03:34:47

标签: excel-vba vba excel

我在VBA很新,所以我确定我错过了一些简单的事情......我收到编译错误"循环没有做"

被称为GrabDataFromMinutes的函数我已经成功测试了它自己。感谢任何帮助,谢谢!

Public Sub CopyAllData()

Dim Location As String
Location = ActiveCell.Address
Dim CellValue As String
CellValue = ActiveCell.Value

Do
    If IsEmpty(CellValue) = True Then 'If cell is empty skip row'
        ActiveCell.Offset(rowOffset:=1, ColumnOffset:=-1).Activate
        Loop

If Location <> "C350" Then 'run the command unless EOF'
        Application.Run ("GrabDataFromMinutes")
        MsgBox "I got here"
        Location = ActiveCell.Address
        Loop

    End If

Exit Do

End Sub

3 个答案:

答案 0 :(得分:0)

正如Scott正确提到的,你需要将LOOP声明放在相应的位置。 Click Here

您的代码应该是这样的

Public Sub CopyAllData()

Dim Location As String
Location = ActiveCell.Address
Dim CellValue As String
CellValue = ActiveCell.Value

Do
    If IsEmpty(CellValue) = True Then 'If cell is empty skip row'
        ActiveCell.Offset(rowOffset:=1, ColumnOffset:=-1).Activate

    If Location <> "C350" Then 'run the command unless EOF'
        Application.Run ("GrabDataFromMinutes")
        MsgBox "I got here"
        Location = ActiveCell.Address

    End If

  End If
Loop

End Sub

答案 1 :(得分:0)

这是我自己问题的最终解决方案。其他答案提供的信息有助于解决我的问题。谢谢大家!

Public Sub CopyAllData()
'Declarations'
Dim CellValue As String
Dim LastRow As Integer
Dim CurrentRow As Integer

Application.Run ("CopyMinuteHeaders") 'Copy Headers and setup sheet'

'Initialize values'
CellValue = ActiveCell.Value
CurrentRow = ActiveCell.Row
LastRow = ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + 1

Do While LastRow <> CurrentRow

        If CurrentRow >= LastRow Then
        Exit Sub
        End If

        If CellValue = "" Then 'If cell is empty skip row'
        Do While CellValue = "" 'Skip multiple rows that are empty'
            ActiveCell.Offset(rowOffset:=1, ColumnOffset:=0).Activate
            CellValue = ActiveCell.Value
            Loop

        End If

        If CurrentRow <> LastRow Then
        Application.Run ("GrabDataFromMinutes")
        CurrentRow = ActiveCell.Row
        CellValue = ActiveCell.Value
        End If

Loop


End Sub

答案 2 :(得分:0)

我不知道你的GrabDataFromMinutes宏做了什么,我认为它会处理活跃的细胞。通常我会尽量不使用select或activate来提供代码。如果您要提供GrabDataFromMinutes的代码,可能会找到更好的解决方案。

在使用此代码的同时,它将仅选择A列中的非空白单元格并调用otherMacro

Sub LoopNonBlanks()
    Dim Lstrw As Long
    Dim Rng As Range
    Dim c As Range

    Lstrw = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng = Range("A2:A" & Lstrw).SpecialCells(xlCellTypeConstants, 23)

    For Each c In Rng.Cells
        c.Select
        otherMacro    'calls the otherMacro
    Next c
End Sub
Sub otherMacro()
    MsgBox "This is the other macro " & ActiveCell.Address & " value is ..." & ActiveCell.Value
End Sub