Excel 当宏位于当前工作簿上时,请使用其他工作簿

Excel 当宏位于当前工作簿上时,请使用其他工作簿,excel,vba,Excel,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 B

我正在尝试编写一个宏,该宏将添加当前工作簿的某些列,并参考其他工作簿。我是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

我的假设是,您希望从代码中打开另一个工作簿,将它的一些值转移到包含宏的工作簿中,然后可能关闭另一个工作簿

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

'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

那一行应该是
setwkb=Excel.Workbooks(“exemplo.xlsm”)
。因为Excel不允许以相同的文件名打开两个工作簿,它会知道您所指的是从
C:\Users\Feels Bad Man\Desktop
打开的工作簿。好吧,我试过了,但还是停在同一行。。。两本作业本都可以使用吗?我指的是我正在工作的那一个,另一个我想加载以执行循环。因此,我不必将两个excel文件合并为一个始终在问题中提供错误号和确切消息。哇,它现在正在运行,但它没有做我希望它做的事情。。。您使用了“value2”,如果我的单元格中有字符串,这是否有效?我想做的是找出电子表格(Mono)中是否有与电子表格(Tabela de síntese)中Example.xmlx相同的字符串。如果有,它会将该表格中列的一些数据复制到我的原始数据中。我希望它以这种方式工作,所以我没有一个25Mb的excel文件。我很高兴您提出了这个解决方案和代码。我对excel vba的所有了解都是我在上个月学到的,因此我不知道如何正确编码yetYes,Value2可以处理存储在单元格中的任何内容。非常感谢,它工作正常。通过这个代码示例,我学到了很多:)
'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
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