G
goss
Hi all -
I need to loop through a range from the bottom up
The cells in the range contain either a zero or a 5 digit number
If the value in the cell is greater than myValue, then assign the
value in the cell to myValue
Else, overwrite the value in the cell with myValue
It appears my macro has 2 errors
1.) It does not appear as though it is working backwards through the
cells, the address is always $A$271
2.)It is assigning the address as the value rather that the value of
the contents of the cell
Can you suggest how I may improve the macro?
Thanks
Best regards,
-markc
code:
Option Explicit
Sub hrs_FillUnitNmbr()
Dim wbBook As Workbook
Dim wsData As Worksheet
Dim myValue As Long
Dim myRange As Range
Dim lngRows As Long
'Setup Environment
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Set wbBook = ThisWorkbook
With wbBook
Set wsData = .Worksheets("Data")
End With
lngRows = wsData.Range("A65536").End(xlUp).Row
With wsData
Set myRange = .Range("A" & lngRows)
End With
'Initialize
myValue = 0
'Fill
myValue = myRange.Value
Do While lngRows > 1
Debug.Print myRange.Address
myRange = myRange.Address(lngRows, 1)
If myRange.Value > myValue Then
myValue = myRange.Value
Else
myRange.Value = myValue
End If
lngRows = lngRows - 1
Loop
'Set Vaules - Clear Formulas
myRange.Copy
myRange.PasteSpecial xlPasteValues
'Calculate
Worksheets(1).Calculate
'Reset / Cleanup
Set wbBook = Nothing
Set wsData = Nothing
Set myRange = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
I need to loop through a range from the bottom up
The cells in the range contain either a zero or a 5 digit number
If the value in the cell is greater than myValue, then assign the
value in the cell to myValue
Else, overwrite the value in the cell with myValue
It appears my macro has 2 errors
1.) It does not appear as though it is working backwards through the
cells, the address is always $A$271
2.)It is assigning the address as the value rather that the value of
the contents of the cell
Can you suggest how I may improve the macro?
Thanks
Best regards,
-markc
code:
Option Explicit
Sub hrs_FillUnitNmbr()
Dim wbBook As Workbook
Dim wsData As Worksheet
Dim myValue As Long
Dim myRange As Range
Dim lngRows As Long
'Setup Environment
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Set wbBook = ThisWorkbook
With wbBook
Set wsData = .Worksheets("Data")
End With
lngRows = wsData.Range("A65536").End(xlUp).Row
With wsData
Set myRange = .Range("A" & lngRows)
End With
'Initialize
myValue = 0
'Fill
myValue = myRange.Value
Do While lngRows > 1
Debug.Print myRange.Address
myRange = myRange.Address(lngRows, 1)
If myRange.Value > myValue Then
myValue = myRange.Value
Else
myRange.Value = myValue
End If
lngRows = lngRows - 1
Loop
'Set Vaules - Clear Formulas
myRange.Copy
myRange.PasteSpecial xlPasteValues
'Calculate
Worksheets(1).Calculate
'Reset / Cleanup
Set wbBook = Nothing
Set wsData = Nothing
Set myRange = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub