0

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

MilanovI
  • 27
  • 8
  • Won't `Range("C1").Activate Selection.CurrentRegion.Select` just select all headers? Seems strange to copy those after filtering. – Christofer Weber Jun 13 '21 at 09:53
  • I am afraid that your question is not clearer than the previous one and I try to deduce what you need... So, do you want to use the value of the active cell in **a second workbook** to filter a range based on it and then copying the filtered range and pasting in a newly created workbook? In range "A12". Then copying the range (active cell plus other 3 to the right) "B2:E2" in the new workbook in range "C10:F10". Should be this understanding correct? If not, where my understanding/deduction does not match your need? – FaneDuru Jun 13 '21 at 10:03
  • @FaneDuru Could you please check again my full code? Now I pasted all my codes. Thank you for your time. – MilanovI Jun 13 '21 at 10:19
  • @ChristoferWeber no, it will select the headers plus all the filtered range which I need. – MilanovI Jun 13 '21 at 10:20
  • Since you only like asking questions and **not answering our questions**, it will be difficult to be helped. **Is it o difficult to answer my question from the previous comment**? Did I correctly deduce what you need? Do you have a problem with English and try always showing different codes, which do not say too much about your need? – FaneDuru Jun 13 '21 at 10:25
  • `shAct.Range(cellscopy).Copy Destination:=newWb.woksheets(1).Range("C7:F7")` ? – Christofer Weber Jun 13 '21 at 10:47
  • @FaneDuru the accnumber (the active cell from the Bank accounts.xlsx workbook) is the filter for the 13.06.2021.xlsx table. Then that range must be pasted in cell A12, then yes return to first workbook and copy B2:E2 and paste in the new workbook in range C10:F10 – MilanovI Jun 13 '21 at 10:49
  • However, the exact address of the cells are not that important, whether is C7 or C10 I just need the code for copying from "13.06.2021.xlsx" workbook and paste into new, then go back the first workbook copy range active cell:activecell.offset(0,3) and return again to the new workbook and paste there. Thank you – MilanovI Jun 13 '21 at 10:51

2 Answers2

3

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.

Christofer Weber
  • 1,464
  • 1
  • 9
  • 18
2

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.

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • Thank you. The second workbook is called (13.06.2021.xlsx). Where should I put the formatting of the date for the name of my second workbook just as in my code above in the question. – MilanovI Jun 13 '21 at 10:48
  • @MilanovI: But why don't using exactly the workbook name as it is? I mean `Set wb2 = Windows("13.06.2021.xlsx")`... I meant, to test the code! What do you mean by 'second_workbook' in this context? Why do you all the time come back with issues not mentioned in your initial question? – FaneDuru Jun 13 '21 at 11:00
  • @MilanovI: But, please test the code as it is and confirm, or infirm that it works (or not) as it should! I will show you how to do it if you did not understand my above comment. But spreading your attention in 1000 directions you will not learn anything, I am afraid and make us loosing the enthusiasm of helping you... – FaneDuru Jun 13 '21 at 11:08
  • Because it must be dynamic. The reference cell has =today() formula and there are new excel files everyday named with the today's date. – MilanovI Jun 13 '21 at 11:12
  • @MilanovI: Did you look to my above comment? Did you ask about that in your initial question? Do you think that we here should assist you all the year in order to finish a project? Do you understand what a question means and how we can help you in solving it? Did you test the code as it is? If yes, does it work? If not, why not tested it? – FaneDuru Jun 13 '21 at 11:14
  • @MilanovI: Like I told you in my previous cases, I do not need notoriety, anymore! I like helping people, but in the community way... In order to become a programmer, you must firstly learn how to clearly ask a question! Then, you should stick on the question and if something new is necessary, please **specify that it is a new thing** and ask. If it is something more complex, please ask a new question, starting from the existing code, clearly explaining what you try doing and what your code does against what you need... – FaneDuru Jun 13 '21 at 11:28
  • @MilanovI: Please, test the updated code, able to use the dynamically named workbook (using the current date) and confirm that it works as you need. – FaneDuru Jun 13 '21 at 11:34
  • The Excel file bank accounts.xlsx is missing tho – MilanovI Jun 14 '21 at 07:25
  • My issue is little bit confusing but let me summarize again all my actions: I select active cell in "workbook_1" and start the macro. Macro copies that cell, pastes it in cell I2 in same Wb, takes value from cell J2 and opens Wb ("Bank accounts.xlsx") and searches in column I:I for the value from J2 + "DD" . Then on the same row where it finds the value it copies another value = "accnumber" on column D, opens Wb ("dd.mm.yyyy.xlsx") - name is different everyday, pastes in filter, copies the filtered range, opens new Wb, pastes in A12, returns to "Workbook_1",continues on next comment – MilanovI Jun 14 '21 at 07:30
  • (continued)...returns to "Workbook_1.xlsx", copies the initially active cell (at the moment of starting the macro, the very first cell) plus 3 cells to the right on the same row, returns to the new workbook which was created when the filtered range has been pasted, and pastes that info in row C7. That's all I need. – MilanovI Jun 14 '21 at 07:32
  • @MilanovI: I am afraid I cannot keep the step with your never ending question... Should I understand that now there are four workbooks involved? The active one, this one mentioned now, the one named as current day and the new created one. Is this understanding correct? If yes, this completely other question and you should post it separately. If you will tag me, when I will be available I will try helping. If not me, somebody else will solve your problem. If I misunderstood your request, please better clarify what is it about. – FaneDuru Jun 14 '21 at 07:37
  • Yes, in the end 4 workbooks are involved. Sorry for confusion but I also had hard times trying to cope with those codes and how to convey in the clearest way possible the information to you. But those 2 last comments from me summarize best what the actions of the macro should be. – MilanovI Jun 14 '21 at 07:40
  • @MilanovI: What I was trying to say in my previous comments was the fact that our purpose here is to help people solving their code, according to what explain that is necessary and also offer a solution to the others searching for a similar issue. When you edit your question six times the answers does not match at all with your initial question, which is not seen anymore by people searching for something similar. If i will place an answer to solve you last request, somebody seeing it will thing that I hit my head recently... Why don't you respect the community rules placing another question? – FaneDuru Jun 14 '21 at 08:01
  • @MilanovI: Like I told you, even if you are a programmer, in order to be a good programmer you must know how to perfectly structure **what is to be done**. So, at least you will learn doing that... – FaneDuru Jun 14 '21 at 08:02
  • https://stackoverflow.com/questions/67966760/how-to-copy-active-cells-and-paste-them-in-an-already-opened-new-workbook I created the new question @FaneDuru Thanks if you answer it. – MilanovI Jun 14 '21 at 08:05
  • 1
    Not only helpful, but would deserve a price for tireless patience :+) – T.M. Jun 14 '21 at 09:41
  • @T.M. Thanks! What is strange/interesting is that after your comment I received 5 notoriety points for this thread... Do you have the knowledge that an algorithm interpreting such a comment and automatically allocating points, does exist? – FaneDuru Jun 14 '21 at 10:03
  • @FaneDuru Sorry, simply don't know. Maybe on Meta? – T.M. Jun 14 '21 at 10:18
  • @T.M. Maybe... It is not bad to know (and test) and use it for cases when a simple vote up should not be considered enough... :) – FaneDuru Jun 14 '21 at 11:24
  • @FaneDuru Maybe helpful (though referring to external cause)? [Massive downvoting](https://meta.stackoverflow.com/questions/318863/what-to-do-when-massive-downvoting-hits-you-due-to-an-external-source) – T.M. Jun 14 '21 at 15:41