VBA to identify and re-enter value in last cell in column

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

An anomaly in my sheet code means I need to re-input a value in the last cell in a specific column for a cell value in another sheet to register correctly.

This is my code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Range("MilesToNextYearEndTotal") < 0 Then
  MsgBox "Re-enter latest distance in Daily Tracking sheet", vbExclamation, "Y/E Total negative value anomaly correction"
  Sheets("Daily Tracking").Select
  End If
The below code, courtesy of DanteAmor, then selects the first blank cell in the Daily Tracking sheet in the current year column
VBA Code:
Dim f As Range
  Dim i As Long
 
  Set f = Range("D1", Cells(1, Columns.Count).End(1)).Find(Year(Date), , xlValues)
  If Not f Is Nothing Then
      For i = 2 To Rows.Count
        If Cells(i, f.Column).Value = "" Then
          If i = 61 Then
            If Day(DateSerial(Year(Date), 3, 1) - 1) = 29 Then
              Cells(i, f.Column).Select
              Exit Sub
            End If
          Else
            Cells(i, f.Column).Select
            Exit Sub
          End If
        End If
      Next
  End If
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

What I'm looking for is for the above to be adapted slightly, so instead of selecting the first blank cell, it selects the cell above it, identifies the value in that cell and 're-enters' it.

Many thanks!
 
Last edited:
Mind if I ask a couple of questions?
1) what is the anomaly you are encountering, and
2) Dante's code is the answer to the question you asked here. What guarantees that the first blank cell in the Daily Tracking sheet in the current year column is the appropriate row for the date you're dealing with?
Of course!
  • The anomaly relates to the value in sheet Training Log cell F2. F2 represents the number of miles I must run to surpass the next historic year end total, which is calculated in the 'Daily Tracking' sheet.
  • The cell updates from a cell in the 'Daily Tracking' sheet that was created many years ago that calculates the next year end total to beat.
  • For some reason that I can't understand, the value shown in Training Log F2 is negative when it surpasses a year end total value in Daily Tracking sheet and I don't understand why because it will always be the number of miles to the next year end total and cannot possibly be a negative value.
  • However, for a reason I don't understand it is corrected when I re-enter the mileage value I have just input that exceeds the total in the cell above the result of Dante's original code that you linked to (which locates the first empty cell - I think that answers your second question).
  • Hence my request for code that would re-enter it for me. If I could identify and correct the source of the anomaly myself then there would be no need for this workaround, but it's the only way I know how with my limited knowledge.
  • The relevant cells in Daily Tracking are below
Years:
test.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
3691981198219841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
Daily Tracking

Year End Mileages
test.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
3752963340533175412929331101538788655267131733252026157953811254131153136223071549132820901216964326121335104119147138979347539
Daily Tracking
Cell Formulas
RangeFormula
Y375:Z375Y375=SUMPRODUCT(--ISNUMBER($A2:$A367), Y2:Y367)
AA375:AO375AA375=SUMPRODUCT(--(ISNUMBER($A$1:$A$370)),(AA$1:AA$370))
Named Ranges
NameRefers ToCells
EntRng=OFFSET('Daily Tracking'!$Y$2:$Y$367,0,YEAR(TODAY())-2005)AO375
Cells with Conditional Formatting
CellConditionCell FormatStop If True
AO375Cell Valuebetween 1000 and 1249textYES
AO375Cell Valuebetween 1250 and 1499textYES
AO375Cell Value>=1500textYES
AN375Cell Valuebetween 1000 and 1249textYES
AN375Cell Valuebetween 1250 and 1499textYES
AN375Cell Value>=1500textYES
AM375Cell Valuebetween 1000 and 1249textYES
AM375Cell Valuebetween 1250 and 1499textYES
AM375Cell Value>=1500textYES
AL375Cell Valuebetween 1000 and 1249textYES
AL375Cell Valuebetween 1250 and 1499textYES
AL375Cell Value>=1500textYES
AF375:AK375Cell Valuebetween 1000 and 1249textYES
AF375:AK375Cell Valuebetween 1250 and 1499textYES
AF375:AK375Cell Value>=1500textYES
B375:AE375,AP375:CC375Cell Valuebetween 1000 and 1249textYES
B375:AE375,AP375:CC375Cell Valuebetween 1250 and 1499textYES
B375:AE375,AP375:CC375Cell Value>=1500textYES

Next Year End Mileage Total to beat and number of years beaten so far:
test.xlsm
CH
375579
37622
Daily Tracking
Cell Formulas
RangeFormula
CH375CH375=SMALL(PreYTD,Counter)
Named Ranges
NameRefers ToCells
Counter='Daily Tracking'!$CH$376CH375
CurYTD=OFFSET('Daily Tracking'!$Y$375,0,YEAR(TODAY())-2005)CH375
MilesSince1981='Daily Tracking'!$CG$375CH375
PreYTD='Daily Tracking'!$B$375:INDEX('Daily Tracking'!$375:$375,1,DailyTrackingColumn-1)CH375
'Daily Tracking'!solver_opt='Daily Tracking'!$S$375CH375

Training Log F2
test.xlsm
F
240
Training Log
Cell Formulas
RangeFormula
F2F2=('Daily Tracking'!CH375-C5)
Named Ranges
NameRefers ToCells
CurGoal='Daily Tracking'!$CH$375F2
VBA_YTD_MILES='Training Log'!$C$5F2

Training Log C5
test.xlsm
C
5529
Training Log
Cell Formulas
RangeFormula
C5C5=INDEX('Daily Tracking'!375:375,1,DailyTrackingColumn)
Named Ranges
NameRefers ToCells
CurGoal='Daily Tracking'!$CH$375C5
CurYTD=OFFSET('Daily Tracking'!$Y$375,0,YEAR(TODAY())-2005)C5
DailyTrackingColumn='Daily Tracking'!$CH$1C5
MilesSince1981='Daily Tracking'!$CG$375C5
PreYTD='Daily Tracking'!$B$375:INDEX('Daily Tracking'!$375:$375,1,DailyTrackingColumn-1)C5
'Daily Tracking'!solver_opt='Daily Tracking'!$S$375C5
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C5Expression=C5>D5textNO
C5Cell Valuebetween 499 and 749textNO
C5Cell Valuebetween 750 and 999textNO
C5Cell Value>999textNO

If I manually amend CH376 to 18 to replicate the anomaly then the year end total to beat is reduced to 520 as below.
test.xlsm
CH
375520
37618
Daily Tracking
Cell Formulas
RangeFormula
CH375CH375=SMALL(PreYTD,Counter)
Named Ranges
NameRefers ToCells
Counter='Daily Tracking'!$CH$376CH375
CurYTD=OFFSET('Daily Tracking'!$Y$375,0,YEAR(TODAY())-2005)CH375
MilesSince1981='Daily Tracking'!$CG$375CH375
PreYTD='Daily Tracking'!$B$375:INDEX('Daily Tracking'!$375:$375,1,DailyTrackingColumn-1)CH375
'Daily Tracking'!solver_opt='Daily Tracking'!$S$375CH375

But then Training Log cell F2 becomes negative - this is the anomaly and I don't know what's causing this.
test.xlsm
F
2-9
Training Log
Cell Formulas
RangeFormula
F2F2=('Daily Tracking'!CH375-C5)
Named Ranges
NameRefers ToCells
CurGoal='Daily Tracking'!$CH$375F2
VBA_YTD_MILES='Training Log'!$C$5F2
 
Last edited:
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
WOW... sorry I asked. I can't even come close to understanding all that.
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,330
Latest member
ThatGuyCap

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