简体   繁体   English

参考 Excel VBA 中的新工作簿

[英]Referring to a new workbook in Excel VBA

In cell K2 in first workbook is written today's date which is the reference for the name of other workbook.在第一个工作簿的单元格 K2 中写入今天的日期,这是其他工作簿名称的参考。 I need to take some information from a second open workbook whose file name is today's date ("13.06.2021.xlsx").我需要从文件名为今天日期(“13.06.2021.xlsx”)的第二个打开的工作簿中获取一些信息。

I created variable second_workbook which is the date.我创建了变量 second_workbook,它是日期。 Then I created variable called "cellscopy" (active cell from first workbook and to copy 3 more cells to the right of it).然后我创建了名为“cellscopy”的变量(第一个工作簿中的活动单元格并在其右侧复制了另外 3 个单元格)。 Then the macro pastes a value in cell I2 in the first workbook (there's a formula in J2 rearranging the account number) and then J2 is the criteria for filter from a third workbook called "Bank accounts.xlsx".然后宏在第一个工作簿的单元格 I2 中粘贴一个值(J2 中有一个重新排列帐号的公式),然后 J2 是从名为“Bank accounts.xlsx”的第三个工作簿中筛选的条件。

My macro then finds the value from first workbook cell J2 ("criteria") from "Bank accounts.xlsx" in columns I:I and copies a value 5 columns leftward from that cell - a bank acc number corresponding to that batch number.然后,我的宏从 I:I 列中的“Bank accounts.xlsx”中的第一个工作簿单元格 J2(“标准”)中找到值,并从该单元格向左复制一个值 5 列 - 对应于该批号的银行账户编号。

I created a variable "accnumber" which is then pasted in a filter in a table in the second workbook ("13.06.2021.xlsx").我创建了一个变量“accnumber”,然后将其粘贴到第二个工作簿(“13.06.2021.xlsx”)的表中的过滤器中。 Then the filtered range from the table is copied and pasted in a new workbook (NewWb) in cell A12.然后,将表格中的过滤范围复制并粘贴到单元格 A12 的新工作簿 (NewWb) 中。 Then I need to go back to the first workbook and copy the "cellscopy" range and paste it again in the new workbook which was created at cell C7.然后我需要 go 回到第一个工作簿并复制“cellscopy”范围并将其再次粘贴到在单元格 C7 处创建的新工作簿中。

However, I get a run-time error 438 Object doesn't support this property or method highlighting the last line of my VBA code.但是,我收到运行时错误 438 Object 不支持此属性或方法突出显示我的 VBA 代码的最后一行。

Can you please help me with this issue?你能帮我解决这个问题吗? I hope I could explain you as clear as possible my problem.我希望我能尽可能清楚地向您解释我的问题。

    second_workbook = Range("K2").Value
    Dim wb As Workbook
    Dim actWb As Workbook, newWb As Workbook, shAct As Worksheet, shNew As Worksheet
    Dim cellscopy As Range
    Set cellscopy = Range(ActiveCell, ActiveCell.Offset(0, 3))
    Set actWb = ActiveWorkbook
    Set shAct = actWb.Sheets(1)
    Set newWb = Workbooks.Add
    Set shNew = newWb.Sheets(1)
    Set wb = Workbooks(Format(second_workbook, "dd.mm.yyyy") & ".xlsx")
    Dim batchnumber As Range
    Selection.Copy
    Range("I2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Criteria = Range("J2").Value
    Windows("Bank Accounts.xlsx").Activate
    Set batchnumber = Range("I:I").Find(Criteria & "TT")
    If Not batchnumber Is Nothing Then
        batchnumber.Select
    End If
    ActiveCell.Offset(0, -5).Range("A1").Select
    accnumber = ActiveCell
    wb.Activate
    ActiveSheet.Range("$A$1:$G$654").AutoFilter Field:=5, Criteria1:=accnumber
    Range("C1").Activate
    Selection.CurrentRegion.Select
    Application.CutCopyMode = False
    Selection.Copy
    newWb.Activate
    Range("A12").Select
    ActiveSheet.Paste
    shAct.Range(cellscopy).Copy Destination:=newWb.Range("C7:F7")

I am getting error 438 at the last line.我在最后一行收到错误 438。

I hope I explained as clear as possible my issue.我希望我尽可能清楚地解释我的问题。 If you could help me I would appreciate it very much如果你能帮助我,我将不胜感激

When creating a new workbook, set it as a variable when doing so.创建新工作簿时,将其设置为变量。
This way it's easy to refer to it.这样就很容易引用了。

Dim wb As Workbook
Set wb = Workbooks.Add

I'm also obliged to link to the how to avoid using select post.我还必须链接到如何避免使用 select帖子。

edit编辑
Now that you completely changed the question, the rest of this doesn't make much sense.既然您完全改变了问题,那么这个 rest 就没有多大意义了。

Please, try the next code.请尝试下一个代码。 You need to understand that you cannot paste IN A WORKBOOK .您需要了解您不能粘贴 IN A WORKBOOK You should paste in a sheet range :您应该粘贴在工作表范围内

 Sub testCopyFilterCopy()
  Dim shAct As Worksheet, wb2 As Workbook, sh2 As Worksheet, wb3 As Workbook, sh3 As Worksheet
  Dim value_for_filter As String, actCell As Range, rngFilt As Range, rngF As Range
  
  Set shAct = ActiveSheet
  Set actCell = ActiveCell
  value_for_filter = actCell.value
  
  Set wb2 = Windows(Format(Date, "dd.mm.yyyy") & ".xlsx")
   Set sh2 = wb2.Worksheets("My sheet") 'Plese use here the appropriate sheet name!!!
   Set rngFilt = sh2.Range("$A$1:$G$654")
   rngFilt.AutoFilter field:=5, Criteria1:=value_for_filter

 On Error Resume Next
   'set a range of the filtered cells only:
   Set rngF = rngFilt.SpecialCells(xlCellTypeVisible)
 On Error GoTo 0
 If Not rngF Is Nothing Then
    Set wb3 = Workbooks.Add
     Set sh3 = wb3.Worksheets(1)
     rngF.Copy Destination:=sh3.Range("A12")
     shAct.Range(actCell, actCell.Offset(0, 3)).Copy Destination:=sh3.Range("C10")
  Else
    MsgBox "No visible cells in the range..."
  End If
 End Sub
  1. You can paste only in a sheet, not in a workbook只能粘贴到工作表中,不能粘贴到工作簿中

  2. If you want to copy the filtered range , you need to use VisibleCells .如果要复制过滤范围,则需要使用VisibleCells Otherwise, all the range will be pasted, not only the filtered one.否则,将粘贴所有范围,而不仅仅是过滤的范围。

  3. You should put Option Explicit on top of your module, in order to be obliged to declare all variables .您应该将Option Explicit放在模块顶部,以便有义务声明所有变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM