Add Border to Cell Range.

Add Border to Cell Range.

C_Haines_ENG
Collaborator Collaborator
1,353 Views
10 Replies
Message 1 of 11

Add Border to Cell Range.

C_Haines_ENG
Collaborator
Collaborator

I have tried everything VBA related to doing this and nothing fruitful has come up without providing me some sort of error. The code below is what seems to create the smallest error, however it doesn't seem to work none the less. 

 

.Range("B2:E2").Borders(xlEdgeBottom).Weight = xlMedium

I would like to be able to add a thick border to the bottom of a cell range.

 

As a bonus, does anyone know how to center and change the width of cells? Or bold cells?

0 Likes
Accepted solutions (2)
1,354 Views
10 Replies
Replies (10)
Message 2 of 11

BM_Ashraf
Advocate
Advocate

Hi,

The code you shared is working fine .

 

Sub Test_XL()

Dim excelApp As excel.Application
Dim ws As WorkSheet
Dim owb As Workbook

On Error Resume Next

Set excelApp = GetObject(, "Excel.Application")

' Try to connect to a running instance of Excel.
    If Err Then
        Err.Clear
        ' Couldn't connect so start Excel.  It's started invisibly.
        Set excelApp = CreateObject("Excel.Application")
        If Err Then
            MsgBox "Cannot access excel."
        End If
    End If
    
  excelApp.Visible = True
  excelApp.DisplayAlerts = False

  Set owb = excelApp.Workbooks.Add

  Set ws = owb.Worksheets(1)

    With ws
    
    .Range("B2:E2").value = "MechDS" & "Email:zezo7790@gmail.com"
    .Range("B2:E2").Font.Bold = True
    .Range("B2:E2").Columns.AutoFit
    .Range("B2:E2").Borders(xlEdgeBottom).Weight = xlThick
    End With
  
  'excelApp.Quit
  
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 3 of 11

C_Haines_ENG
Collaborator
Collaborator

Your code does not seem to work for me, ive posted the code ive written below:

 

'define the file to create/open
xlsPath = "C:\BOM Calculator\Test.xlsx"

'define Excel Application object
excelApp = CreateObject("Excel.Application")
'set Excel to run visibly, change to false if you want to run it invisibly
excelApp.Visible = True
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False



'check for existing file 
If Dir(xlsPath) <> "" Then
'workbook exists, open it
excelWorkbook = excelApp.Workbooks.Open(xlsPath)
ExcelSheet = excelWorkbook.Worksheets(1)
Else
'workbook does NOT exist, so create a new one
excelWorkbook = excelApp.Workbooks.Add
End If

']


'Insert data into Excel.
With excelApp
	
	If .Range("A1").Value < 2
		
		RowNum = 2
	   .Range("A1").Value = 2

	Else 
		
		RowNUM = .Range("A1").Value
		
	End If
	
			.Range("B" & RowNum) = iProperties.Value(modelName,"Project", "Part Number")
			.Range("C" & RowNum) = iProperties.Value(modelName, "Project", "Description")
		
End With   

'set all of the columns to autofit
excelApp.Columns.AutoFit   
'save the file
excelWorkbook.SaveAs(xlsPath)

''close the workbook and the Excel Application
''uncomment if you want to close the xls file at the end
excelWorkbook.Close
excelApp.Quit
excelApp = Nothing     
0 Likes
Message 4 of 11

tyler.warner
Advocate
Advocate

Beginning at line 26, you're inserting the data into the Excel application. Review MechDS' code from above. You want to be working on a specific worksheet. You had defined the worksheet if there was an existing one (line 17) but didn't create one otherwise and then you're not actually using it in your code.

 

Here's the full documentation for the borders & cells:

Excel Object Model on Borders (link) 

Excel Object Model on Cells (link) 

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
0 Likes
Message 5 of 11

C_Haines_ENG
Collaborator
Collaborator

Where do I have to declare this, I keep getting random errors.

 

.Worksheets(1).Range("B" & RowNum & ":" & "G" & RowNum).Borders(xlEdgeBottom).Weight = xlMedium

The documentation states that this is acceptable but it still doenst work.  

0 Likes
Message 6 of 11

tyler.warner
Advocate
Advocate

What are the errors that you're getting? Please provide images.

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
0 Likes
Message 7 of 11

A.Acheson
Mentor
Mentor
Accepted solution

Hi @C_Haines_ENG 

 

Implementing in VB.NET is a little harder than VBA. You will have to access objects in more depth adding the enumeration. 

For Example this is how to access your original snippet in VB.NET and here is the link to the Weight enumeration 

and the Border Enumeration

.Range("B2:E2").Borders(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium

 Here is the working method in VB.Net

 

AddReference "Microsoft.Office.Interop.Excel" 'To use excel 
Imports Microsoft.Office.Interop 'To use excel
Imports Microsoft.Office.Interop.Excel 'To use excel

Dim excelApp As Excel.Application = GetObject(, "Excel.Application") Try excelApp = CreateObject("Excel.Application") Catch MsgBox ("Cannot access excel.") End Try excelApp.Visible = True excelApp.DisplayAlerts = False Dim oWb As Workbook = excelApp.Workbooks.Add Dim oWs As Worksheet = oWb.Worksheets(1) With oWs .Range("B2:E2").Value = "This is a new Excel value" .Range("B2:E2").Columns.AutoFit Try .Range("B2:E2").Borders(XlBordersIndex.xlEdgeBottom).Color = RGB(255, 0, 0) .Range("B2:E2").Borders(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium Catch MessageBox.Show ("Error applying border.","iLogic") End Try End With

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 8 of 11

C_Haines_ENG
Collaborator
Collaborator

Here is the error im receiving:

 

Error on line 110 in rule: BOMCalc, in document: 751434.dwg

Exception from HRESULT: 0x800A03EC

 

.Range("B" & RowNum & ":" & "G" & RowNum).Borders(xlEdgeBottom).Weight = xlMedium

Below is how "excelApp" is being defined.

 

define Excel Application object
excelApp = CreateObject("Excel.Application")
'set Excel to run visibly, change to false if you want to run it invisibly
excelApp.Visible = True
'suppress prompts (such as the compatibility checker)
excelApp.DisplayAlerts = False



'check for existing file 
If Dir(xlsPath) <> "" Then
'workbook exists, open it
'excelWorkbook = excelApp.Workbooks.Open(xlsPath)
excelWorkbook = excelApp.Workbooks.Open(xlsPath)
ExcelSheet = excelWorkbook.Worksheets(1)
Else
'workbook does NOT exist, so create a new one
excelWorkbook = excelApp.Workbooks.Add
End If

 

0 Likes
Message 9 of 11

C_Haines_ENG
Collaborator
Collaborator

Hey you solved it!

 

.Range("B2:E2").Borders(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium

That did the trick. Thank you!

 

Is there a form where I can see all the different options? Like outside boxes, line weights etc? 

 

Would you also know how to format colums/cells, id like to center a column but the code ive found seems to have the same problems.

0 Likes
Message 10 of 11

A.Acheson
Mentor
Mentor
Accepted solution

Here is the RangeHorizontalAlignment and here is how a handy post discussing how to declare the value xlCenter

.Range("B2:E2").HorizontalAlignment = Excel.Constants.xlCenter

 Regarding a form unfortunately I don't. Internet searching is your best bet. Find it in VBA then check the documentation for how to implement in VB.NET

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 11 of 11

C_Haines_ENG
Collaborator
Collaborator

Double win, thanks a ton!

0 Likes