Autofill when there are blank values - vba

Needing help with the following.
I want for the first column: To auto fill the remaining blank spaces until another value is found. Example: RMDSADMN would be autofilled until TXAADGLI is found, then this would be autofilled until TXAADM, then this would get filled one time since there is one blank space.
I tried adding input boxes where I had to manually insert the name of each value but I am aiming for something that automatically checks the values, instead of me inserting them.

Try,
with activesheet
with .cells(1,1).currentregion
.specialcells(xlcelltypeblanks).formular1c1 = "=r[-1]c"
.value = value
end with
end with

Try this:
Sub test()
Dim lRow As Integer
Dim i As Integer
lRow = Cells(Rows.Count, 5).End(xlUp).Row
With ThisWorkbook.ActiveSheet
For i = 1 To lRow
If .Cells(i, 1).Value = "" Then
.Cells(i, 1).Value = .Cells(i - 1, 1).Value
End If
Next i
End With
End Sub

Can be achieved easily without VBA:
Enter something in first row after last blank (same column), select from RMDSADMIN down to that something, =, Up, Ctrl + Enter.

The following code can help is there you need to auto-fill the previous values between 1st and last cells depending on value of 1st cell as mentioned in question Excel - VBA fill in cells between 1st and Last value
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long
For i = 2 To Target.Column
If Cells(Target.Row, i) = "" Then
If Cells(Target.Row, i - 1) <> "" Then
Range(Cells(Target.Row, i), Cells(Target.Row, i)).Value = Range(Cells(Target.Row, i - 1), Cells(Target.Row, i - 1)).Value
End If
End If
Next i
End Sub
This sub is activated by clicking on any cell.
Same cell marks the end of the loop i.e. to stop the loop just click the cell till which you want to fill the blank cells.
Update: this can be similarly done for other way round as well as asked in this question.

Related

VBA Highlight row if cell value is numeric

I am trying to highlight rows in Excel if they have a numeric values in this column (others are blank), however this code highlights all of them:
For lRow = LastRow To FirstRow Step -1
With .Cells(lRow, "AF")
If IsNumeric(.Value) Then
.EntireRow.Interior.Color = 5296274
End If
End With
Next lRow
Any advice or assistance will be appreciated.
The safest way to check for Numeric values in a cell is also check that the cell is not empty, or contains only spaces, for that you can use Trim(.Value2) <> "".
Code
If IsNumeric(.Value) And Trim(.Value2) <> "" Then
.EntireRow.Interior.Color = 5296274
End If
Edit 1: Faster run-time code.
Using VBA, the tasks which consume the longest are the ones accessing the worksheet, in this case it's .EntireRow.Interior.Color = 5296274.
What we can do, is use a Range object, in my code it's ColorRng, and every time the If criteria is met, we add that cell to this Range, using the Union function. At the end of the code, we just change the color of the EntireRow in ColorRng, which will result coloring the entire rows which are not numeric at one shot.
Modified Code
Option Explicit
Sub ColorEmptyRows()
Dim ColorRng As Range
For lRow = LastRow To FirstRow Step -1
With .Cells(lRow, "AF")
If IsNumeric(.Value) And Trim(.Value2) <> "" Then
If Not ColorRng Is Nothing Then
Set ColorRng = Application.Union(ColorRng, .Cells(lRow, "AF"))
Else
Set ColorRng = .Cells(lRow, "AF")
End If
End If
End With
Next lRow
' if the range has at least 1 cell, color the entire range at the same time
If Not ColorRng Is Nothing Then ColorRng.EntireRow.Interior.Color = 5296274
End Sub
Empty cells are also considered as numeric with that function. You can use If Isnumeric(.Value) and .Value <> "" Then for instance.

VBA Excel search column for last changing value

