I am trying to figure out how to autofill a specific range of cells based on already defined data in the same row. My task is more complex, but a simplification of the step can be seen in the code below. What I want to achieve is:
Define a range where I want to output my values
Multiply two values in the same row of the selected range cell (to its left), and output this number in the currently selected range cell. To do this, one of the numbers to be multiplied will be dependant on a string also in the row (which describes its type).
Loop through the defined range and repeat the calculation on each row.
My current code is as follows and outputs a "application defined or object defined error".
Any help would be much appreciated.
For a = Range("P12") To Range("P33") 'Range of cells I want to fill.
If Cells(a, -10).Value = "B" 'If the cell 10 to the left of our "a" value is = "B".
Then c = Cells(a, -10).Value * Worksheets("LCI").Range("D4").Value 'Then new variable "c" = (cell 9 to left of a) * (number from another sheet containing a database)
Cells(a).Value = c 'Update value of selected range cell to contain c which we calculated.
Next 'Repeat for all rows in range.
You are close.
You need to think of a as a cell or Range object. That variable is the cell itself which has properties like a.row and a.column that describe its location on the sheet in which it lives.
When you say Cells(a, -10) you are saying "On the Activesheet I want a Cell where the row is a, and where the column has the number -10". There is no row a (as a is a range/cell object and because you didn't specify which property of a you are wanting here it will default to a.value which is probably nothing) and there is no column -10 on the Activesheet.
Your loop is defined incorrectly. You can use a For Each loop to loop through cells in a range.
Instead:
For Each a In Range("P12:P33")
'clear `c` each loop
c=0
If a.Offset(,-10).Value = "B" Then 'start at cell `a` and move back 10 columns and grab the value
c = a.Offset(, -10).Value * Worksheets("LCI").Range("D4").Value
End If
a.Value = c
Next a
Related
I am a newbie. I want to sum the squares of the numbers in the active column. I am getting an error 'object doesn't support this method'. Here is my code
Sub sum_squares()
Dim total As Integer
Dim c As Range
Dim d As Range
'set d equal to column from active cell down to last non-empty'
d = Range(ActiveCell, ActiveCell.endxldown)
total = 0
For Each c In d
total = total + c.Value ^ 2
Next c
End Sub
Appreciate the help.
Thanks
As has been pointed out you've got the syntax of xlDown incorrect.
You should also start at the bottom and move up - xlDown may not find the last cell.
E.g.
With a value in cell A1:A3 and A1 as the ActiveCell it will correctly return A3.
Same scenario but with A4 left blank and a value in A5 still returns A3.
Same scenario with A1 left blank it returns A2.
This will return a reference from the ActiveCell to the last cell containing a value in that column.
Note that if the ActiveCell is lower down than the last cell containing data you'll get a reference reflecting that.
Set d = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
A Range can be made up of one or more cell references:
Range("A1") and Range("A1,A3,C5") reference individual cells.
Range("A1:C5") and Range("A1", "C5") reference all cells between the first and last address.
A Cell is a single cell reference that uses row and columns identifiers.
Cells("A1") will return an error as it's a full address.
Cells(1,"A") will return A1 (row 1, column A)
Cells(1,1) will also return A1 (row 1, column 1)
The code above is using two cell addresses to reference all cells between the two.
Range(ActiveCell,....) is the reference to the first cell.
Cells(Rows.Count, ActiveCell.Column) is the reference to the second cell using row numbers and column numbers.
If the ActiveCell is in column B then this is the same as writing Cells(1048573,2).
The End(xlUp) then goes from that cell back up to the first one containing data.
There is a syntax error in your code - .endxldown and add Set before assigning range.
Correct it to -
Set d = Range(ActiveCell, ActiveCell.End(xlDown)
Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell
I have some VBA code that will delete the entire row if a cell in a column has red text
Dim Cell As Range
For Each Cell In Intersect(Columns("L"), ActiveSheet.UsedRange)
If Cell.DisplayFormat.Font.ColorIndex = 3 Then Cell.Value = "#N/A"
Next
On Error GoTo NoRedText
Columns("L").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoRedText:
I would like to extend this code to also include another column that includes a "Y" value in the cell.
Column L includes the red text string
Column P includes the "Y" text string
Therefore if the text is red in column L AND the text is equal to "Y" in column P it should delete the entire row
What do I need to add to the code to achieve this?
Thank you
Your Cell variable references a range and all the properties that go with it.
The Offset property
Returns a Range object that represents a range that's offset from the
specified range.
(https://msdn.microsoft.com/en-us/library/office/ff840060.aspx)
Using this knowledge you can tell your code to look at the range that is offset by four columns:
If Cell.DisplayFormat.Font.ColorIndex = 3 AND Cell.Offset(,4)="Y" Then Cell.Value = "#N/A"
I need some help here if someone can provide me a code for this.
Scenario:
cell A is a drop down list
column B has a static value
cell C is where they input the value
cell D is where the value of cell c stored
so if cell A = value in column B, the value of cell C should be copied and saved to cell D, because cell A is a dropdown list, if I change the cell A to another value, cell c should reset to "blank", but the value of cell D should remain to where cell A = cell B.
I have this code inputed in cell D, but when I change the value of cell A, the value of cell D also disappear which it should remain where cell a = cell b
=vlookup(A2,Sheet1!B2:C5,2,FALSE)
I'm new to excel vba so any help will be much appreciated.
thanks,
Overly simplified, but here's a solution ...
First, here is a screenshot of how I set up the data ...
The data in column A uses a drop down list. Values that may or may not be the same are entered into column B. Column C are user entries and Column D gets those upon recalculation.
Here is the code behind the Recalculate button ...
Option Explicit
Option Base 1
Public Sub Recalculate()
Dim iLoop As Integer
For iLoop = 2 To 4
If Sheets("Sheet1").Cells(iLoop, 1) = Sheets("Sheet1").Cells(iLoop, 2) Then
Sheets("Sheet1").Cells(iLoop, 4) = Sheets("Sheet1").Cells(iLoop, 3)
Else
Sheets("Sheet1").Cells(iLoop, 3) = ""
End If
Next iLoop
End Sub
I didn't do anything to handle adding rows, etc. Just blindly calculating as you requested.
I changed the value in A3 and here is a screen shot of the result.
I want to use an If statement (VBA code) to check the cell range in a column for a given numeric parameter. For the cell that matches the given value, the cells at the right (in the same row) should change the background color.
Pseudocode Example:
A1=5,7
If cell in Range(F1:F10) has value=A1 Then
(random matched cell: F7=5,7)
Range (G7:M7) = Background Blue
The part to change the background I know how to do it, but what is the best way to check the given range?
I think you want something like
for i = 1 to 10 'rows in column f to loop through
if cells(i,6) = cells(1,1) then 'column a is 1, column f is 6, etc.
range(cells(i,7), cells(i,13)).interior.colorindex = 'number for that color
end if
next i
I'm guessing you may have multiple rows in F1:F10 that have a match on A1. I would iterate through the cells in the range with:
For each rngCell in Range("F1:F10")
If rngCell.value = Range("A1").value
Range("G" & rngCell.row, "M" & rngCell.row).Interior.ColorIndex = 5
End If
Next