VBA:为另一个工作簿运行宏(不是从)

VBA:为另一个工作簿运行宏(不是从),vba,excel,Vba,Excel,我有一个工作簿,其中有一个模块和一个子例程。子例程从internet下载excel文件workbookB并将其打开。我面临的问题是如何从工作簿a中的子例程执行工作簿B中的子例程 重申一下,我只在工作簿A中有我想要的子例程,希望通过使用工作簿A中的子例程将其应用于工作簿B 注意:在我的代码工作簿B=Nuance Mobility JIRA.xls中,工作簿B中需要执行的子例程是removeColumns 我的代码可以在下面找到: Public Sub DL() Dim WebUrl As Str

我有一个工作簿,其中有一个模块和一个子例程。子例程从internet下载excel文件workbookB并将其打开。我面临的问题是如何从工作簿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
但是如果我使用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窗口不会在前面弹出?

您面临的问题是因为您不直接处理所需的工作表/工作簿,而是始终使用所选的工作表,而您不应该这样做。它不清楚,如果直接引用,也可以简单地完成

为了引用worbookB,我在子removeColumns中添加了一个参数,以便您可以传递所需的工作簿

在sub-then中,您只需在使用工作表的任何位置使用引用即可

因此,与其只是写:

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)

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

希望我能提供帮助,如果还有什么不清楚的地方,请尽管问。

当你在A for B中运行例程时,会发生什么?我很确定这些问题是由于使用了不合格的对象范围、工作表等造成的@A.S.H基本上,我下载的excel文件中有大量我不想要的列,并且列的数量可以根据运行代码的用户的不同而变化,因此,与其逐个删除所有列,我只保留包含特定标题键、摘要、状态的列,并删除其他列。我在运行代码时没有错误消息,我只需要实现或调用A/for B中的removeColumns子命令,将代码转换为XLAM文件,然后将其作为外接程序添加到Excel,然后确保您正在处理的工作簿/工作表符合要求。非常感谢,这完全奏效了!我从来没有想过直接引用它。我对VBA或编码几乎没有任何知识,我只是边学习边在网上找到我能找到的工作工具:没问题,不直接引用是初学者常见的错误。只需确保使用尽可能少的代码。选择或。激活尽可能多的代码,并尝试编写尽可能清晰直接的代码,即使这可能是一个多一点的代码:我还有一个更简单的问题,如何使x可以与xlApp.Visible和Call命令一起使用?Set xlApp=New Excel.Application xlApp.Visible=False Set x=xlApp.Workbooks.OpenC:\Users\&[A2]&\Downloads\Nuance Mobility JIRA.xls Call removeColumnsx请不要在注释中写入多行代码,因为这很不清楚,只需编辑你的问题,并将其与下一期文章放在现有文本下。很抱歉,我以为我可以将代码以与上面相同的格式放入注释中:s但我编辑了我的问题,你已经为我做了很多,我希望你能再次帮助我!如果不担心,我最终会让它工作:
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