0

我有一本工作簿,里面有一些用户表单和代码。现在基于这个宏,我将数据提取到另一个工作簿中。现在从第一个 excel 我想在获取所需数据后格式化第二个工作簿。

下面是我的代码:

 Set abc = objWorkbook1.Sheets(report_shtnm)
     abc.Activate
   With Sheet2.Range(Cells(4, 1), Cells(rw_reps_sht, cl_reps_sht))
    .Borders.Weight = xlThin
    .WrapText = True
   End With

问题是它格式化具有宏的excel。请帮忙。

4

1 回答 1

1

最好的方法是正确声明您的对象,然后简单地使用它们...参见这个例子

Option Explicit

Sub Sample()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet

    '~~> This workbook which has the macro
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Sheets("Sheet")

    '~~> The other workbook
    Set wb2 = Workbooks.Open("C:\Sample.xlsx")
    Set ws2 = wb2.Sheets("Sheet1")

    '~~> Work with sheet1 in the workbook which has the macro
    With ws1
        '
        '~~> Your code here
        '
    End With

    '~~> Work with sheet1 in the second workbook
    With ws2
        '
        '~~> Your code here
        '
    End With
End Sub

如果您注意到使用此方法甚至不需要您使用.Selector .Activate;) 您可能还想阅读有关or.Select.Activate

于 2013-02-19T07:51:57.030 回答