Help needed to modify or change code

John T

Board Regular
Joined
Nov 28, 2013
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Hi,

I currently have the following code which would put the inputted value into cell G40.
However my spreadsheet has now changed such that the destination cell wont always be row 40 in column G.
Can you ammend my code so that the figure in the MsgBox is inputted into the correct cell.

If it helps in the adajacent cell in column F is the word "Actual Cash". Maybe that cell could be found and then offest one to the right
Or the cell i need in column G is the second last cell in the column with data. Maybe a code that starts from the bottom and finds the first cell with data and moves one up.
Thanks in advance.



Excel Formula:
Sub MM1()
Dim ans As Variant
Do
ans = Application.InputBox("Enter PDQ Z Total", Type:=2)
 If ans = "" Or TypeName(ans) = "Boolean" Then
    MsgBox "You must enter a PDQ Value", vbCritical, "Invalid Value"
 ElseIf TypeName(ans) <> "Double" Then
    Range("G40").Value = ans
    Exit Sub
 End If
Loop
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think i've fixed it by adding the following but if there's a better way then please let me know.

Excel Formula:
Sub MM1()
Dim ans As Variant
Do
ans = Application.InputBox("Enter PDQ Z Total", Type:=2)
 If ans = "" Or TypeName(ans) = "Boolean" Then
    MsgBox "You must enter a PDQ Value", vbCritical, "Invalid Value"
 ElseIf TypeName(ans) <> "Double" Then
  Range("G1000").End(xlUp).Select
 ActiveCell.Offset(-1, 0).Select
 ActiveCell.Value = ans
    
    
    'Range("G40").Value = ans
    Exit Sub
 End If
Loop
End Sub
 
Upvote 0
You can simplify all these lines:
VBA Code:
  Range("G1000").End(xlUp).Select
 ActiveCell.Offset(-1, 0).Select
 ActiveCell.Value = ans
down to just this:
VBA Code:
  Range("G1000").End(xlUp).Offset(-1, 0).Value = ans
(there usually isn't any need to select the cells to work with them - most of that is the Macro Recorder's literal recording of each distinct step).
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,457
Members
449,083
Latest member
Ava19

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