簡體   English   中英

要格式化一個工作簿,但我在另一工作簿中寫入宏

[英]Want to format one workbook but i am wrinting the Macro in another workbook

我有一本工作簿,里面有一些用戶表單和代碼。 現在,基於此宏,我正在將數據提取到另一個工作簿中。 現在,從第一個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。 請幫忙。

最好的方法是正確聲明您的對象,然后簡單地使用它們。

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.Activate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM