Excel macro: how can I select/treat cells with ending superscript charatcers? - excel

I'm currenlty putting together an If, ElseIf and Else statement to go through the lists and make it standard. The parts where I'm struggling are those cells where a superscript character has been used at the end of the sentaence for footnote references. These cells need to be given specific line heights different from the standard 10.5.
Thank you marg for the working If, ElseIf, Else statement.
But how can I find & treat those cells with a superscript character at the end (it's always at the end?
Any guidance or pointers very welcome.
The code below does not update cells with superscript only at the end of the sentence.
Dim targetCell As Range
...
ElseIf targetCell.font.superscript Then
targetCell.RowHeight = 12.75
...
etc
Many thanks
Mike.

This should do it.
Sub testForSuperscriptAtEndOfCellValue()
Dim c As Range
For Each c In Range("A:A").Cells
If c.Characters(Len(c), 1).Font.Superscript Then
c.EntireRow.RowHeight = 42
End If
Next c
End Sub

Related

How can align text to the right using VBA, if a cell contains a specific value?

I am trying to fix a spreadsheet, so that if the word TRAVEL is in a cell, then the contents of the cell is right justified.
I have looked into what VBA code is generated when you right justify (.HorizonalAlignment = xlRight) but I feel my implementation is the issue.
I am also confining it to a certain range (A3 - A101)
Private Sub CellHasTravel()
If Range("A3:A101").Value == "TRAVEL" Then
.HorizonalAlignment = xlRight
End If
End Sub
I was expecting this code to actually right justify a cell, but instead I am presented with
Compile Error: Syntax error
Any help would be appreciated.
Sub travel()
Dim cells As Range
For Each cells In ActiveSheet.Range("A3:A101")
If cells.Value = "TRAVEL" Then
cells.HorizontalAlignment = xlRight
Else
End If
Next cells
End Sub
Hope you find usefull and I could help.

How do I write an WITH statement that loops through rows within the WITH-formula?

I'm building an interactive Gantt chart with many rows (which is why I use VBA) and I'm having trouble with formatting my cells. Basically I just want the cells to have color based on an AND-formula. The formula is referring to other cells, so the formatting is not based on the cells own value.
The tricky part is that my formula needs to change according to the specific row that it's looping through. I managed to build a code that correctly loops through each row, but I can't get the formula to change as well. Right now my code gives me an syntax error in the formula-part of my with-expression.
I hope you guys can help!
I figured that the problem might be that the concatenate-trick might not work with with-statements. But I don't know how I can do the formatting in any other way.
Public Sub FormatTest()
Dim Sheet As Object
Dim Area As Range, r As Range
Dim i As Integer
Set Area = Sheets("Styregruppe - Tester").Range("H11:BK58")
For Each r In Area.Rows
For i = 11 To 58
With r.FormatConditions
.Delete
With .Add(Type:=xlExpression, Formula1:="=OG(("C" & i)<=H8;("D" & i)>=H8)")
.Interior.Color = RGB(0, 176, 240)
.StopIfTrue = False
End With
End With
Next i
Next r
End Sub
This line: .Add(Type:=xlExpression, Formula1:="=OG(("C" & i)<=H8;("D" & i)>=H8)")
Has improper quotes you took the C out of the quotes so it thinks its a variable and the ampersand is within the quotes so is being interpreted as a string character and not as the concatenation character.
You want something like: .Add(Type:=xlExpression, Formula1:="=OG((C" & i & ")<=H8;(D" &
i & ")>=H8)")

Converting a static formula to selection code VBA macro

I'm working on an record processing tool and have run into a problem.
One of our clients provides us with raw data that contains carriage breaks - making it unusable until formatted. Unfortunately the easy solution of running a clean function can't be used as it erases the last digit of the 16-digit account numbers in the process. Hoping that every end user remembers to keep destination formatting when porting the data over to have it formatted into text strings isn't really an option either.
So here's my plea for help:
I was able to get a clean value by using this formula
=IF(RIGHT(A2,1)=" ",LEFT(A2,LEN(A2)-1),A2)
I now need to turn it into a macro that will change the cell.value of the selected cells to the result of the formula. But after 8 hours of banging my head against the wall I just can't figure out how to do it. Any help is much appreciated!
Your formula in VBA:
Sub Klean()
Dim Cell As Range
For Each Cell In Selection
v = Cell.Value
If Right(v, 1) = " " Then
v = Left(v, Len(v) - 1)
Cell.Value = v
End If
Next Cell
End Sub
You need a function that iterates over a given set of cells. You can call that function using whatever cells are currently selected.
Option Explicit
Sub DoSomethingCool()
CleanCellText Selection
'--- or ---
CleanCellText ActiveSheet.Range("A1:Z20")
End Sub
Public Sub CleanCellText(ByRef thisRange As Range)
Dim cell As Range
For Each cell In thisRange
If Right$(cell.Value, 1) = " " Then
cell.Value = Left$(cell.Value, Len(cell.Value) - 1)
End If
Next cell
End Sub
Many thanks to everyone for such quick and informative responses!
I have managed to identify the issue - turns out as well as a carriage break the raw data also contained a tab space in front of the data that twas causing all the issues and was refusing to be edited by trim or manual edit! Here's the final code fr the macro button I embedded that solved the issue:
Private Sub CommandButton6_Click()
Dim Rng As Range
Set Rng = Selection
For Each cell In Rng
If IsEmpty(cell.Value) = False Then
cell.Value = Replace(cell.Value, Chr(9), "'")
End If
Next cell
End Sub
I've set the tab space to be replace by an apostrophe so that final value does not end up getting truncated, like what was happening when i used the clean function.
Once again many thanks everyone!

VBA checking cell interior color

I am trying to do something depending on the interior color of a cell.
This is my code so far but it is showing errors on the If line.
For i = 3 To dumpLastRow
With masterFile.Sheets(dumpRef)
If .Range("A", i).Interior.ColorIndex = 4 Then
''''CODE''''
Else
''''CODE''''
End If
End With
Next
If you have any idea it would be appreciated. Thanks
as alternative this version might be a bit easier to work with
With masterFile.Sheets(dumpRef)
Dim cell As Range
For Each cell In .Range("A3:A" & dumpLastRow).Cells
If cell.Interior.ColorIndex = 4 Then
''''CODE''''
Else
''''CODE''''
End If
Next
End With
You cannot combine letters and numbers like that in range. Use cells instead. You will need to put in cells twice as Range requires that when using cells to populate it.
Range(Cells(i, 1), Cells(i, 1)).Interior.ColorIndex

Assign formula to cell using VBA?

VBA has been giving me an error 1004 for this code:
Sub UpdateCellsFormula()
Dim FormulaRange As Range
Dim i As Integer
Set FormulaCellsRange = Range("J17", "J95")
i = 17
For Each r In FormulaCellsRange.Cells
r.Formula = Replace("=D17*L21+E17*M21+F17*O21+G17*N21+H17*P21+I17*Q21)/(1+0,85+0,6+0,4+0,37)", "17", i)
i = i + 1
Next
End Sub
Can anyone have a look at it?
Formulas assigned with .Formula must be in En-US locale. The decimal dot in numbers must therefore be ., not , (1+0.85+0.6+0.4+0.37).
Also you have a missing opening parenthesis in the beginning, right after =.
You also might want to learn about absolute references in Excel. That way you can copy the same formula into all cells in FormulaCellsRange without replacing anything:
Sub UpdateCellsFormula()
ActiveSheet.Range("J17", "J95").Formula = "=(D17*$L$21+E17*$M$21+F17*$O$21+G17*$N$21+H17*$P$21+I17*$Q$21)/(1+0.85+0.6+0.4+0.37)"
End Sub

Resources