Filling an Empty Cell with Previous Known Value in a column - excel

I have various empty cells in a table that I want to fill with the last known value in that column.
Sub autofiller()
Dim DataRange As Range
Set DataRange = Range("A1:D4")
Dim i As Integer
FillA = ""
For Each cell In DataRange
If cell(1, 1).Value <> "" Then
FillA = cell(1, 1).Value
Else
cell(1, 1).Value = FillA
End If
i = i + 1
Range("C1").Value = i
Next
End Sub
There will be no empty values in the first row. My logic is that it will look through every cell in that range, and if it is not an empty cell, pick up the value until it hits an empty cell where then I will place that value. The i counter is for me to keep track. It seems to me that the code is checking cell by cell horizontally but I want it to check cell by cell vertically. How can I make it check vertically?
I am new to VBA so any additional comments/guides will help.

Try this:
Sub autofiller()
Dim cell as range
Dim DataRange As Range
Set DataRange = Range("A1:D4")
For Each cell In DataRange
If cell.Value = "" Then
cell.Value = cell.Offset(-1).Value
End If
Next
End Sub
Because the for each will go left to right, top to bottom it will fill it with the cell above as it encounters empty cells.
You are confusing your variable cell which is a range variable with Cells() which is a range object. cell(1,1) is equal to cell as it is getting the first cell in a range of one.
Another way to write cell.Offset(-1) would be cell(-1,1) this gets the cell directly above the cell.

Related

Change formula to next cell down

I am trying to compare values from a vlookup to a static value in the column to the left, and then paste the value if it matches.
Column J contain static values
Column K contains a vlookup formula referencing another sheet
I need to loop through column K, and if the value from the vlookup matches the value in the same row in column J, then paste the value into column K.
So if cell J2 = 240.89, and the value of the vlookup in cell K2 = 240.89, then paste the value 240.89 into K2.
So far, the code I have loops through the range but stops at the last pasted value and ignores the vlookup formulas.
Here is the code I have:
Option Explicit
Sub CheckValue()
Dim myRange As Range
Dim cell As Range
Set myRange = Range("K2:K3194")
For Each cell In myRange
If cell.Value = cell.Offset(0, -1) Then
cell.Copy
cell.PasteSpecial xlPasteValues
End If
Next cell
End Sub
Any help is appreciated!
Just for fun another option:
Sub Test()
Dim ws as Worksheet: Set ws = Thisworkbook.Worksheets("Sheet1")
ws.Range("K2:K3194").Value = ws.Evaluate("IF(K2:K3194=J2:J3194,J2:J3194,FORMULATEXT(K2:K3194))")
End Sub
Note: This requires FORMULATEXT, a function available since Excel2013.
Try
For Each cell In myRange
If cell.Value2 = cell.Offset(0, -1).Value2 Then
cell.value = cell.value
End If
Next cell
or maybe use a WorksheetFunction.Round to match the values to a specific precision. I feel like this is more the issue than anything.

Apply loop to check if value is equal between ranges

I am trying to highlight a cell if it doesn't equal the value defined in a worksheet range.
I am checking each cell in the range "ADS_Export[ADS_208_SZ]" against Worksheets(ADS_Validator").Range("E3:E500") but it doesn't like the range E3:E500.
Seems to work if I just put E3 but all after the first cell check are incorrect as its checking against the third row, not the next row in the worksheet range (E4 etc.).
It needs to check the first value in the ADS_Export range against the first value in the worksheet range which starts at E3 and then does the next one. Also, need to redefine to highlight if not equal instead of equal.
Only just getting into VBA so my knowledge is very limited.
Sub IF_Loop()
Dim cell As Range
For Each cell In Range("ADS_Export[ADS_208_SZ]")
If cell.Value = Worksheets("ADS_Validator").Range("E:E").Value Then
cell.Interior.Color = 65535
End If
Next cell
End Sub
Try this code, please:
Sub IF_Loop()
Dim cell As Range, celVal As Range, lastRow As Long
lastRow = Worksheets("ADS_Validator").Range("E" & Rows.Count).End(XlUp).row
For Each cell In Range("ADS_Export[ADS_208_SZ]")
For Each cellval In Worksheets("ADS_Validator").Range("E3:E" & lastRow)
If cel.value = celVal.value Then
cell.Interior.Color = 65535
End If
Next
Next cell
End Sub

Referring to range that is the value of a cell

I have two columns, one contains addresses of other cells ($F$2 for example), the other contains colour index numbers (15, for example).
I want to colour the referred-to cell (which is on another sheet).
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p3907") 'column with cell values
Worksheets("World").Range(Range(Cell).Value).Interior.ColorIndex = Cell.Offset(0, 5).Value
End Sub
Because variable Cell in For Each will return string in range("p2:p3907") and when we refer to the range just use only Range("string refer to cell"). So, remove one range from your code and it will be as you need. See code below.
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p3907") 'column with cell values
Worksheets("World").Range(Cell).Interior.ColorIndex = Cell.Offset(0, 5).Value
Next
End Sub
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p10") 'column with cell values
Worksheets("World").Range(Cell.Value).Interior.ColorIndex = Cell.Offset(0, 5).Value
Next Cell
End Sub
Sub mySub()
Dim colorCodeRange As Range 'Cell is a keyword so you don't want to try to use it as a variable name.
Dim outputCell As String
Dim colorCode As Byte
Set colorCodeRange = Sheets(1).Range("P2:P10")
For Each Cell In colorCodeRange.Cells 'either define the range and use it here or don't define the range object and type in a hard-coded range here.
'you were defining the range then not using it, which is pointless.
outputCell = Cell.Value2 'get the output cell from the color code sheet
colorCode = Cell.Offset(0, 5).Value 'get the color code
Sheets("World").Range(outputCell).Interior.ColorIndex = colorCode 'output the color to the "World" sheet
Next Cell 'be sure to end the loop with "Next Cell" or at least "Next"
End Sub
This Sub will get output cells (F2, F3, etc.) from the range P2:P10. These values are in sheet 1. The color codes are in the same row, 5 columns over to the right (column U). The fill colors are output to the "World" sheet in the cell specified in the colorCodeRange variable ("P2:P10").

Move cell value on alternate rows to next row and delete empty row using VBA/Excel

Based on the images above I want to be a to select a range that looks like the first image dynamically and process it into the layout of the second image.
Below is some code i have tried to execute to do this dynamically.
Dim Myrange As Range
Dim Myrow As Range
Set Myrange = Selection
Dim strVal As String
For Each Myrow In Myrange.Rows
If Myrow.Row Mod 2 = 0 Then
strVal = Myrow.Columns(1).Cells
Myrow.Columns(1).Value = ""
Myrow.Offset(1, -1).Value = strVal
End If
Next Myrow
Undesired result
The last image is what i get when selection is processed.
Three cells instead of one are replaced with the values on previous row.
Assuming Participants is column A, put this formula in A1 and drag down.
=IF(ISNUMBER(LEFT(B1,1)), OFFSET(B1,-1,0),"")

My nested For-Each loop isn't going through each cell in a given range

In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With

Resources