为方程定义变量

时间:2016-05-09 02:55:15

标签: excel vba excel-vba

您好,我有一个代码可以检查列以查看任何值> 0如果有,它会将该列复制粘贴到另一个列,否则它只会显示弹出消息。

尝试做代码,但我无法定义a因此我需要一些帮助。在谢谢你:)

这是代码:

Option Explicit

Sub TestPasteColumnData2()

    Dim lastrow As Long
    Dim i As Long
    lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 4 To lastrow
    a = Cells(i, "C").Value
    If a < 0 Then

     MsgBox ("No Value")
     Exit Sub

   Else

   Sheets("WF - L12 (3)").Columns(3).Copy Destination:=Sheets("Sheet1").Columns(3)

   End If

   Next
  MsgBox ("Done")
End Sub

Sub TestPasteColumnData3()

Dim lastcol As Long
Dim j As Long

    With Worksheets("WF - L12 (3)")
    lastcol = .Cells(4, Columns.Count).End(xlToLeft).Column
       For j = 3 To lastcol
        If CBool(Application.CountIfs(.Columns(j), ">0")) Then
            .Columns(j).Copy Destination:=Worksheets("Sheet1").Columns(3)
        Else
            MsgBox ("No Value")
            Exit Sub
        End If
        Next
    End With

    MsgBox ("Done")
End Sub

2 个答案:

答案 0 :(得分:3)

Option Explicit

Sub TestPasteColumnData2()

    Dim lastrow As Long
    Dim i As Long
    Dim a As Integer ' Added Declaration        
    lastrow = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 4 To lastrow
    a = Cells(i, 3).Value  ' Put column no. insted of "C"
    If a < 0 Then

     MsgBox ("No Value")
     Exit Sub

   Else

   Sheets("WF - L12 (3)").Columns(3).Copy Destination:=Sheets("Sheet1").Columns(3)

   End If

   Next
  MsgBox ("Done")
End Sub

请进行上述2项更改。你的程序应该可以工作。

答案 1 :(得分:2)

  

...检查列以查看是否有任何值&gt; 0如果存在,则会将该列复制粘贴到第一页...

Sub TestPasteColumnData2()

    With Worksheets("WF - L12 (3)")
        If CBool(Application.CountIfs(.Columns(3), ">0")) Then
            .Columns(3).Copy Destination:=Worksheets("Sheet1").Columns(3)
        Else
            MsgBox ("No Value")
            Exit Sub
        End If
    End With

    MsgBox ("Done")
End Sub