Highlight specific rows in table according to condition - vba

I am trying make this macro works. It supposed to highlight rows in a table according to the condition. When I try to offset the selected range, so only the cells within the table get highlighted. But when I do so, my condition doesnt work as it should be. I am completly newbie to this, many thanks for any advice.
Sub výklep()
'Find the last non-blank cell in column
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Set SelectedRange = Range("I6:I" & LastRow)
For Each cell In SelectedRange
If cell.Value > 10 Then cell.Range(Cells(1, 1), Cells(1, 9)).Offset(1, -7).Interior.ColorIndex = 3
Next
End Sub

You must be careful while offsetting with minus sign (-). In your case it is minus seven (-7). While looping it is crossing the left border of your sheet thus causing error.

Related

Saving number as text, how to automate it

I have a 7702216772 number inside a cell. If I put a ' before the fist digit and click Enter Excel transforms the number to a text and puts a green triangle at the left top of the cell:
I have many rows of similar numbers all of which need to be transformed into text. However clicking each and adding ' before the first symbol and clicking Enter would take a lot of time. Is there any way to do it programatically?
I tried using formula: ="'"&H4 but it doesn't do what's expected - the green triangle never appears on the result cell.
I also tried setting cell format to Text, but the green triangle doesn't appear in that case too.
I need the green triangle to appear at the upper left corner, just like at the picture!
If all your number are in a single column, the following code will do it:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRow 'loop from row 1 to last
ws.Cells(i, "A").Value = "'" & ws.Cells(i, "A").Value 'add the ' before the number
Next i
End Sub
Change the "A" to whichever column you are using.
Just Select the cells you wish to process and run this short macro:
Sub Textify()
Dim rng As Range, r As Range
Set rng = Selection.Cells.SpecialCells(2, 1)
For Each r In rng
r.Value = "'" & r.Value
Next r
End Sub
Non VBA answer; I'm using Column G in this answer but it depends on where your numbers are. You'll have to change the cell but I think you will be ok with this.
In an empty cell, enter formula: ="'"&G4
Use the fill handle or Ctrl+D to fill it down to the length of Column G's values.
Select the whole of Column G's values and copy them to the clipboard
Select the same range in Column G, right-click, select Paste Special and choose Values
I have tested it now for several times and it worked always
Cells(xx, xx).FormulaR1C1 = "'" & Cells(xx, xx).Value
Same would work for ActiveCell or whatever you like.

Simplifying complex excel formula with VBA

