When I run the following code from an external spreadsheet and supply the file name, I get the error Application-defined or object-defined error on the line
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous 'Error Here
However if I cut and paste this code in the local spreadsheet as opposed to running it from another spreadsheet it works just fine. Any ideas? Thanks.
Sub Borders()
Dim WB, objexcel, colp, colq, rowx, rowy, incrowy, stringrange, rwindex, filename
Set objexcel = CreateObject("Excel.Application")
filename = InputBox("Enter ANTIBIOGRAM File Name")
Set WB = objexcel.Workbooks.Open("C:\Documents and Settings\pasvarghes\Desktop\" & filename & ".xls")
With WB.Sheets(1)
colp = "A"
colq = InputBox("Enter Last Column")
rowx = 3
rowy = InputBox("Enter Last Row")
incrowy = rowx + 2
For rwindex = rowx To rowy Step 1
If incrowy > rowy + 1 Then
Exit For
End If
stringrange = colp & rowx & ":" & colq & incrowy
.Range(stringrange).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous 'Error Here
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
rowx = rowx + 4
incrowy = rowx + 2
Next rwindex
stringrange = "a1" & ":" & colq & "1"
.Range(stringrange).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
WB.Save
WB.Close
objexcel.Quit
Set WB = Nothing
Set objexcel = Nothing
MsgBox "Done"
End Sub
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous 'Error Here
However if I cut and paste this code in the local spreadsheet as opposed to running it from another spreadsheet it works just fine. Any ideas? Thanks.
Sub Borders()
Dim WB, objexcel, colp, colq, rowx, rowy, incrowy, stringrange, rwindex, filename
Set objexcel = CreateObject("Excel.Application")
filename = InputBox("Enter ANTIBIOGRAM File Name")
Set WB = objexcel.Workbooks.Open("C:\Documents and Settings\pasvarghes\Desktop\" & filename & ".xls")
With WB.Sheets(1)
colp = "A"
colq = InputBox("Enter Last Column")
rowx = 3
rowy = InputBox("Enter Last Row")
incrowy = rowx + 2
For rwindex = rowx To rowy Step 1
If incrowy > rowy + 1 Then
Exit For
End If
stringrange = colp & rowx & ":" & colq & incrowy
.Range(stringrange).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous 'Error Here
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
rowx = rowx + 4
incrowy = rowx + 2
Next rwindex
stringrange = "a1" & ":" & colq & "1"
.Range(stringrange).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
WB.Save
WB.Close
objexcel.Quit
Set WB = Nothing
Set objexcel = Nothing
MsgBox "Done"
End Sub