I've got a column: U. This column has values from U10 till U500.
What I need to get is the last changing value of the column and if it doesn't change then a text "False" or something and if the last changing value is an empty cell, then ignore that..
Column U
11
11
5
11
11
21
For example here the result should be 21.
I've tried comparing two rows and with conditional formatting but with such a big range doing all this for each row is a bit too much.
Does anybody know a good way to do this?
Something like that should do it ...
Sub test()
Dim LastRow As Long, i As Long
With Worksheets("Sheet1") 'your sheet name
LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row 'find last used row in column U
For i = LastRow To 2 Step -1 'loop from last row to row 2 backwards (row 1 can not be compared with row before)
If .Cells(i, "U").Value <> .Cells(i - 1, "U").Value Then 'compare row i with row before. If it changes then ...
MsgBox "Last row is: " & .Cells(i, "U").Address & vbCrLf & _
"Value is: " & .Cells(i, "U").Value
Exit For 'stop if last changing row is found
End If
Next i
End With
End Sub
It loops from last used row in column U to the first row and checks if the current row is different from the row before. If so it stops.
i am not sure of the how you want the output.
IF(AND(RC[-1]<>R[-1]C[-1],ROW(RC[-1])>500,R[-1]C[-1]<>""),RC[-1],"")
try this formula in cells V10:V500
Try this Macro.
First run the AnalyseBefore sub and when you want to check if the value has changed run the AfterAnalyse sub.
Incase you want the range to be dynamic use the code that I have commented and include iCount in your Range calculation
Sub AnalyseBefore()
Dim iCount
Range("U10").Select
iOvalue = Range("U500").Value
'iCount = Selection.Rows.Count
Range("Z1").Value = iOvalue
End Sub
Sub AnalyseAfter()
Dim iCount
Range("U10").Select
iNValue = Range("U500").Value
Range("Z2").Value = iNValue
iOvalue = Range("Z1").Value
If (iOvalue = iNValue) Then
Range("U500").Value = "FALSE"
End If
End Sub

Insert row base on specific text and its occurrence

I am using a VBA code to insert rows below based on a specific text and its occurrence .
I am using the following code to do so
Sub try()
Dim c As Range
For Each c In Range("A1:A100")
If c.Value Like "*COLLECTION*" Then
c.Offset(1, 0).EntireRow.Insert
End If
Next c
End Sub
I want to have the text BALANCE below the COLLECTION cell instead of blank row.
I want to insert the BALANCE row below the last COLLECTION entry, for example if there are two collections rows serially then I want to add the BALANCE row after the 2nd collection row. but with the above VBA code I am getting blank rows below to the each collection row.
My Collection and balance rows are in the column A
Before macro Image kindly check
After macro I want like this Image kindly check
I would do this using a loop from row 1 till last filled row in column A. Then having a boolean marker which is true while the cell value in current cell is like "*COLLECTION*" but false while not. So if the current cell is not like "*COLLECTION*" but the marker is true then the last cell above the current cell was like "*COLLECTION*". Then insert a new row with "BALANCE" if that cell is not already "BALANCE".
Sub try()
Dim c As Range
Dim lRow As Long
lRow = 1
Dim lRowLast As Long
Dim bFound As Boolean
With ActiveSheet
lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
Do
Set c = .Range("A" & lRow)
If c.Value Like "*COLLECTION*" Then
bFound = True
ElseIf bFound Then
bFound = False
If c.Value <> "BALANCE" Then
c.EntireRow.Insert
lRowLast = lRowLast + 1
c.Offset(-1, 0).Value = "BALANCE"
c.Offset(-1, 0).Font.Color = RGB(0, 0, 0)
End If
End If
lRow = lRow + 1
Loop While lRow <= lRowLast + 1
End With
End Sub
That's typically the kind of cases you want to start from the last cell, because inserting a row will mess up all counters from what is below.
In other words, the elegant for each is not really a good idea. Too unpredictable. An ugly, old simple For Step -1 is the way to go. Something like :
Sub Macro1()
For l = 100 To 1 Step -1
If Trim(Cells(l, 1)) = "COLLECTION" And Trim(Cells(l + 1, 1)) = "DEMAND" Then
Rows(CStr(l + 1) & ":" & CStr(l + 1)).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(l + 1, 1) = "BALANCE"
End If
Next l
End Sub
Just tried on EXCEL 2013, seems to work as you want. There may be more elegant solutions, though.
EDIT : the idea is the following one :
_Begin by the last line(in fact, the last line cannot work, so one optimization could be to start from the prevo=ious one), and go to the first one
_If the line testes is "COLLECTION", and the next one is "DEMAND", then you need to insert a "BALANCE" line in between. It's done in 2 times, first insert an empty line, then add "BALANCE" in the newly created line.

VBA runtime error 9

