Access / excel vba中的范围问题

时间:2014-02-04 20:12:22

标签: excel vba

不确定我做错了什么。以下所有语句都不会产生错误,但它们不起作用。我之前有一个关于边界的问题,我认为它有效,但它没有

以下是代码段

With ws
   With .Range(.Cells(startRow, startCol), .Cells(endRow, endCol))
      .Borders.LineStyle = 1
      .Borders.Weight = 4
   End With
End With

ws.Range("D1").Value = 3.14159

With ws.Range("D1").Borders(9)
    .LineStyle = 1
    .Weight = 2
    .ColorIndex = 3
End With

ws.usedrange.Borders.LineStyle = 1
ws.usedrange.Borders.Weight = 4
ws.usedrange.Borders.Color = RGB(255, 0, 0)

With ws.Range("E1:F2")
    .Value = "x"
    .Font.Bold = True
    .Borders.LineStyle = 1
End With

2 个答案:

答案 0 :(得分:0)

可能是你的参数......以下是有效的:

Sub dural()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    startRow = 3
    startCol = 3
    endRow = 7
    endCol = 7
    With ws
       With .Range(.Cells(startRow, startCol), .Cells(endRow, endCol))
          .Borders.LineStyle = 1
          .Borders.Weight = 4
       End With
    End With
End Sub

答案 1 :(得分:0)

我从previous question获取了您的代码 并添加了最后四行来保存更改并关闭wb。这应该可以解决您的问题:

Sub setBorder()

    Dim xlApp As Object
    Dim wb As Object
    Dim ws As Object
    Dim rng As Object
    Dim startRow As Integer, startCol As Integer
    Dim endRow As Integer, endCol As Integer

    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False

    Set wb = xlApp.Workbooks.Open("H:\Documents\Misc-Work\BU\test.xlsx")

    'your code

    wb.Close True
    xlApp.Quit
    Set wb = Nothing
    Set xlApp = Nothing
End Sub

注意,既然您已经运行了以前的代码(没有关闭wb和xlApp),可能需要使用任务管理器关闭文件“H:\ Documents \ Misc-Work \ BU \ test.xlsx”(甚至如果看起来你没有打开这个文件的话