Saving number as text, how to automate it - vba

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.

Related

Highlight specific rows in table according to condition

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.

copy conditional formatting format but not rules/numbers

need some help thinking through how to do this.
ultimately, what i want to achieve is to sum together cells based on if another corresponding cell is 0 or 1. it's a bit convoluted but i'll try my best to explain.
sheet 1 has data that shows, by month, if actuals have come in for the month. a column will display a 0 for accounts/month that need to be added.
sheet 2 has two tables. table 1 pulls in the 0 & 1 from sheet 1 and uses conditional formatting to highlight cells that need to be added. the only thing in this table is 0 & 1. table 2 is the exact same setup as table 1, just with the actual numbers.
my original thought was to just copy the highlighting format from table 1 onto table 2 then use a macro to sum highlighted cells. obviously, i have found that that is not possible.
i tried looking around and haven't found anything that lets me just copy the highlighting format without overwriting the numbers in table 2.
is this possible?
Try this. I'm not sure how your data is laid out, but if it's in a row, you can run this on a new cell at the end of that row (meaning, the next empty column):
Function sumHighlighted(ByVal myRng As Range)
Dim cel As Range, iRow As Range
Dim fSum As Long, totalRows As Long
fSum = 0
For Each cel In Range(Cells(myRng.Row, 1), Cells(myRng.Row, myRng.Column))
If cel.Interior.ColorIndex <> -4142 Then
fSum = fSum + cel.Value
End If
Next cel
Debug.Print "The total in this row is: " & fSum
sumHighlighted = fSum
End Function
Steps:
1. Copy the Cell with conditional Format.
2. Paste to cell where you want (Hold ALT + press E + S + T).
To generate a code, click "Record Macro" first and do the 2 steps above and stop the recording macro once you're done.

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,"")

Repeat command and increase range

I have almost no experience with VBA that being said.
I have a function in "B21" that I have copied over the same row up to "Z21"
I want the results from these cells to be shown on a different sheet all in one Column starting at "B2" and repeating until till it reaches the end.
This is what I have for that.
Sheets("Barlist").Range("B2").Value = "B21"
Alternatively the function I have in "B21" that I have copied over to repeat itself is:
=IF((COLUMN()-1)<$B$16,ROUND(TAN(RADIANS($B$5))*(($B$6)-($B$15*(COLUMN()-2))),2)+($B$11-0.33),IF((COLUMN()-1)=$B$16,$B$18,""))
So if I could just tell in to repeat this function down Column "B" until it returns a blank result that would should work to.
This code would cycle through all cells from B21 to the last column in row 21 with data and if the cell is blank you would need to add the code for what you wanted to do with that cell. Otherwise, the cells gets placed into "Barlist" starting at B2 and working down (using the next blank row each time)
Sub NAMEOFSUB()
Dim LastCol As Long
Dim CurCol As Long
Dim DestRow As Long
LastCol = Sheets("CURRENT SHEET NAME").Cells(21, Columns.Count).End(xlToLeft).Column
For CurCol = 2 to LastCol
If Sheets("CURRENT SHEET NAME").Cells(21, CurCol).Value = "" Then
'Do Something with your blank cell such as add a .Formula
Else
DestRow = Sheets("Barlist").Range("B" & Rows.Count).End(xlUp).Row + 1
Sheets("Barlist").Range("B" & DestRow).Value = Sheets("CURRENT SHEET NAME").Cells(2, CurCol).Value
End If
Next CurCol
End Sub
What exactly are you trying to do?
Right now your code would just enter the value "B21" in cell B2 on sheet Barlist.
If you want to add a reference to that cell, you could use Sheets("Barlist").Range("B2").FormulaR1C1="R21C2"
Although I really don't see any reason to use VBA at all for this. If you want a dynamic range, so it is linked to the original range, you could simply use the Transpose Function.
For your example, select the cells B2 to B26 on your Barlist sheet. Press F2 (should enter cell B2) and enter the formula =TRANSPOSE(SHEET!B21:Z21). This is an array function, so you don't just press enter, you have to press CTRL+SHIFT+ENTER at the same time to enter it. Excel will automatically append little curly brackets {} around the formula so you know it's an array formula.
If you want static values, select the range B21:Z21 you want to copy, then go to cell B2 on your Barlist sheet and right click>paste special and use the "trasnpsose" option

Excel VBA conditional formatting based on next cell value

I am trying to do some conditional formatting in Excel for the font size but seeing as it can't be done from the format menu, it needs to be done with VBA.
I have a range B6 to however many rows and I want to look at the cell next to it and see if it's blank (column C). If it is then format the cell to Bold and 11pt. If it's not blank then it needs to be normal and 9pt.
My code at the minute only makes the last row Bold and 11pt and the rest of the column, even if column C is empty will be normal 9pt.
What is going wrong? BTW I'm using Excel 2003
Dim c As Range, rng
Dim LASTROW As Long
LASTROW = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("B6:B" & LASTROW)
For Each c In rng
If Len(c.Offset(1, 0)) = 0 Then
c.Font.Bold = True
c.Font.Size = 11
Else
c.Font.Bold = False
c.Font.Size = 9
End If
Next c
Your Offset parameters are backwards. You are checking the cell below the current one.
Note the trick is to use a single rule, coded for the top-left cell
This doesn't need a macro - you can do it using a formula in Conditional Formatting.
Say you wanted to highlight the adjacent cell in column B red when the cell in column C had a value of "Red":
=IF(C6="Red",TRUE,FALSE)
then just fill down with the fill handle as usual.
Rule editor (2007):