In my worksheet, I have a table with buttons to add or erase a line below the last filled line. (It copies a range of pre-filled data and formatting from another sheet, or erases it - I don't delete the row in case it would mess with my other sheets).
When I copy the data it updates the table range, but when I erase it, it doesn't... so when I copy a new one, it skips a line.
This is the delete sub:
Sub DeleteLastLine()
'
' DeleteLastLine Macro
'
'
Sheets("Cadastro Geral").Select
Range("A" & Rows.Count).End(xlUp).EntireRow.Select 'I WOULD HAVE TO EITHER FIND A WAY TO SELECT THE LAST CELL WITH CONTENT IGNORING THE TABLE RANGE'
Selection.ClearContents
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
Dim tbl As ListObject
'OR FIND A WAY TO DYNAMICALLY RESIZE THE RANGE OF MY TABLE. NOW IT GOES FROM A3:AI913... TOMORROW IT MIGHT BE AT A3:AI950
Range("A" & Rows.Count).End(xlUp).Select
End Sub
I have a macro in my workbook that is tied to a hotkey which highlights all columns in the currently selected row. However, it only works if one row is selected. I cant think of a way to adjust it to highlight all the rows if multiple are selected. Here is the code that I am currently using.
Sub highlight_done()
'
' highlight_done Macro
'
' Keyboard Shortcut: Ctrl+q
'
Dim r As Long
r = ActiveCell.Row
Range("A" & r & ":Y" & r).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 12611584
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.Color = vbWhite
.TintAndShade = 0
End With
End Sub
Any help would be appreciated.
Perhaps like the following, using Intersect and Selection.EntireRow to get the range to be colored:
Sub highlight_done()
'
' highlight_done Macro
'
' Keyboard Shortcut: Ctrl+q
'
If Not TypeOf Selection Is Range Then Exit Sub
Dim rng As Range
Set rng = Intersect(Selection.EntireRow, Range("A:Y"))
With rng.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 12611584
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With rng.Font
.Color = vbWhite
.TintAndShade = 0
End With
End Sub
I want to execute a series of code based on row number but want to keep the selection of row number flexible, so that i can execute code for row 15:15, 20:20 etc
here is the code i tried...
Dim i As Integer
i = 15
Rows("i:i").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows("i-1:i-1").Select
Selection.Copy
Rows("i:i").Select
ActiveSheet.Paste
Rows("i:i").Select
Application.CutCopyMode = False
With Selection.Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("Ei-1").Select
Selection.ClearContents
Range("Ei-1").Select
ActiveCell.FormulaR1C1 = "=RC[-1]+(R[1]C-RC[-1])/2"
Range("Ei").Select
Something like this:
Dim i As Long
i = 15
Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows(i - 1).Copy Rows(i)
With Rows(i).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("E" & i - 1).FormulaR1C1 = "=RC[-1]+(R[1]C-RC[-1])/2"
New to VBA and just learning how to use relative references. I have an excel worksheet that will contain roughly 27 tables and I'll need to create 5 versions of this worksheet. The output from the tool I'm using is not in the format that's needed. Below is a screenshot of the output:
This is an example of what I'm trying to achieve:
Each of the tables in the worksheet will have a different number of rows. I've managed to get the code that bolds the question, centers the columns (from 'Total' to 'None') and highlights the different sections... but I can't get the code that bolds the row labels and percentages, or the row labeled 'column name.'
Here is my code for the first part:
Format Macro
'
' Keyboard Shortcut: Ctrl+Shift+F
'
ActiveCell.Select
Selection.Font.Bold = True
ActiveCell.Offset(2, 1).Range("A1:B1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveCell.Offset(-1, 3).Range("A1:E1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
ActiveCell.Offset(2, 0).Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
ActiveCell.Offset(-2, 8).Range("A1:B1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
ActiveCell.Offset(0, -11).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End Sub
Here's was my attempt to write the code for the bolding:
' Bold Macro
'
' Keyboard Shortcut: Ctrl+b
'
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = False
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = False
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
ActiveCell.Offset(3, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
End Sub
I've included a link to a sample file for reference.
It isn't in "relative referencing" where your problems lie but in referencing. Take this code.
Dim Ws As WorkSheet
Dim Rng As Range
Set Ws = Worksheets("Sheet1")
Set Rng = Ws.Cells(1, "A").Resize(2, 10)
The code first declares two objects, meaning, names are given. This simple setup allows you to avoid all "Select" or "Activate" statements and does away with all references to the ActiveSheet or the Selection object. That's about half your code.
Having defined the worksheet and given it a name you can refer to any cell on it, like Ws.Cells(13, "C").Font.Bold = True. Note that Cells(13, "C") could also (better) be addressed as Cells(13, 3). You can change the name of that sheet, like Ws.Name = "My New Name" and still refer to it in your code as Ws. You can specify ranges in it as shown above, either by specifying an offset (as the Resize method does) or by simply specifying first and last cell.
Set Rng = Ws.Range("A1:C37")
or
Set Rng = Ws.Range(Ws.Cells(1, 1), Ws.Cells(37, "A"))
which would usually be written like this:-
With Ws
Set Rng = .Range(.Cells(1, 1), .Cells(37, 1))
End With
You can also use the worksheet object to identify tables in the worksheet.
Dim Tbl As ListObject
Set Tbl = Ws.ListObjects(1)
or
Set Tbl = Ws.ListObjects("Table1")
That new object gives you access to all sorts of cells.
Set Rng = Tbl.DataBodyRange ' all the data below the header row
Set Rng = Tbl.HeaderRowRange ' the row with captions
Set Rng = Tbl.Range ' all of the table
Within each range cells are accessible by their row and column or by their index number. Tbl.Range.Cells(1) is the same as Tbl.Range.Cells(1, 1) or Tbl.HeaderRowRange.Cells(1). Each of these cells has a Font object which has a Bold property which you can set without ever selecting anything. And you might still refer to any of them by their worksheet coordinates.
Enough. Please take it from here.
I Set up a Macro that Finds the differences between the two rows and then highlights them. I want the macro to Cycle through the next two rows and do the same thing and go on until there are no more rows of data(This Number varies all the time). So the Next selection would be Rows 4:5 and it would Select the differences and highlight them and so on. How is this possible? Any help is greatly appreciated. Thank you,
FindVariance Macro
Rows("2:3").Select
Range("A3").Activate
Selection.ColumnDifferences(ActiveCell).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("F16").Select
End Sub
Try:
FindVariance Macro
For j=2 to Range("A1").End(xlDown).Row-1
i=j+1
Rows(j & ":" & i).ColumnDifferences(Range("A" & i)).Offset(1,0).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
j=j+1
Next j
End Sub
Here's my sample
Option Explicit
Sub FindVariance()
Dim last As Integer, i As Integer, r As Boolean
last = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
For i = 2 To last
If i Mod 2 = 0 Then
Rows(i & ":" & i + 1).Select
r = Selection.ColumnDifferences(ActiveCell).Select
If r = True Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End If
Next i
Range("F16").Select
End Sub
it's always a good habit to:
use objects reference and avoid the use of selections
which can be deceiving and slows down the code
use full reference for ranges, up to the workbook.
to avoid point to an unwanted active sheet or workbook!
so here's my code
Sub FindVariance()
Dim j As Long
Dim nRows As Long
With ActiveSheet
nRows = .Cells(.Rows.Count, 1).End(xlUp).Row
For j = 2 To nRows Step 2
With .Rows(j).Resize(2).ColumnDifferences(.Cells(j + 1, 1)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Next j
End With
End Sub
and there's still some job to do in order to catch and properly treat exceptions (uneven number of rows, empty rows...)