I have the following data in Excel:
1.07 ± 0.35^a 1.21 ± 0.13^a 0.67 ± 0.31^a 1.43 ± 0.05^a
I am looking for a macro to change the text after the ^ symbol to superscript, whilst also removing the ^ symbol. I thought I had found the answer from this site http://www.beingbrunel.com/inline-subsuper-script-in-excel-and-more/, but I can't get the add-in to work.
This is my attempted code, but no cigar.
Sub Loop_Exampl()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the A column in this example
With .Cells(Lrow, "B")
With ActiveCell.Characters(Start:=2, Length:=1).Font
.Superscript = False
.Subscript = True
End With
End Sub
Not sure how you'd do this without the ^ Maybe superscript every letter which directly c=follows a digit ?
Sub tester()
Dim c As Range
For Each c In Selection.Cells
SuperIt c
Next c
End Sub
Sub SuperIt(rng As Range)
Dim s, p, e
s = rng.Text
p = InStr(s, "^")
If p > 0 Then
Do
e = 1
Do While Mid(s, p + e, 1) <> " " And p + e < Len(s)
e = e + 1
Loop
rng.Characters(p, 1).Delete
rng.Characters(p, e).Font.Superscript = True
s = rng.Text
p = InStr(s, "^")
Loop While p > 0
End If
End Sub
This code will create superscripts of letters in the selected range, meaning that the ^ symbol is not required.
Sub FixFormatting()
Dim c As Range
Dim StartCells As Range
Dim ws As Worksheet
Dim intPlace As Integer
Dim wsStartsProtected As Boolean
Application.ScreenUpdating = False
On Error GoTo errorCatch
Set StartCells = Selection
For Each c In Selection.Cells
With c
.Replace What:="a", Replacement:="a", LookAt:=xlPart, MatchCase:=False
.Replace What:="b", Replacement:="b", LookAt:=xlPart, MatchCase:=False
.Replace What:="c", Replacement:="c", LookAt:=xlPart, MatchCase:=False
End With
intPlace = InStr(c.Value, "a")
If intPlace > 0 Then
If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect
c.Characters(intPlace, 1).Font.Superscript = True
End If
intPlace = InStr(c.Value, "b")
If intPlace > 0 Then
If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect
c.Characters(intPlace, 1).Font.Superscript = True
End If
intPlace = InStr(c.Value, "c")
If intPlace > 0 Then
If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect
c.Characters(intPlace, 1).Font.Superscript = True
End If
If wsStartsProtected Then ws.Protect
Next
StartCells.Parent.Activate
StartCells.Select
Application.ScreenUpdating = True
Exit Sub
errorCatch:
If wsStartsProtected Then ws.Protect
StartCells.Parent.Activate
StartCells.Select
Application.ScreenUpdating = True
End Sub
Related
I am using the following VBA code to save each individual sheet in a .xlsx workbook into a .csv file.
Whilst the code is working well I would like to adapt the VBA code so blank columns & rows are removed from the .csv files which are being created.
Existing VBA Code:
Public Sub SaveWorksheetsAsCsv()
Dim xWs As Worksheet
Dim xDir As String
Dim folder As FileDialog
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.SaveAs xDir & "\" & xWs.Name, xlCSV
Next
End Sub
To remove blank rows & columns I was able to get the below JavaScript working in a .hta application, but would like to integrate this same functionality into the above VBA code.
//Remove all blank rows
for(var i = usedRng.Rows.Count; i > 0; i--){
if( xlApp.CountA(usedRng.Rows(i)) == 0 ) usedRng.Rows(i).Delete();
}
//Remove all blank columns
for(var i = usedRng.Columns.Count; i > 0; i--){
if( xlApp.CountA(usedRng.Columns(i)) == 0 ) usedRng.Columns(i).Delete();
}
How can I integrate this row/column removal code into VBA?
Use below sub-routine to delete empty row/column in the spreadsheet
Sub RemoveEmptyRowColumn()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
CalcMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
ActiveSheet.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
Firstrow = ActiveSheet.UsedRange.Cells(1).Row
Lastrow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
FirstColumn = ActiveSheet.UsedRange.Cells(1).Column
LastColumn = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
'------------------
' Delete Empty Rows
'------------------
For Lrow = Lastrow To Firstrow Step -1
For LColumn = LastColumn To FirstColumn Step -1
With ActiveSheet.Cells(Lrow, LColumn)
If Not IsError(.Value) Then
If .Value = "" Then
DeleteRow = "Yes"
Else
DeleteRow = "No"
Exit For
End If
End If
End With
Next LColumn
If DeleteRow = "Yes" Then
ActiveSheet.Cells(Lrow, LColumn + 1).EntireRow.Delete
End If
Next Lrow
'---------------------
' Delete Empty Columns
'---------------------
For LColumn = LastColumn To FirstColumn Step -1
For Lrow = Lastrow To Firstrow Step -1
With ActiveSheet.Cells(Lrow, LColumn)
If Not IsError(.Value) Then
If .Value = "" Then
DeleteColumn = "Yes"
Else
DeleteColumn = "No"
Exit For
End If
End If
End With
Next Lrow
If DeleteColumn = "Yes" Then
ActiveSheet.Cells(Lrow + 1, LColumn).EntireColumn.Delete
End If
Next LColumn
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
I have some coding which loops through a list of policy numbers, and then searches through an entire workbook for a matching policy number, bolding any matches found in the original list. The coding works fine, however what I would like to do if possible, is to 'print' the address/cell location of the matching value. For example, if policy number 1 was located on sheet 3, cell a1, then this would be displayed on a new blank worksheet. I'm sure this can be done using dictionaries and the print.debug function, But I want to avoid using a dictionary if possible. Hope this makes sense!
Sub HighlightMatches()
Application.ScreenUpdating = True
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long,bln As Boolean
iRowL = Sheets(3).Cells(Rows.Count, "P").End(xlUp).row
For iRow = 1 To iRowL
If Not IsEmpty(Cells(iRow, 16)) Then
For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
bln = False
var = Application.match(Cells(iRow, 16).Value, Worksheets(iSheet).Columns(16), 0)
If Not IsError(var) Then
bln = True
Exit For
End If
Next iSheet
End If
' The below would 'print' all matches and an offset value to a new Worksheet.
If bln = False Then
Cells(iRow, 16).Font.Bold = False
Else
Cells(iRow, 16).Font.Bold = True
End If
Next iRow
Application.ScreenUpdating = True
End Sub
This would do it:
Option Explicit
Sub HighlightMatches()
Dim OriginalWS As Worksheet, NewListWorksheet As Worksheet, ws As Worksheet
Dim Cel As Range, Rng As Range, rSearchCell As Range
Set OriginalWS = ActiveSheet
Set Rng = Columns("P:P").SpecialCells(xlCellTypeConstants, 23)
For Each Cel In Rng
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> OriginalWS.Name And ws.Name <> "Search List" Then
On Error Resume Next
Set rSearchCell = ws.Columns("P:P").Find(What:=Cel.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
On Error GoTo 0
If Not rSearchCell Is Nothing Then
Cel.Font.Bold = True
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Search List"
On Error Resume Next
Set NewListWorksheet = ActiveSheet
On Error GoTo 0
If Not NewListWorksheet Is Nothing Then
NewListWorksheet.Range("A" & NewListWorksheet.Range("A" & NewListWorksheet.Rows.Count).End(xlUp).Row + 1).Value = Cel.Value
Else
Set NewListWorksheet = Sheets.Add.Name = "Search List"
NewListWorksheet.Range("A1").Value = "Search results"
NewListWorksheet.Range("A1").Font.Bold = True
NewListWorksheet.Range("A2").Value = Cel.Value
End If
Else
Cel.Font.Bold = False
End If
End If
Set rSearchCell = Nothing 'Clear Cel
Next
Next
End Sub
i'm actually trying to, for a command button, set a print area based on colomn A (if A is empty then this row is last row. and when the print area is set, just print it out with landscape layout. My code for now is as follow. When i clic it prints but doesn't update the print area can you help me plz
Private Sub Imprimer_Click()
ActiveSheet.Unprotect Password:="mypass"
Dim usedRangeEx As Range
Set usedRangeEx = GetUsedRangeIncludingCharts(ActiveSheet)
usedRangeEx.Activate
Debug.Print usedRangeEx.Address
ActiveSheet.Protect Password:="mypass"
End Sub
Private Function GetUsedRangeIncludingCharts(target As Worksheet) As Range
ActiveSheet.Unprotect Password:="mypass"
Dim firstRow As Long
Dim firstColumn As Integer
Dim lastRow As Long
Dim lastColumn As Integer
Dim oneChart As ChartObject
For Each cell In Range("A5:A65")
If Not IsEmpty(cell) Then
lastRow = cell.Row
End If
Next
With target
firstRow = .UsedRange.cells(1).Row
firstColumn = .UsedRange.cells(1).Column
lastColumn = .UsedRange(.UsedRange.cells.Count).Column
Set GetUsedRangeIncludingCharts = .Range(.cells(firstRow, firstColumn), _
.cells(lastRow, lastColumn))
End With
ThisWorkbook.ActiveSheet.PrintOut
With ActiveSheet.PageSetup
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ActiveSheet.Protect Password:="mypass"
End Function
Set your print area
Sub Button1_Click()
Dim LstRw As Long, PrnG As Range
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Set PrnG = Range("A1:C" & LstRw) ' or whatever column you want
ActiveSheet.PageSetup.PrintArea = PrnG.Address
End Sub
I'm trying this, but it's not working:
Sub macro1()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim LR As Long
With ActiveSheet
LR = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
For r = 2 To LR
p = Application.Match(Cells(r, 3), Sheets("Input").Range("C:C"), 0)
If IsError(p) Then GoTo nextr:
Cells(r, 4).Value = .Value & ",newsletter" '***here***
nextr:
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
If I use the following on the line marked here :
Cells(r,4) = "Y"
It works, so I guess I've done my append wrong?
Just to spell out the answer and to format it nicely:
Option Explicit
Sub macro1()
Dim p As Double
Dim LR As Long
Dim r As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
LR = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
For r = 2 To LR
p = Application.Match(Cells(r, 3), Sheets("Input").Range("C:C"), 0)
If Not IsError(p) Then
ActiveSheet.Cells(r, 4).Value = ActiveSheet.Cells(r, 4).Value & ",newsletter" '***here***
End If
Next r
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
I have an Excel sheet with the following structure:
What I need to do is delete an entire record if either it's Type A or Type B are = 0. As an example, for record 1, I need to delete A & B because B = 0.
I have the following code:
Sub Loop_Example()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "B")
If Not IsError(.Value) Then
If .Value = "0" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
Therefore, what I would like to do is add the logic to delete the entire row if the value is 0 and either the row above or below depending on its 'type'.
Thanks.
this should work.
Sub pDeleteRow()
Dim wksData As Worksheet
Dim rngCell As Range
Dim lngCounter As Long
Dim lngTotalCount As Long
Set wksData = Worksheets("Sheet1")
lngTotalCount = wksData.Range("A1").CurrentRegion.Rows.Count
lngCounter = 1
With wksData
While lngCounter <= lngTotalCount
If (UCase(Trim(.Cells(lngCounter, 2))) = "A" Or UCase(Trim(.Cells(lngCounter, 2))) = "B") And UCase(Trim(.Cells(lngCounter, 3))) = "0" Then
.Cells(lngCounter, 1).EntireRow.Delete
lngCounter = lngCounter - 1
lngTotalCount = lngTotalCount - 1
End If
lngCounter = lngCounter + 1
Wend
End With
End Sub
You can Try This:
Sub ConditionalRowDelete()
Set colA = Range("C1", Cells(Rows.Count, "C").End(xlUp))
Set colB = Range("D1", Cells(Rows.Count, "D").End(xlUp))
MsgBox colA.Rows.Count
For i = 1 To colA.Rows.Count
If colB(i) = 0 Then
If colA(i) = "A" Then
'colB(i).Select
With colB(i) 'Selection
Application.Union(.EntireRow, .Offset(1, 0).EntireRow).Delete 'Select
End With
'Selection.EntireRow.Select
'MsgBox "found A"
End If
If colA(i) = "B" Then
'colB(i).Select
With colB(i) 'Selection
Application.Union(.EntireRow, .Offset(-1, 0).EntireRow).Delete 'Select
End With
'MsgBox "found B"
End If
End If
Next
End Sub