arrayssortingconditional-statementscopypaste

conditional copy and paste line by line with vba


With the precious help of @FaneDuru, I could copy and paste cell contents (which can includes new lines with alt+enter) respectively and separately to another column in another sheet with the below code until i added bolded part into the code. My aim is to sort cell content if there is ‘NONE’ string in the C column. But i receive error 1004 at this line.

Sub Sheet2_Button_Click()
Dim ws As Worksheet, lastR As Long, arr, arrSpl, arrFin, i As Long, j As Long, k As 
Long

Set ws = ActiveSheet
lastR = ws.Range("A" & ws.rows.count).End(xlUp).row

arr = ws.Range("A1:A" & lastR).Value2
ReDim arrFin(1 To UBound(arr) * 15, 1 To 1)
For i = 1 To UBound(arr)
     If arr(i, 1) <> "" And **ws.Range(“C2:C”) = “NONE”** Then
         arrSpl = Split(arr(i, 1), vbLf)
         For j = 0 To UBound(arrSpl)
             k = k + 1
             arrFin(k, 1) = arrSpl(j)
         Next j
     End If
Next i
If k > 0 Then
 Worksheets(sheet1).Range("B:B").ClearContents
 Worksheets(sheet1).Range("B1").Resize(k, 1).Value2 = arrFin
End If
End Sub

Solution

  • Please, try the next adapted code. Do not forget to use your real destination sheet name:

    Sub splitAndCopyInAnotherColumn()
       Dim ws As Worksheet, wDest As Worksheet, lastR As Long, arr, arrSpl, arrFin, i As Long, j As Long, k As Long
       
       Set ws = ActiveSheet: Set wDest = Worksheets("your destination sheet name") 'use here the CORRECT existing destination sheet name
       lastR = ws.Range("A" & ws.rows.count).End(xlUp).row
       
       arr = ws.Range("A1:C" & lastR).Value2
       ReDim arrFin(1 To UBound(arr) * 15, 1 To 1)
       For i = 1 To UBound(arr)
            If arr(i, 1) <> "" And UCase(arr(i, 3)) = "NONE" Then
                arrSpl = Split(arr(i, 1), vbLf)
                For j = 0 To UBound(arrSpl)
                    k = k + 1
                    arrFin(k, 1) = arrSpl(j)
                Next j
            End If
       Next i
       If k > 0 Then
        wDest.Range("B:B").ClearContents
        wDest.Range("B1").Resize(k, 1).Value2 = arrFin
       End If
    End Sub
    

    Please, send some feedback after testing it.

    Edited:

    I cannot stay too much near my laptop. So, since you do not clarify the sheets/column issue, I prepared another version processing column D:D if in C:C "NONE" exists and returning to the other sheet in "A1":

    Sub splitAndCopyInAnotherColumnReversedSheets()
       Dim ws As Worksheet, wDest As Worksheet, lastR As Long, arr, arrSpl
       Dim i As Long, j As Long, dict As Object
       Const searchStr As String = "NONE" 'you can use here any string you need
       
       Set ws = Worksheets("Sayfa2"): Set wDest = Worksheets("Sayfa1")  'use here the CORRECT existing sheet
       lastR = ws.Range("D" & ws.Rows.Count).End(xlUp).Row 'last row in column D:D
       
       Set dict = CreateObject("Scripting.Dictionary") 'set the dictionary (able to keep only unique keys)
       arr = ws.Range("C1:D" & lastR).Value2
       ReDim arrFin(1 To UBound(arr) * 15, 1 To 1)
       For i = 1 To UBound(arr)
            If arr(i, 2) <> "" And UCase(arr(i, 1)) = searchStr Then
                arrSpl = Split(arr(i, 2), vbLf)
                For j = 0 To UBound(arrSpl)
                    dict(arrSpl(j)) = "" 'create dictionary keys only for unique strings!
                Next j
            End If
       Next i
       If dict.Count > 0 Then
            wDest.Range("A:A").ClearContents 'clear all values in A:A
            wDest.Range("A1").Resize(dict.Count, 1).Value2 = Application.Transpose(dict.keys)
            wDest.Activate
       Else
          MsgBox "No any value in D:D containing """ & searchStr & """ in C:C exists..." 'message in case of no returned value
       End If
        
       MsgBox "Ready..."
    End Sub