1

I'm really bad at VBA (kind of understand when reading the code, but I cannot write it myself). I would like to create a report file in workbook y that copies part of the data present in workbook x.

I based my current code on the two following articles: Copy from one workbook and paste into another

and Copy value & offset from workbook X to workbook Y only when range value > 0, then loop for remaining rows

I get no error messages with my code, but it doesn't do anything (except open the files). I double-checked the values and the Like, and they seem ok to me. Any Idea why it doesn't work?

Changed my names & path for anonymity, but here it is:

Private Sub CommandButton1_Click()
Dim x As Workbook
Dim y As Workbook
Dim i, LastRow

'## Open both workbooks first:
Set x = Workbooks.Open("C:\filedestination\filex.xlsx")
Set y = Workbooks.Open("C:\filedestination\filey.xlsm")

'Now, copy what I want from x to y:
LastRow = x.Sheets("Sheetname1").Range("A" & Rows.Count).End(xlUp).Row
y.Sheets("Sheetname2").Range("A2:N5000").ClearContents
For i = 2 To LastRow
If x.Sheets("Sheetname1").Cells(i, "D").Value = "Fixedtext" And x.Sheets("Sheetname1").Cells(i, "F").Value Like "*Subject ?ompensation" Then
x.Sheets("Sheetname1").Cells(i, "D").EntireRow.Copy Destination:=y.Sheets("Sheetname2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i

'Close x:
x.Close

End Sub

Thanks

Community
  • 1
  • 1
nootaku
  • 61
  • 9
  • Also check the value of `LastRow` before the loop starts. If it's bigger than 1, add `Debug.? x.Sheets("Sheetname1").Cells(i, "D").Value = "Fixedtext", x.Sheets("Sheetname1").Cells(i, "F").Value Like "*Subject ?ompensation"` before the `If`. – GSerg Apr 28 '16 at 10:21
  • I tried Vityata's code (the debug) Same result ... – nootaku Apr 28 '16 at 10:31
  • What was printed in the the debug window? – GSerg Apr 28 '16 at 10:35
  • nothing. i can send screenshot if you want – nootaku Apr 28 '16 at 10:57

1 Answers1

0

Two things can be wrong:

  1. LastRow can be 1, thus it does not enter the loop.
  2. Possibly it does not enter in the if clause.

Use the following code to find out which is wrong:

    Private Sub CommandButton1_Click()
    Dim x As Workbook
    Dim y As Workbook
    Dim i As Long, LastRow as Long

    '## Open both workbooks first:
    Set x = Workbooks.Open("C:\filedestination\filex.xlsx")
    Set y = Workbooks.Open("C:\filedestination\filey.xlsm")

    'Now, copy what I want from x to y:
    LastRow = x.Sheets("Sheetname1").Range("A" & Rows.Count).End(xlUp).Row
    debug.print LastRow

    y.Sheets("Sheetname2").Range("A2:N5000").ClearContents
    For i = 2 To LastRow
    If x.Sheets("Sheetname1").Cells(i, "D").Value = "Fixedtext" And x.Sheets("Sheetname1").Cells(i, "F").Value Like "*Subject ?ompensation" Then
    Debug.Print "Entered in the if"
    x.Sheets("Sheetname1").Cells(i, "D").EntireRow.Copy Destination:=y.Sheets("Sheetname2").Range("A" & Rows.Count).End(xlUp).Offset(1)
    else
       Debug.Print "Did not enter for " ;i    
    End If
    Next i

    'Close x:
    x.Close

    End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • 1
    Of course you do. This code prints diagnostic messages, not magically fixes your output. – GSerg Apr 28 '16 at 10:37
  • ;-) i know, that's what i meant. I got no msg prints, so i suppose that there is no enormous error – nootaku Apr 28 '16 at 10:39
  • You could not possibly get no prints @nootaku. At least `debug.print LastRow` always executes. Did you actually look in the debug window (Ctrl+G)? – GSerg Apr 28 '16 at 10:46
  • @GSerg Ofc I did, it remains blank, thought it might be a problem with LastRow as mentionned hereabove. searching for solutions atm – nootaku Apr 28 '16 at 10:56
  • @nootaku, then start executing the code with F8 line by line. E.g.Select the first line and press F8. Then keep on pressing it until you go to the last line. What happens? – Vityata Apr 28 '16 at 11:35
  • 1
    @nootaku define LastRow as integer (or long): Dim i, LastRow As Integer. Then the output at least will be 0 instead of "". – Marco Vos Apr 28 '16 at 11:52
  • @Vityata , The dim part works fine, the set part seem to work as well. When i arrive at "Set y = Workbooks.Open("C:\filedestination\filey.xlsm")" all my selection goes blank. If i press f8 again, i'm back at line 1 – nootaku Apr 28 '16 at 12:10
  • Got it i think ... Really stupid, though: since i ask to open workbookY, the process resets. I launch macro from workbook Y. Can I select it by writing Set y = ThisWorkbook ??? – nootaku Apr 28 '16 at 12:36
  • OK, got debug now - sorry for this really stupid mistake. The error seems to be originating with debug "Did not enter for " ;i " – nootaku Apr 28 '16 at 12:46