Niva
Niva

Reputation: 298

Define variable for equation

Hi there I have a code that checks through a column to see for any values >0 if there is it would copy paste that column to sheet one else it would just display a pop up message.

Tried doing the code but I am unable to define a Hence i would need some help with that. Thanks in adv:)

This is the code:

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

Upvotes: 1

Views: 38

Answers (2)

user4039065
user4039065

Reputation:

... that checks through a column to see for any values >0 if there is it would copy paste that column to sheet one ...

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

Upvotes: 2

skkakkar
skkakkar

Reputation: 2828

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

Please make the 2 changes indicated above. Your program should work.

Upvotes: 3

Related Questions