VBA If a cell contains a value in a range of cells ensure that another cell in a different column in the same row also has a value

DavyJones90

Board Regular
Joined
Feb 23, 2022
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hello I have a Macro which automatically creates a PDF of an Analysis sheet, now the problem I am facing is that I want to build in a check step to ensure the full analysis was completed fully,

So my idea is something like this


VBA Code:
'(FAKE CODE)
IF a cell in Range("N4:N14") <> 0 OR "" check the the same Row in Column("I")  and ensure that it's value is more than 0 OR "" Then

MsgBox "PDF Could not be created because a Value in Coloumn I is missing "

Else
ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Range("O4").NumberFormat = "dd/mm/yy"
    ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Range("O4").Value = Date
    ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Cells.Copy
    newWorkbook.Worksheets(1).Cells.PasteSpecial Paste:=xlPasteFormats
    newWorkbook.Worksheets(1).Cells.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    newWorkbook.Worksheets(1).Range("A:A").ClearContents
    newWorkbook.Worksheets(1).Name = "Values"
    
    Application.CutCopyMode = False
    Application.CutCopyMode = True
    newWorkbook.Sheets.Add.Name = "XL FILE"
    newWorkbook.Sheets("XL FILE").Move After:=Sheets("Values")
    ThisWorkbook.Worksheets("XL FILE").Cells.Copy
    newWorkbook.Worksheets("XL FILE").Cells.PasteSpecial Paste:=xlPasteFormats
    newWorkbook.Worksheets("XL FILE").Cells.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
    Application.CutCopyMode = False

    newWorkbook.SaveAs newWbPath
End if

Thanks in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Is this what you are trying?

VBA Code:
Option Explicit

Sub Sample()
    Dim aCell As Range
    Dim ws As Worksheet
  
    '~~> Change this to relevant sheet
    Set ws = Sheet1
  
    With ws
        '~~> Loop through the range and check if the range has value > 0
        For Each aCell In .Range("N4:N14")
            If Val(aCell.Value2) > 0 Then
                If Not Val(.Range("I" & aCell.Row).Value2) > 0 Then
                    MsgBox "PDF Could not be created because a Value in cell I" & aCell.Row & " is missing "
                    Exit Sub
                End If
            End If
        Next aCell
      
        '
        '~~> Rest of the code to create the pdf
        '
    End With
End Sub
 
Upvote 0
Solution
This would have worked perfectly, except for one small detail. I forgot to mention that the Value is a percentage value. It doesn't take > 0.1 for some reason
 
Upvote 0
This would have worked perfectly, except for one small detail. I forgot to mention that the Value is a percentage value. It doesn't take > 0.1 for some reason
Slightly confused. Why > 0.1?

Val(aCell.Value2) > 0 will catch decimal values as well.
 
Upvote 0
OK, it's probably me, but don't get what I am doing wrong.

So this is part of the sheet

MASTER CURRENCIES.xlsm
IJKLMN
1
210 M EXTREMESBREAKOUT LEVELS
3M.M ROC7(+)ROC7(-)LEVELSTR/SFTRATING
430%0.550.251.052SOFT1(+)
530%0.400.50.7STRONG1(-)
630%0.40.300 
70%0.300.300.854SOFT 
AUTO ENTRY POINTS
Cell Formulas
RangeFormula
I4:I7I4=OFFSET(Hourly!F$48,$A4,0)
J4:J7J4=OFFSET(Hourly!Q$50,A4,0)
K4:K7K4=OFFSET(Hourly!U$50,A4,0)
L4:L7L4=OFFSET(Hourly!K$49,A4,0)
M4:M7M4=OFFSET(Hourly!M$49,$A4,0)
N4N4=IF('USD PM H'!$Y$14="","",'USD PM H'!$Y$14)
N5N5=IF('USD PM H'!$Y$17="","",'USD PM H'!$Y$17)
N6N6=IF('USD PM H'!$Y$20="","",'USD PM H'!$Y$20)
N7N7=IF('USD PM H'!$Y$23="","",'USD PM H'!$Y$23)


and this is my code

VBA Code:
Dim aCell As Range
    Dim ws As Worksheet
    Set newWorkbook = Workbooks.Add
    Set ws = ThisWorkbook.Sheets("AUTO ENTRY POINTS")
    
  With ws
        '~~> Loop through the range and check if the range has value > 0
        For Each aCell In .Range("N4:N14")
            If Val(aCell.Value2) > 0 Then
                If Not Val(.Range("I" & aCell.Row).Value2) > 0 Then
                    MsgBox "PDF Could not be created because a Value in cell I" & aCell.Row & " is missing "
                    Exit Sub
                End If
            End If
        Next aCell


It only runs correctly when I set the Value of the cell in I to 100%
 
Upvote 0
Can you debug the For Each loop and tell me what values are you getting for aCell and .Range("I" & aCell.Row).Value2.
 
Upvote 0
aCell is "1(+)" and .Range("I" & aCell.Row).Value2. is 0.3
1652110010740.png
 
Upvote 0
Great. Now what do you get when you type

?Val(.Range("I" & aCell.Row).Value2) > 0

in Immediate Window at this point of time?
 
Upvote 0
OK I dont understand how or why it works, but this is what I changed and it works, Thanks for your help!


VBA Code:
            If Val(aCell.Value2) <> Empty Then
                If Not .Range("I" & aCell.Row).Value2 * 10 > 0 Then
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top