How to use VBA to open input box of cell contains 0 and replace with value (variable) entered in a column

Shjph

New Member
Joined
Feb 23, 2022
Messages
3
Office Version
  1. 2011
Platform
  1. Windows
Hi I have written a macro below the checks for 0 in column AE and if it finds it opens an input box to enter a number over 0. This then replace the cell with the value entered.
However due to my limited knowledge I can only do it with one specific cell. Is there a way I can get it to check cells one by one in a column.
I.e start at cell AE1 going down the colours and check for cells containing 0. Then prompting input box each time a cell meeting this criteria is meet.
Below is my current macro.

Sub ENTER_VALUE()
Dim FileImport As String
Dim AnsBool As Boolean

AnsBool = True

If Range("AH11") = 0 Then

FileImport = InputBox("MISSING INVOICE AMOUNT, Please Provide Amount")

Do While AnsBool
If StrPtr(FileImport) = 0 Then Exit Sub
If FileImport <> "" Then Exit Do
MsgBox "Please Provide Reasoning"
FileImport = InputBox("MISSING INVOICE AMOUNT, Please Provide Amount")
Loop
End If

'You can change this to the range needed
Range("AH11").Value = FileImport

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,
welcome to forum

See if this update to your code does what you want

VBA Code:
Sub ENTER_VALUE()
    Dim FileImport  As Variant
    Dim rng         As Range, fnd As Range
   
    Const Prompt As String = "MISSING INVOICE AMOUNT"
   
    'search range
    Set rng = Range(Range("AE2"), Range("AE" & Rows.Count).End(xlUp))

    Do
        Set fnd = rng.Find(0, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            fnd.Select
            FileImport = InputBox(Prompt, "Please Provide Amount")
            'cancel pressed
            If StrPtr(FileImport) = 0 Then Exit Sub
            'inform user
            If Val(FileImport) = 0 Then MsgBox Prompt & Chr(10) & _
                                       "Please Provide Amount", 48, "Entry Required"
            'update range
            fnd.Value = Val(FileImport)
        End If
    Loop Until fnd Is Nothing
   
End Sub

I have assumed that the sheet being updated is the Activesheet

Dave
 
Upvote 0
Solution
Hi,
welcome to forum

See if this update to your code does what you want

VBA Code:
Sub ENTER_VALUE()
    Dim FileImport  As Variant
    Dim rng         As Range, fnd As Range
  
    Const Prompt As String = "MISSING INVOICE AMOUNT"
  
    'search range
    Set rng = Range(Range("AE2"), Range("AE" & Rows.Count).End(xlUp))

    Do
        Set fnd = rng.Find(0, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            fnd.Select
            FileImport = InputBox(Prompt, "Please Provide Amount")
            'cancel pressed
            If StrPtr(FileImport) = 0 Then Exit Sub
            'inform user
            If Val(FileImport) = 0 Then MsgBox Prompt & Chr(10) & _
                                       "Please Provide Amount", 48, "Entry Required"
            'update range
            fnd.Value = Val(FileImport)
        End If
    Loop Until fnd Is Nothing
  
End Sub

I have assumed that the sheet being updated is the Activesheet

Dave
Thank you so much this worked perfectly
 
Upvote 0
most welcome & appreciate your feedback

Dave
Hi Dave is it possible to add cell information into the input box,

For instance if macro finds 0 in cell AE11 the input box references the information from cell F11 in the wording . I have
 
Upvote 0
Hi Dave is it possible to add cell information into the input box,

For instance if macro finds 0 in cell AE11 the input box references the information from cell F11 in the wording . I have

Hi,
you can include cell address where shown in BOLD

Rich (BB code):
Sub ENTER_VALUE()
    Dim FileImport  As Variant
    Dim rng         As Range, fnd As Range
    
    Const Prompt As String = "MISSING INVOICE AMOUNT"
    
    'search range
    Set rng = Range(Range("AE2"), Range("AE" & Rows.Count).End(xlUp))

    Do
        Set fnd = rng.Find(0, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            fnd.Select
            FileImport = InputBox(Prompt & Chr(10) & fnd.Address, "Please Provide Amount")
            'cancel pressed
            If StrPtr(FileImport) = 0 Then Exit Sub
            'inform user
            If Val(FileImport) = 0 Then MsgBox Prompt & Chr(10) & _
                                       "Please Provide Amount", 48, "Entry Required"
            'update range
            fnd.Value = Val(FileImport)
        End If
    Loop Until fnd Is Nothing
   
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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