我有一个工作簿,我有一些用户表单和代码。现在基于这个宏我将数据提取到另一个工作簿。现在,从第一个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。请帮忙。
答案 0 :(得分: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
如果您发现使用此方法甚至不要求您使用.Select
或.Activate
;)您可能还想阅读.Select
或this {1}}