mercredi 31 mars 2021

How do I get VBA to exclude cells where the formula returns zero?

I am going to try and explain this as best I can:

  1. I am working with a file that has 4 sheets.

  2. From all of them, I want to copy and paste column A (from A:10) (which contains a concat formula) when some other rows are populated and then save into a csv. all rows from A10 onwards have the concat formula which is then filled in depending on the other columns (the same applies for the other sheets)

  3. I have it currently creating sheet1, and pasting into there, then saving as a csv.

  4. However, from the first sheet it looks at, it takes only the first line (but the second line - J11 (and so A11) are populated.

  5. In the other sheets, it is copy and pasting the 2 rows that are populated, but also all the other rows as there are formulas there that return zero. As I have the .End(xlDown) and technically all the other rows are populated

  6. I have tried doing an IF statement for the last sheet only as a test, and currently it only copies the first populated line, and not the second (but at least it also doesn't copy all the other cells with zero).

  7. Essentially, for each sheet I'd like it to loop through with for example E10 is populated, copy and paste A10 into Sheet1, etc, if E10 is not zero.

I am a VBA beginner so am quite stuck on this!

Any help is appreciated.

Sub Output_test1()
'
' Output_test1 Macro
'

'
    Sheets("Create").Select
    Range("A10", Range("J10").End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets.Add.Name = "Sheet1"
    Sheets("Sheet1").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Assign").Select
    Range("A10", Range("E10").End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A1").End(xlDown).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Date & Time").Select
    Range("A10", Range("E10").End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A1").End(xlDown).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Event Type").Select
    Dim rg As Range
    For Each rg In Range("E10").End(xlDown)
    If rg.Value > 0 Then

    End If
        Range("A10").Select
     Application.CutCopyMode = False
    Selection.Copy
             Sheets("Sheet1").Select
    Range("A1").End(xlDown).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Sheet1").Select
    Application.CutCopyMode = False
    Next
    

    
    Sheets("Sheet1").Move
    myTime = Format(Now, ("dd.mm.yy"))
    ChDir "C:\Users\"
    ActiveWorkbook.SaveAs Filename:= _
        "Recruit_" & myTime & ".csv", FileFormat:=xlCSVUTF8, _
        CreateBackup:=False
End Sub

Aucun commentaire:

Enregistrer un commentaire