Results 1 to 10 of 294

Thread: Appendix Thread. ( Codes for other Threads, ( Avinash ).)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #36
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Macro solution for these posts

    https://excelfox.com/forum/showthrea...between-sheets
    https://www.excelforum.com/excel-pro...en-sheets.html
    http://www.eileenslounge.com/viewtop...bf154f#p271799


    Code:
    Sub MoveSomeDataRowsToNewWorksheetBasedOnConditions()
    Rem 1 worksheets data info
    Dim Wb1 As Workbook
     Set Wb1 = Workbooks("1.xls")
    Dim Ws1 As Worksheet
     Set Ws1 = Wb1.Worksheets.Item(1)
    Dim Lr1 As Long: Let Lr1 = Ws1.Range("A" & Ws1.Rows.Count & "").End(xlUp).Row    '    Make Lr Dynamic : https://excelfox.com/forum/showthread.php/2364-Delete-rows-based-on-match-criteria-in-two-excel-files?p=11467&viewfull=1#post11467  https://excelfox.com/forum/showthread.php/2364-Delete-rows-based-on-match-criteria-in-two-excel-files?p=14565&viewfull=1#post14565
    Dim arr1DEF() As Variant
     Let arr1DEF() = Ws1.Range("D1:F" & Lr1 & "").Value2
    Rem 2 Get the row numbers wanted in the New worksheet and in the first worksheet after
    '2a(i) Build the string indicies based on the criterias
    Dim str1 As String, str2 As String '  strings to build for  Row numbers  for the two sheets after
     Let str1 = "1": Let str2 = "1"    '  Both Worksheets should have the headers
    Dim Cnt
        For Cnt = 2 To Lr1 Step 1
            If arr1DEF(Cnt, 1) = arr1DEF(Cnt, 2) Or arr1DEF(Cnt, 1) = arr1DEF(Cnt, 3) Then '
             ' Do nothing .. For this macro I want to add here the rows which will still be there in the original worksheet  After
             Let str1 = str1 & " " & Cnt
            Else
            '   ..........."...put that data into new worksheet by creating a new sheet in it & remove that data from current sheet........"
             Let str2 = str2 & " " & Cnt ' this will be used for the new worksheet  It is not being used for the first Worksheet after. So that will mean that these rows do not appear in the first worksheet after  ClearContentsing it
            End If
        Next Cnt
    '2a(ii)
    Dim Rws1() As String, Rws2() As String
     Let Rws1() = Split(str1, " ", -1, vbBinaryCompare): Let Rws2() = Split(str2, " ", -1, vbBinaryCompare)
    '2b) Make the "virtical" row indicie array needed in the  "Magic code line"
    Dim RwsV1() As String: ReDim RwsV1(1 To UBound(Rws1()) + 1, 1 To 1): Dim RwsV2() As String: ReDim RwsV2(1 To UBound(Rws2()) + 1, 1 To 1)
        For Cnt = 1 To UBound(Rws1()) + 1  '  +1 is needed because the array returned by  Split  is a 1D array starting at 0
         Let RwsV1(Cnt, 1) = Rws1(Cnt - 1)
        Next Cnt
        For Cnt = 1 To UBound(Rws2()) + 1  '  +1 is needed because the array returned by  Split  is a 1D array starting at 0
         Let RwsV2(Cnt, 1) = Rws2(Cnt - 1)
        Next Cnt
    Rem 3 Output
    Dim Clms() As Variant: Let Clms() = Evaluate("=COLUMN(A:K)") ' ** CHANGE TO SUIT ** This is currently for columns A B C ...K  1 2 3..... 11  For non consequtive columns you can use like  Array("1", "3", "26")  - that last example gives in the new range just columns A C Z from the original worksheet
    '3a new Worksheet
     Worksheets.Add After:=Worksheets.Item(1)
     Let ActiveSheet.Name = "New Worksheet"
    Dim arrOut() As Variant: Let arrOut() = Application.Index(Ws1.Cells, RwsV2(), Clms()) ' ' The magic code line ---     '  "Magic code line"            http://www.eileenslounge.com/viewtopic.php?p=265384#p265384    http://www.eileenslounge.com/viewtopic.php?p=265384&sid=39b6d764de41f462fe66f62816e5d789#p265384          See also https://www.excelforum.com/excel-new-users-basics/1099995-application-index-with-look-up-rows-and-columns-arguments-as-vba-arrays.html#post4571172  , http://www.excelfox.com/forum/showthread.php/2145-Excel-VBA-Interception-and-Implicit-Intersection-and-VLookUp   for full explanation
     Let Worksheets("New Worksheet").Range("A1").Resize(UBound(arrOut(), 1), 11).Value2 = arrOut() ' We can paste out an array of values in one go to a spreadsheet range. Here A1 is taken as top right which is then resized to the size of the field of data values in arrOut()
    '3b) Original worksheet after
     Let arrOut() = Application.Index(Ws1.Cells, RwsV1(), Clms()) ' ' The magic code line ---     '  "Magic code line"            http://www.eileenslounge.com/viewtopic.php?p=265384#p265384    http://www.eileenslounge.com/viewtopic.php?p=265384&sid=39b6d764de41f462fe66f62816e5d789#p265384          See also https://www.excelforum.com/excel-new-users-basics/1099995-application-index-with-look-up-rows-and-columns-arguments-as-vba-arrays.html#post4571172  , http://www.excelfox.com/forum/showthread.php/2145-Excel-VBA-Interception-and-Implicit-Intersection-and-VLookUp   for full explanation
    'Ws1.UsedRange.ClearContents
    Ws1.Range("A1:K" & Lr1 & "").ClearContents
     Let Ws1.Range("A1").Resize(UBound(arrOut(), 1), 11).Value2 = arrOut()
    End Sub
    Last edited by DocAElstein; 07-27-2020 at 12:10 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. Replies: 192
    Last Post: 08-30-2025, 01:34 AM
  2. Tests and Notes for EMail Threads
    By DocAElstein in forum Test Area
    Replies: 29
    Last Post: 11-15-2022, 04:39 PM
  3. Replies: 379
    Last Post: 11-13-2020, 07:44 PM
  4. Appendix Thread. Diet Protokol Coding Adaptions
    By DocAElstein in forum Test Area
    Replies: 6
    Last Post: 09-05-2019, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •