Hi,
I am relatively new to VBA so still struggling to fully understand the syntax. I have been working on an automation macro & I am quite close to completion I just have a step that is appearing more difficult than I would've thought.
I am trying to provide a pop-up that alerts the user that there are no negative values in the final column. I have attempted two ways of going about this, firstly I tried to acquire the column letter of the final col with data to then loop through that as follows:
Sub FindNeg()
Dim Ws As Worksheet
Dim LastColumn As Long
Dim myCol As String
LastColumn = Ws(2, Ws.Columns.Count).Cells.End(xlToRight).Column
myCol = ColumnLetter(LastColumn)
For Each I In Range("myCol:myCol")
If I.Value > 0 Then
MsgBox ("No negative Values")
End If
Next I
End Sub
using this function I found online
Function ColumnLetter(ColumnNumber As Long) As String
ColumnLetter = Split(Cells(1, ColumnNumber).Address(True, False), "$")(0)
End Function
My second attempt feels more successful as I am not obtaining any errors but I am still not getting any pop-ups despite there being no negative values in the column. Here is the second attempt:
Sub Macro3()
For I = Range("A1").End(xlDown).End(xlToRight).End(xlUp) To Range("A1").End(xlDown).End(xlToRight)
For Each C In I
If C > 0 Then
MsgBox ("There are no negative values!")
Else
End If
Next C
Next
End Sub
If anyone could provide more insight into why either of the two may not be working that would be great! The first of the two obtains a run-time error '91': Object variable or with block variable not set (in regards to the LastColumn variable.
Whilst the second attempt doesn't demonstrate any error value it just doesn't provide the required popup.
I am relatively new to VBA so still struggling to fully understand the syntax. I have been working on an automation macro & I am quite close to completion I just have a step that is appearing more difficult than I would've thought.
I am trying to provide a pop-up that alerts the user that there are no negative values in the final column. I have attempted two ways of going about this, firstly I tried to acquire the column letter of the final col with data to then loop through that as follows:
Sub FindNeg()
Dim Ws As Worksheet
Dim LastColumn As Long
Dim myCol As String
LastColumn = Ws(2, Ws.Columns.Count).Cells.End(xlToRight).Column
myCol = ColumnLetter(LastColumn)
For Each I In Range("myCol:myCol")
If I.Value > 0 Then
MsgBox ("No negative Values")
End If
Next I
End Sub
using this function I found online
Function ColumnLetter(ColumnNumber As Long) As String
ColumnLetter = Split(Cells(1, ColumnNumber).Address(True, False), "$")(0)
End Function
My second attempt feels more successful as I am not obtaining any errors but I am still not getting any pop-ups despite there being no negative values in the column. Here is the second attempt:
Sub Macro3()
For I = Range("A1").End(xlDown).End(xlToRight).End(xlUp) To Range("A1").End(xlDown).End(xlToRight)
For Each C In I
If C > 0 Then
MsgBox ("There are no negative values!")
Else
End If
Next C
Next
End Sub
If anyone could provide more insight into why either of the two may not be working that would be great! The first of the two obtains a run-time error '91': Object variable or with block variable not set (in regards to the LastColumn variable.
Whilst the second attempt doesn't demonstrate any error value it just doesn't provide the required popup.