I have a macro that is generally slow due to overuse of LOOKUP formulas. I want to insert some VBA variables to speed these up. I am currently working on speeding up the formula below:in Excel:
=IF(ISNA(MATCH(A2,Summary!B:B,0)),"n",I2-((I2/LOOKUP(2,1/(I:I<>""),I:I))*VLOOKUP(A2,Summary!$G$10:$H$902,2,FALSE)))
in VBA:
"=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-((RC[-1]/LOOKUP(2,1/(C[-1]<>""""),C[-1]))*VLOOKUP(RC[-9],Summary!R10C7:R902C8,2,FALSE)))"
The portion I need to replace is LOOKUP(2,1/(C[-1]<>""""),C[-1]). All this does is reference the last non empty cell in column I. Right now I have the following code to return the address of the last cell in VBA
Sub FormulaTest()
Set lRow = Range("I1").SpecialCells(xlCellTypeLastCell).Address
End Sub
I am trying to figure out how to implement this "lRow" into the VBA code for the formula. Can anyone steer me in the right direction?
**EDIT 1
Please see Fernando's comment below. He has the right idea however the solution is still off a bit. Ill try to explain it better in a few comments: First off, The first row is always a title row, the last row is always a sum row, the current tab is the "Sales" tab, and the amount of rows in any given Sales tab will vary (could be I1:I59, could be I:1:I323).
In this example I1 is a row title and I59 is the sum of I2:I58. Rows I2:I58 are dollar amounts. My macro places this formula in J2:J58. This formula takes each row's dollar amount (I2:I58) as a percentage of the total (I59) and multiplies it by an input amount on the Summary tab (the VLOOKUP). This amount is then subtracted proportionately from the dollar value in column I with the J cell showing the result.
I am looking to eliminate the need for the LOOKUP function (selects last non empty cell) within my formula above: LOOKUP(2,1/(C[-1]<>""""),C[-1]).
**EDIT 2
Fernando's solution worked. Thank you all for your input
This would return the last non-empty row in column I
with Worksheets("Summary")
lRow = .Cells(.Rows.Count, "I").End(xlUp).Row
end with
So your code would be
sub testy
dim lRow as long
with Worksheets("Summary")
lRow = .Cells(.Rows.Count, "I").End(xlUp).Row
end with
"=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-_
((RC[-1]/R"&lRow&"C[-1])*VLOOKUP(RC[-9],Summary!R10C7:R902C8,2,FALSE)))"
In your solution you're using xlCellTypeLastCell. This is very useful, but it calculates based on UsedRange, which may not be what you want. with this, if you have data up to row n and then you update the data and now you have less records, the last row with xlCellTypeLastCell will still be n, so be careful with that.
Assuming that you are doing all your work on the active sheet, looking up to a "Summary" sheet:
Sub fillCol()
Dim aRow As Long, bRow As Long
aRow = Cells(Rows.Count, "I").End(xlUp).Row
bRow = Sheets("Summary").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J" & aRow).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-" _
& "((RC[-1]/" & aRow & ")*VLOOKUP(RC[-9],Summary!R10C7:R" & bRow & "C8,2,FALSE)))"
End Sub
You made need to change the columns which contain the contiguous range (in order to determine the last row)

IF THEN VBA MACRO - Update one column if contents of another = 100%

I have a workbook with "Results" being sheet 3, this being the worksheet I want to use.
I have tried a few formulaes to try and add a macro to do the following:
I have column G with percentages. I then have column I where I would like there to be a result saying TRUE/FALSE where the contents of G are equal to 100%. Column G is formatted to percentage with two decimals.
Some considerations: I have my first row being a Hyperlink to another sheet, then my headings, then the first row of "results". I have 457 rows, if there is a measurement of the range, perhaps it could be on A?
I keep getting this error 9 with my range and have got a bit stuck.
Thanks in advance!
Sub PartialHits1()
Dim rng As Range
Dim lastRow As Long
Dim cell As Range
With Sheet3
lastRow = .Range("G" & .Rows.Count).End(xlUp).Row
Set rng = .Range("G1:G" & lastRow)
For Each cell In rng
If cell.Value = 100
Then
cell.Range("I1:I1").Value = 100
End If
Next
End With
End Sub
(I have hacked this a bit, just was trying to get it to set as 100 instead of the TRUE/FALSE Also was playing around near the Sheet 3 part as I got errors.)
RangeVariable.Range can refer only to a cell within RangeVariable, so you can't refer to column I in this way. Try: .Range("I"&cell.row)=100.
Also your criteria is probably wrong, if you have 100% in a cell it's actual value is 1.
And last question: why do you want to do this with VBA, it would be much more simple with worksheet function =IF(G3=1,100,"")

Changing values in a specific cell based on other cell/row inputs

Hi I'm fairly new to formulas and excel but this is one of the problems I have encountered.
I have been using 2 conditional formatting formulas as follows
=INDIRECT("I"&ROW())="Del"
=INDIRECT("I"&ROW())="Sum"
Where the first formula simply highlights the row grey if del is in the I column of that row, is there also a way of making it change say the K column to 0 if column H in that row is 0?
And for the second formula which also highlights the row another color based on sum input in the given column of that row, is it also possible to change K column of that row to match the value of H column of the given row.
I know they would be similar but I needed to make it so formula one would only zero the K column in the given row if I column had "del" and H column of the row had Zero.
And for the second formula the values would only change in column k of the given row if "sum" was in the I column. Anything else needs to stay unformatted unless these changes are implemented.
I am unable to add a formula the the cells in question as these are overwritten with an button clicked event which inputs data into this field.
any information is appreciated, formula or VBA.
a) use =$I2="Del" instead of INDIRECT (where 2 is the first row of the range your conditional format applies to, e.g. =$A$2:$Z$9999, or the row of the firstly selected cell of the range when you are inserting the conditional format)
b) if you can use a new column that won't be overwritten, the formula in this new column can be:
=if(and(I2="Del";H2=0);0;if(I2="Sum";H2;K2))
P.S.: use , instead of ; if your Windows > Control Panel > Region and Language > Additional settings... > List separator is set to a comma
Just adjust the Offset accordingly to change the column(s) you want.
Dim firstCell As Range
Dim FoundCell As Range
Dim lastrow As Long
With ActiveSheet
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
With Range("H2:H" & lastrow)
Set FoundCell = .Find(What:="3")
Set firstCell = FoundCell
Do Until FoundCell Is Nothing
'.offset(0,-1) would be the same row in Column "G"
FoundCell.Offset(0, -1).Value = 0
'if you wanted to assign the same value then do this:
' FoundCell.Offset(0, -1).Value = FoundCell.Value
Set FoundCell = .FindNext(FoundCell)
If FoundCell.Address = firstCell.Address Then
Exit Do
End If
Loop
End With
End With

VBA Excel Line Break in Cell

I've been searching for a solution to this problem for over a week now. I have a sheet with formatted text (colors and styling) with values an undetermined distance down the first column. There are some spaces in between but I believe I've handled that situation correctly by using IsEmpty. I want to copy each of these values from each cell all to one cell at the top of the second column. I've been successful in being able to copy the text from each of these cells into a specified concatenated cell with line breaks, however I've been unsuccessful at keeping the formatting. Any help anyone can provide on how to copy the formatting along with this text would be greatly appreciated. Thank you!
'set variable to find the last row of the sheet
Dim LastRow As Integer
'find the last row of the active sheet
LastRow = ActiveSheet.UsedRange.Rows.Count
'loop through each of the rows
For C = 1 To LastRow
'determine if a cell has a value in it - if so complete below commands
If (IsEmpty(Cells(C, 1).Value) = False) Then
'leave the contents of the first cell in the second column, insert a linebreak and copy the values from the current cell in the first column
Cells(1, 2).Value = Cells(1, 2).Value & Chr(10) & Cells(C, 1).Value
End If
Next C
You can make use of Range.Copy and Range.PasteSpecial properties that is easy to use and solved your problem of formatting cell .