簡體   English   中英

VBA:為另一個工作簿運行宏(不是來自)

[英]VBA : Run a macro FOR another workbook (not from)

我有一本工作簿(A),其中有一個帶有一個子例程的模塊。 該子例程從Internet下載Excel文件(工作簿(B))並打開它。 我面臨的問題是從工作簿(A)中的子程序中找到一種方法來執行工作簿(B)中的子例程。

重申一下,我在工作簿(A)中有我想要的子例程,並希望通過使用工作簿(A)中的子例程將其應用於工作簿(B)。

注意:在我的代碼工作簿(B)= Nuance Mobility JIRA.xls中,工作簿(B)中需要執行的所需子例程是removeColumns()。

我的代碼可以在下面找到:

Public Sub DL()

Dim WebUrl As String
Dim x As Workbook
Dim z As Workbook
Dim nmjexcel As String
Dim xlApp As Excel.Application

' I check to see if the file exists and delete it if it does
nmjexcel = "C:\Users\" & [A2] & "\Downloads\Nuance Mobility JIRA.xls"

If Len(Dir(nmjexcel)) <> 0 Then
    SetAttr nmjexcel, vbNormal
    Kill nmjexcel
End If

'I open chrome and download the file from an URL
WebUrl = [J1]
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)


Application.Wait (Now + TimeValue("0:00:3"))

'I create a new 'hidden' excel app and open workbook (B)
Set xlApp = New Excel.Application
xlApp.Visible = False

Set x = Workbooks.Open("C:\Users\" & [A2] & "\Downloads\Nuance Mobility JIRA.xls")

' I delete some rows, a picture and some columns. 
' It's here that i would like my other subroutine, removeColumns(), to take place !

With x.Sheets("general_report")

    .Rows("1:3").Delete
    .Shapes.Range(Array("Picture 1")).Delete
    .Cells.UnMerge
    .Range("A:A,D:D,E:E,F:F,H:H,I:I,J:J,K:K,L:L,M:M,N:N,O:O,P:P").Delete Shift:=xlToLeft

End With

'Then I copy whats left and paste it into workbook (A)
Set z = ThisWorkbook

Application.ScreenUpdating = False

x.Sheets("general_report").Range("A1").CurrentRegion.Copy

z.Sheets(1).Range("A13").PasteSpecial xlValues


x.Save
x.Application.CutCopyMode = False
x.Close

End Sub

我想要執行的子程序如下

Sub removeColumns()

Dim rng As Range 'store the range you want to delete
Dim c 'total count of columns
Dim I 'an index
Dim j 'another index
Dim headName As String 'The text on the header
Dim Status As String 'This vars is just to get the code cleaner
Dim Name As String
Dim Age As String
Dim sht As Worksheet

Rows("1:3").Delete


Key = "Key"
Summary = "Summary"
Status = "Status"
Set sht = Sheets("general_report")

sht.Activate 'all the work in the sheet "Incidents"
c = Range("A1").End(xlToRight).Column
'From A1 to the left at the end, and then store the number
'of the column, that is, the last column
j = 0 'initialize the var
For I = 1 To c 'all the numbers (heres is the columns) from 1 to c
    headName = Cells(1, I).Value
    If (headName <> Key) And (headName <> Summary) And (headName <> Status) Then
    'if the header of the column is differente of any of the options
        j = j + 1 ' ini the counter
        If j = 1 Then 'if is the first then
            Set rng = Columns(I)
        Else
            Set rng = Union(rng, Columns(I))
        End If
     End If
Next I
rng.Delete 'then brutally erased from leaf
End Sub

提前非常感謝您!

進一步的問題 :

1)有沒有辦法讓下載的Excel保持隱藏?

我有 :

Set xlApp = New Excel.Application

xlApp.Visible = False

Set x = Workbooks.Open("C:\Users\" & [A2] & "\Downloads\Nuance Mobility JIRA.xls")

但是如果我使用x = xlApp .Workbooks.Open它給我一個錯誤'下標超出范圍'並突出顯示:

Set sht = Sheets("general_report")

我試着做

Dim xlApp as Excel.Application)
...
Set sht = xlApp.Sheets("general_report")

但是會出現更多錯誤

2)更籠統地說,他們是否可以將注意力集中在我的工作簿(A)上,以便當chrome下載工作簿(B)時,chrome窗口不會在前面彈出?

由於您沒有直接解決所需的工作表/工作簿,而寧願始終使用“ Selected工作表,而您不應該直接面對,因此會出現您遇到的問題。 尚不清楚,直接引用即可完成。

為了引用worbookB我在子worbookB removeColumns添加了一個參數,因此您可以傳遞所需的工作簿。

然后,在子目錄中,您只需要在使用工作表的任何地方使用參考即可。

因此,不只是寫:

somVariable = Cells(1,1).Value 'This always refers to the 'Selected' worksheet

你必須寫:

someVariable = myWorkbook.myWorksheet.Cells(1,1).Value

'or to use the parameter wb like i did in your code:

someVariable = wb.Sheets(1).Cells(1,1).Value
'Here the first sheet of this workbook will be used

'You also can use the 'With' statment here:
With wb.Sheets(1)
    someVariable = .Cells(1,1).Value 'Note the dot in font of the 'Cells'
End With

因此,在您的示例中使用此知識,您應該嘗試更改代碼,如下所示:

/////////////////////////////////////////////////////////////////////////  
Set xlApp = New Excel.Application

xlApp.Visible = False
xlApp.Workbooks.Open("C:\Users\" & [A2] & "\Downloads\Nuance Mobility JIRA.xls")

Set x = xlApp.Workbooks(1)

Call removeColumns(x)

/////////////////////////////////////////////////////////////////////////

Sub removeColumns(ByVal wb As Workbok)

...    

'Always when you are referring to the workbook, you have to use the reference passed as parameter
wb.Sheets("general_report").Rows("1:3").Delete
'In you code the first three rows will always be deleted from the 'Selected' sheet and not the one you are working on later, the 'general_report'
...

Set sht = wb.Sheets("general_report") 

'Also don´t activate() sheet here, youst directly refer to it later
'sht.Activate 'all the work in the sheet "Incidents"

'You can directly refer t it over the variable you created, like this:
c = sht.Range("A1").End(xlToRight).Column
'From A1 to the left at the end, and then store the number
'of the column, that is, the last column

j = 0 'initialize the var
For I = 1 To c 'all the numbers (heres is the columns) from 1 to c
    headName = sht.Cells(1, I).Value
    If (headName <> Key) And (headName <> Summary) And (headName <> Status) Then
    'if the header of the column is differente of any of the options
        j = j + 1 ' ini the counter
        If j = 1 Then 'if is the first then
            Set rng = sht.Columns(I)
        Else
            Set rng = Union(rng, sht.Columns(I))
        End If
     End If
Next I

rng.Delete 'then brutally erased from leaf
End Sub

希望我能幫忙,如果還有不清楚的問題請隨便問

暫無
暫無

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

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