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

LP

我有一本工作簿 (A),其中有一个模块和一个子程序。该子程序从 Internet 下载一个 excel 文件(工作簿(B))并打开它。我面临的问题是找到一种方法从工作簿 (A) 中的子程序执行工作簿 (B) 中的子程序。

重申一下,我在工作簿 (A) 中有我想要的子例程,并希望通过使用工作簿 (A) 中的 sub 将其应用于工作簿 (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我在 sub 中添加了一个参数removeColumns,因此您可以传递所需的工作簿。

在 sub 中,您只需要在使用工作表的任何地方使用参考。

所以,而不是仅仅写:

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

希望我能帮上忙,如果还有什么不清楚的可以随时问

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从另一个工作簿运行宏

来自分类Dev

当修改的工作簿日期/时间大于另一个日期时运行的VBA宏

来自分类Dev

使用多个工作表对来自另一个工作簿的值求和的宏

来自分类Dev

Excel VBA:Shell可以在另一个Excel实例中的工作簿中运行宏

来自分类Dev

在另一个工作簿上运行宏,然后关闭启动该宏的工作簿

来自分类Dev

Excel宏-针对另一个工作簿运行

来自分类Dev

使用VBA通过个人工作簿宏打开另一个Excel文件

来自分类Dev

Excel VBA onkey宏在另一个宏运行时可以正常工作

来自分类Dev

当宏在当前工作簿上时使用另一个工作簿

来自分类Dev

比较一行并查看它是否存在于另一个工作簿中的 VBA 宏

来自分类Dev

将宏和模块从一个工作簿复制到另一个工作簿,并使所有VBA工作在一个主工作簿中

来自分类Dev

尝试在另一个工作簿中的一个工作簿中执行宏时发生错误

来自分类Dev

从一个工作簿到另一个工作簿的 Excel 宏数据传输

来自分类Dev

Excel宏(VBA)对照另一个工作簿中的列表检查单元格值

来自分类Dev

将启用宏的工作表从一个工作簿复制到另一个“运行时错误“1004”

来自分类Dev

从一个工作簿的VBA返回另一个工作簿的VBA的值

来自分类Dev

从另一个工作簿(加载项)中调用专用宏/ UDF

来自分类Dev

Excel宏将工作簿保存在另一个Excel会话中

来自分类Dev

将变量从一个工作簿转移到另一个工作簿vba

来自分类Dev

使用 vba 将数据从一个工作簿传输到另一个工作簿

来自分类Dev

没有宏将特定工作表从一个工作簿复制到另一个工作簿

来自分类Dev

从另一个工作簿中的另一个工作表在一个工作簿中的一个Excel工作表上执行筛选宏

来自分类Dev

创建一个宏以复制数据并将其粘贴到另一个工作簿

来自分类Dev

将工作表复制到另一个工作簿VBA

来自分类Dev

使用 VBA 将工作表复制到另一个工作簿

来自分类Dev

如果使用宏满足某些条件,则将一些行从一个工作簿复制到另一个工作簿

来自分类Dev

Excel宏:初学者-将格式从一个工作簿粘贴到另一个工作簿

来自分类Dev

要从一个工作簿复制到另一个工作簿的宏-不起作用

来自分类Dev

使用宏将一个工作簿中的单元格的值初始化到另一个工作簿

Related 相关文章

  1. 1

    从另一个工作簿运行宏

  2. 2

    当修改的工作簿日期/时间大于另一个日期时运行的VBA宏

  3. 3

    使用多个工作表对来自另一个工作簿的值求和的宏

  4. 4

    Excel VBA:Shell可以在另一个Excel实例中的工作簿中运行宏

  5. 5

    在另一个工作簿上运行宏,然后关闭启动该宏的工作簿

  6. 6

    Excel宏-针对另一个工作簿运行

  7. 7

    使用VBA通过个人工作簿宏打开另一个Excel文件

  8. 8

    Excel VBA onkey宏在另一个宏运行时可以正常工作

  9. 9

    当宏在当前工作簿上时使用另一个工作簿

  10. 10

    比较一行并查看它是否存在于另一个工作簿中的 VBA 宏

  11. 11

    将宏和模块从一个工作簿复制到另一个工作簿,并使所有VBA工作在一个主工作簿中

  12. 12

    尝试在另一个工作簿中的一个工作簿中执行宏时发生错误

  13. 13

    从一个工作簿到另一个工作簿的 Excel 宏数据传输

  14. 14

    Excel宏(VBA)对照另一个工作簿中的列表检查单元格值

  15. 15

    将启用宏的工作表从一个工作簿复制到另一个“运行时错误“1004”

  16. 16

    从一个工作簿的VBA返回另一个工作簿的VBA的值

  17. 17

    从另一个工作簿(加载项)中调用专用宏/ UDF

  18. 18

    Excel宏将工作簿保存在另一个Excel会话中

  19. 19

    将变量从一个工作簿转移到另一个工作簿vba

  20. 20

    使用 vba 将数据从一个工作簿传输到另一个工作簿

  21. 21

    没有宏将特定工作表从一个工作簿复制到另一个工作簿

  22. 22

    从另一个工作簿中的另一个工作表在一个工作簿中的一个Excel工作表上执行筛选宏

  23. 23

    创建一个宏以复制数据并将其粘贴到另一个工作簿

  24. 24

    将工作表复制到另一个工作簿VBA

  25. 25

    使用 VBA 将工作表复制到另一个工作簿

  26. 26

    如果使用宏满足某些条件,则将一些行从一个工作簿复制到另一个工作簿

  27. 27

    Excel宏:初学者-将格式从一个工作簿粘贴到另一个工作簿

  28. 28

    要从一个工作簿复制到另一个工作簿的宏-不起作用

  29. 29

    使用宏将一个工作簿中的单元格的值初始化到另一个工作簿

热门标签

归档