I am currently getting a error saying subscript out of range. on the line of code
If Sheets(Master).Cells(i, A).Value = AssetNum.Value Then
I'm trying to use a for loop to increment i so the row range starts at 12 and adds 1 to it each time. Then inside the for loop I want to use an If statement to check and see if the cell (i,A) is equal to the value in AssetNum. If the loop reaches the value of the EmptyRow it ends the loop. Im not exactly sure how to use a for loop IF-THen statement correctly.
Public i As Integer
Private Sub AssetNum_Change()
End Sub
Private Sub Enter_Click()
Dim EmptyRow As Long
'Audit will only search Master Sheet
Worksheets("Master").Activate
'Find empty row value so we can use that for limit of search
With Sheets("Master")
EmptyRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
End With
'i needs to be set to minimum limit
'Begin loop of search
For i = 12 To EmptyRow + 1
If Cells(i, 1).Value = AssetNum.Value Then
'Go to compare userform to display
Compare.AssetDisplay.Value = AssetNum.Value
Compare.LocationDisply.Value = Cells(i, 2).Value
Compare.Show
End If
Next i
'If i gets to emptyrow num then go to non found asset userform
Unload Me
NonFoundAsset.Show
I assume your refer with your error to the line:
If Cells(i, A).Value = AssetNum.Value Then
Well, I see nowhere that A is declared. Then VBA declares it automatically (advice: always turn Tools, Options, Require variable declarations to ON). Neither do I see A being initialized, so its value will be 0 and that is not a valid reference for Cells. Hence, Subscript out of bounds.
If you want to reference the "A" column, then write:
If Cells(i, 1).Value = AssetNum.Value Then
as "A" is the first column.
Use as follow:
If Sheets(Master).Cells(i, "A").Value = AssetNum.Value Then
And also this line:
Compare.LocationDisply.Value = Cells(i, "B").Value

vba how to check if a cell has no formatting or color

I recently had a question regarding how to copy a cell value into all cells below it and stop based on when column A got to a blank cell. post
The excel sheet I'm working in has many divider rows that are color filled across the entire row to separate categories as you scroll down the sheet. I would like to be able to skip these separating rows in column A when the macro checks for a blank cell in column A. Or I would like to just assign StopRow to the first cell which has no formatting/no color/no value.
Here is what I have, thanks to Ripster earlier today, but I've failed incorporating a proper if then statement with what he came up with.
Sub Example()
Dim MasterValue As String
Dim StopRow As Long
Dim i As Long
'Get the master value
MasterValue = Range("C5").Value
'Get the first blank cell in column A
StopRow = Range("A1").End(xlDown).Row
'Start at row 6 and continue to the "Stop Row"
For i = 6 To StopRow
'Set every cell from row 6 in column 3 to the "Master Value"
Cells(i, 3).Value = MasterValue
Next
End Sub
Please help.
Thanks
This took me a while but I found solution to your problem ;)
If macro goes to cell with different color - checking and do nothin, next "i" is taken. This should do what u want. It's possible to add more color ;)
Link to colors - http://dmcritchie.mvps.org/excel/colors.htm
Sub Example()
For i = 6 To 1200
If Cells(i, 3).Interior.ColorIndex = 1 Then 'here is color black
Else
If IsEmpty(Cells(i, 1)) = True Then 'if cells in column "A" is empty then stop
Exit For
Else
Cells(i, 3).Value = Range("C5").Value
End If
End If
Next i
End Sub
Your conditions for StopRow aren't clear. Do you want to set StopRow when the cell has a conditional formatting rule or simply when it has a different format than the default ? A cell may have a rule but it may not be applied. Anyway, the function presented here is something you might find of use.
Copy the ActiveCondition function somewhere in a module and then change your for loop like so:
For i = 6 To StopRow
If ActiveCondition(Cells(i,3))=0 Then StopRow=i : Exit For
Cells(i, 3).Value = MasterValue
Next
If you want to check for change in font color that didn't come from conditional formatting then you'd need an extra line:
For i = 6 To StopRow
If ActiveCondition(Cells(i,3))=0 Then StopRow=i : Exit For
If Cells(1,3).Font.ColorIndex=0 Then StopRow=i : Exit For
Cells(i, 3).Value = MasterValue
Next
There are more elegant ways to do this but this solution is the easiest to implement in your code.
Hope this helps.