Roxana Vlad
asked on
Dynamic Ranges in VBA
Hello,
I am currently working on developing a VBA code and I got stuck on the following problem.
I have multiple VBA Codes which helps me format a table and the content within that table. First of all I am defining the table borders. Then, I am doing some formats on the cell selection. Is there a way to run the VBA without selecting the cells? If he finds borders to left and right on that row where I have the active cell, the to run the VBA between that borders?
Here are the codes on how I format the table borders, and then I set some format on some cells.
E.g. if I have D2 as active cell ( in a table from B2:H10) and I run macro to format as Subheading, the format will apply between B2:H2
Sub Callback1(control As IRibbonControl)
Selection.Borders(xlEdgeLe ft).LineSt yle = xlContinuous
Selection.Borders(xlEdgeRi ght).LineS tyle = xlContinuous
Selection.Borders(xlEdgeBo ttom).Line Style = xlContinuous
Selection.Borders(xlEdgeTo p).LineSty le = xlContinuous
Selection.font.Name = "Calibri"
Selection.font.Size = "9"
Selection.Interior.ColorIn dex = 2.5
Selection.RowHeight = 12
Selection.VerticalAlignmen t = xlCenter
End Sub
Sub Callback2(control As IRibbonControl)
Selection.RowHeight = 12
Selection.font.Name = "Calibri"
Selection.font.Size = "9"
Selection.Interior.ColorIn dex = 2.5
Selection.font.Bold = True
Selection.font.Color = vbBlack
Selection.Borders(xlEdgeTo p).LineSty le = xlContinuous
Worksheets("Tabelle1").Act ivate
Selection.VerticalAlignmen t = xlCenter
End Sub
Many thanks for your help!
Roxana
I am currently working on developing a VBA code and I got stuck on the following problem.
I have multiple VBA Codes which helps me format a table and the content within that table. First of all I am defining the table borders. Then, I am doing some formats on the cell selection. Is there a way to run the VBA without selecting the cells? If he finds borders to left and right on that row where I have the active cell, the to run the VBA between that borders?
Here are the codes on how I format the table borders, and then I set some format on some cells.
E.g. if I have D2 as active cell ( in a table from B2:H10) and I run macro to format as Subheading, the format will apply between B2:H2
Sub Callback1(control As IRibbonControl)
Selection.Borders(xlEdgeLe
Selection.Borders(xlEdgeRi
Selection.Borders(xlEdgeBo
Selection.Borders(xlEdgeTo
Selection.font.Name = "Calibri"
Selection.font.Size = "9"
Selection.Interior.ColorIn
Selection.RowHeight = 12
Selection.VerticalAlignmen
End Sub
Sub Callback2(control As IRibbonControl)
Selection.RowHeight = 12
Selection.font.Name = "Calibri"
Selection.font.Size = "9"
Selection.Interior.ColorIn
Selection.font.Bold = True
Selection.font.Color = vbBlack
Selection.Borders(xlEdgeTo
Worksheets("Tabelle1").Act
Selection.VerticalAlignmen
End Sub
Many thanks for your help!
Roxana
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Roy Cox,
Thanks a lot for your reply!
I did run your code but it does not make any changes to my data. I get the boxes with questions about the numbers of columns, rows and the starting cell, but id doesn't do anything.
Thanks a lot for your reply!
I did run your code but it does not make any changes to my data. I get the boxes with questions about the numbers of columns, rows and the starting cell, but id doesn't do anything.
Hi Roxana,
For any dynamic range, you need to have data first, you cannot expect code to run dynamically on blank cells. Else you need to specify the range.
For any dynamic range, you need to have data first, you cannot expect code to run dynamically on blank cells. Else you need to specify the range.
Have you tried running the code in sample I provided?
ASKER
Hi Shums,
I did tried your code. But for e.g. I have one row which I need to only format by adding a color for cells with no data in it. Isn't there a way to check if there are borders to left and right and then to run the macro in between these borders? This is an example only for one border.
So to have something like: If border is found left and border found right then run macro. Does it make any sense what I am saying?
Sub DynamicRange()
Dim i As Integer
i = 1
For i = 1 To 23
'checks if a border exists in the edge left
If Range(Cells(i, 1), Cells(i, 1)).Borders(xlEdgeLeft).Li neStyle _
<> xlNone Then
Cells(i, 2) = "True"
Else
Cells(i, 2) = "False"
End If
Next i
End Sub
I did tried your code. But for e.g. I have one row which I need to only format by adding a color for cells with no data in it. Isn't there a way to check if there are borders to left and right and then to run the macro in between these borders? This is an example only for one border.
So to have something like: If border is found left and border found right then run macro. Does it make any sense what I am saying?
Sub DynamicRange()
Dim i As Integer
i = 1
For i = 1 To 23
'checks if a border exists in the edge left
If Range(Cells(i, 1), Cells(i, 1)).Borders(xlEdgeLeft).Li
<> xlNone Then
Cells(i, 2) = "True"
Else
Cells(i, 2) = "False"
End If
Next i
End Sub
The code to change the formatting is yours, which you said worked.
The code to get the range is mine defines the range based on the answers provided.
I really don't see why you do not format the Range as a Table. There are so many advantages and you can even define your own design.
The code to get the range is mine defines the range based on the answers provided.
I really don't see why you do not format the Range as a Table. There are so many advantages and you can even define your own design.
Hi Roxana,
In attached, if you click Format Header, it will prompt you to select Header Cell, please select the first cell, which is E4.
If you click Format Data, it will prompt you to select Data Range First Cell, please select E5.
Hope this is what you wanted initially.
You don't need code to check if cells has borders or not, you can just remove all formatting before applying new one, which I have included in attached.
Kindly note, you need to have data to run above code.
FormatHeader-Data_v2.xlsm
In attached, if you click Format Header, it will prompt you to select Header Cell, please select the first cell, which is E4.
If you click Format Data, it will prompt you to select Data Range First Cell, please select E5.
Hope this is what you wanted initially.
You don't need code to check if cells has borders or not, you can just remove all formatting before applying new one, which I have included in attached.
Kindly note, you need to have data to run above code.
FormatHeader-Data_v2.xlsm
ASKER
Thanks a lot for all your answers!
My range won't have data in each cell. There can be rows that only need a font color change. My table can have even 50 columns and I didn't wanted to select each row that needs a format, for 50 columns...
I will try and re-do my code and use a Table instead of a Region surrounded by borders.
Roxana
My range won't have data in each cell. There can be rows that only need a font color change. My table can have even 50 columns and I didn't wanted to select each row that needs a format, for 50 columns...
I will try and re-do my code and use a Table instead of a Region surrounded by borders.
Roxana
The code I provided helps you set a dynamic range and formats using your code.
ASKER
My Excel does not have an actual Table in it. I am building one using VBA code by adding borders to a Region in Excel, and then Step by step, using other macros I am formatting the entire Table(or Region ).
This Region may change from one use to another, so I need to establish a Dynamic Range for my code. I will add a picture to make it more understandable. Also, for Heading and Sub Heading, I am kind of building my one with my formats.
If active cell is F6 and I run macro to format into Calibri, I want to actual format from E6:I6 the entire row with Calibri.
There are times when I have to format a Table with 30 columns and I don't want to have to select 30 cells to format.
Thank you!