简体   繁体   中英

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. I need to take some information from a second open workbook whose file name is today's date ("13.06.2021.xlsx").

I created variable second_workbook which is the date. Then I created variable called "cellscopy" (active cell from first workbook and to copy 3 more cells to the right of it). 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".

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 created a variable "accnumber" which is then pasted in a filter in a table in the second workbook ("13.06.2021.xlsx"). Then the filtered range from the table is copied and pasted in a new workbook (NewWb) in cell A12. 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.

However, I get a run-time error 438 Object doesn't support this property or method highlighting the last line of my VBA code.

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.

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.

edit
Now that you completely changed the question, the rest of this doesn't make much sense.

Please, try the next code. You need to understand that you cannot paste 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 . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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