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

时间:2017-11-04 19:23:43

标签: excel-vba vba excel

我正在尝试编写一个宏,它将添加当前工作簿的一些列,以及其他工作簿。我有点擅长Vba,我试着在互联网上找到答案,然后再问这里。提前致谢

我的代码:

Sub Mono_recurso()
'
' Mono_recurso Macro
'
lin_ori = 2
lin_dest = 2


Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet

Set wkb = Excel.Workbooks("C:\Users\Feels Bad Man\Desktop\exemplo.xlsm")
Set wks = wkb.Worksheets("Tabela de síntese")


Do While Sheets("Mono").Cells(lin_ori, 1) <> ""


Sheets("Mono recurso").Cells(lin_dest, 1) = Sheets("Mono").Cells(lin_ori, 1)
Sheets("Mono recurso").Cells(lin_dest, 2) = Sheets("Mono").Cells(lin_ori, 2)

lin_ori = lin_ori + 1
lin_dest = lin_dest + 1

Loop



lin_ori = 3
lin_dest = 2


Do While Sheets("Mono recurso").Cells(lin_dest, 1) <> ""

Do While wkb.wks.Cells(lin_ori, 2) ' <> ""))


If Sheets("Mono recurso").Cells(lin_dest, 1) = wkb.wks.Cells(lin_ori, 2) Then


    Sheets("Mono recurso").Cells(lin_dest, 3) = wkb.wks.Cells(lin_ori, 6)
    Sheets("Mono recurso").Cells(lin_dest, 4) = wkb.wks.Cells(lin_ori, 7)
    Sheets("Mono recurso").Cells(lin_dest, 5) = wkb.wks.Cells(lin_ori, 8)
    Sheets("Mono recurso").Cells(lin_dest, 6) = wkb.wks.Cells(lin_ori, 15)
    Sheets("Mono recurso").Cells(lin_dest, 7) = wkb.wks.Cells(lin_ori, 16)

    lin_ori = lin_ori + 1

Else

    lin_ori = lin_ori + 1

End If

Loop

lin_dest = lin_dest + 1



Loop

'
End Sub

我尝试了几种解决方案,但我的代码只运行直到达到此目的:

Set wkb = Excel.Workbooks("C:\Users\Feels Bad Man\Desktop\exemplo.xlsm")

1 个答案:

答案 0 :(得分:1)

我的假设是您要从代码中打开其他工作簿,将其部分值传输到包含宏的工作簿中,然后关闭其他工作簿。

您的代码存在一些问题,我在下面尝试解决这些问题。评论提供了一些细节。

'Always put Option Explicit at the top of your modules.
'Declare all variables and compile your code (menu: Debug / Compile).
Option Explicit

'
' Mono_recurso Macro
'
Sub Mono_recurso()
    Dim exemploWbk As Excel.Workbook
    Dim tabelaSinteseWks As Excel.Worksheet
    Dim monoWks As Excel.Worksheet
    Dim monoRecursoWks As Excel.Worksheet
    Dim lin_ori As Long
    Dim lin_dest As Long

    'Open the workbook.
    'Note: there are many options to the Open method; check them online.
    Set exemploWbk = Workbooks.Open("C:\Users\Feels Bad Man\Desktop\exemplo.xlsm")

    'Obtain a reference to the target worksheet.
    Set tabelaSinteseWks = exemploWbk.Worksheets("Tabela de síntese")

    'Obtain references to local worksheets.
    'Notice the use of ThisWorkbook; without it, Excel would try to find worksheet "Mono" in the Active workbook,
    'which might not be the one you'd expect.
    Set monoWks = ThisWorkbook.Worksheets("Mono")
    Set monoRecursoWks = ThisWorkbook.Worksheets("Mono recurso")

    'Haven't tried to understand what you want to do, but notice the use of the references obtained above,
    'the Value2 property for good practice (without it, you're dealing with Range objects and relying on
    'VBA to use the default property of the Range class), and the correction from your original "wkb.wks"
    'to just "...Wks".

    lin_ori = 2
    lin_dest = 2
    Do While monoWks.Cells(lin_ori, 1).Value2 <> ""
        monoRecursoWks.Cells(lin_dest, 1).Value2 = monoWks.Cells(lin_ori, 1).Value2
        monoRecursoWks.Cells(lin_dest, 2).Value2 = monoWks.Cells(lin_ori, 2).Value2

        lin_ori = lin_ori + 1
        lin_dest = lin_dest + 1
    Loop

    lin_ori = 3
    lin_dest = 2
    Do While monoRecursoWks.Cells(lin_dest, 1).Value2 <> ""
        Do While tabelaSinteseWks.Cells(lin_ori, 2).Value2 <> "" 'Note: you had commented out this comparison.
            If monoRecursoWks.Cells(lin_dest, 1) = tabelaSinteseWks.Cells(lin_ori, 2) Then
                monoRecursoWks.Cells(lin_dest, 3) = tabelaSinteseWks.Cells(lin_ori, 6)
                monoRecursoWks.Cells(lin_dest, 4) = tabelaSinteseWks.Cells(lin_ori, 7)
                monoRecursoWks.Cells(lin_dest, 5) = tabelaSinteseWks.Cells(lin_ori, 8)
                monoRecursoWks.Cells(lin_dest, 6) = tabelaSinteseWks.Cells(lin_ori, 15)
                monoRecursoWks.Cells(lin_dest, 7) = tabelaSinteseWks.Cells(lin_ori, 16)
            End If

            lin_ori = lin_ori + 1
        Loop

        lin_dest = lin_dest + 1
    Loop

    'Release references to objects within the other workbook before closing it.
    Set tabelaSinteseWks = Nothing
    exemploWbk.Close SaveChanges:=False

    'Cleanup.
    Set monoRecursoWks = Nothing
    Set monoWks = Nothing
    Set exemploWbk = Nothing
End Sub

使用包含宏的工作簿中的工作表可以做很酷的事情:直接在VBA中使用 CodeName 。可以从Visual Basic编辑器中查看和修改工作表的CodeName。很酷的部分是,即使您从Excel更改工作表的名称,此名称也不会更改。

在项目浏览器(Ctrl + R)中,选择Mono节点下的工作表(例如Microsoft Excel Objects),然后按F4转到其“属性”窗口。更改(Name)属性。现在它可能是Sheet1或者其他东西。将其更改为monoWks。对Mono recurso工作表执行相同操作,并将其命名为monoRecursoWks

完成此操作后,您可以删除几行代码,因为您可以直接通过其CodeName引用这两个本地工作表。再次,奖励是,如果从Excel中看到的名称发生变化,您不必担心代码中断。

Sub Mono_recurso()
    Dim exemploWbk As Excel.Workbook
    Dim tabelaSinteseWks As Excel.Worksheet
    '... No need to declare variables to hold local worksheet references ...
    Dim lin_ori As Long
    Dim lin_dest As Long

    'Open the workbook.
    'Note: there are many options to the Open method; check them online.
    Set exemploWbk = Workbooks.Open("C:\Users\Feels Bad Man\Desktop\exemplo.xlsm")

    '... No need to get references to local worksheets by their tab names in Excel ...

    'Obtain a reference to the target worksheet.
    Set tabelaSinteseWks = exemploWbk.Worksheets("Tabela de síntese")

    '... Copy code goes here, unchanged ...

    'Release references to objects within the other workbook before closing it.
    Set tabelaSinteseWks = Nothing
    exemploWbk.Close SaveChanges:=False

    'Cleanup.
    Set exemploWbk = Nothing
End Sub