Transfer Specific Columns From Source Sheet To Two Sheets

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Hello everyone
I managed find this code for Mr Doc.AElstein it is the perfect code and is running well exactly as I need.
This code to Transfer specific columns from one sheet into two sheets Depending on a specific condition in column K .... At the following links
https://eileenslounge.com/viewtopic.php ... 77#p245177

https://www.excelforum.com/excel-progra ... heets.html

Code: Select all

Sub VBAArrayTypeAlternativeToFilter()
' Range Info
Dim arrK() As Variant: Let arrK() = Worksheets("Source").Range("K1:K" & Worksheets("Source").Range("A" & Rows.Count & "").End(xlUp).Row & "").Value
' Indicies needed  for output arrays
Dim strSuc As String, strSpit As String ' For like "1 3 5 .......42"
Let strSuc = "7": Let strSpit = "7" ' Header row 1
Dim cnt As Long
    For cnt = 8 To UBound(arrK(), 1)
        If arrK(cnt, 1) = "Y" Then
         Let strSuc = strSuc & " " & cnt
        Else
         Let strSpit = strSpit & " " & cnt
        End If
    Next cnt
'Output Suc
Dim clms() As Variant: Let clms() = Array(1, 2, 3, 4, 5, 6, 13) ' column A to column F & M
Dim strRws() As String: Let strRws() = Split(strSuc, " ", -1, vbBinaryCompare)
Dim Rws() As String: ReDim Rws(1 To UBound(strRws(), 1) + 1, 1 To 1)
    For cnt = 1 To UBound(strRws(), 1) + 1
     Let Rws(cnt, 1) = strRws(cnt - 1)
    Next cnt
Dim arrOut() As Variant: Let arrOut() = Application.Index(Worksheets("Source").Cells, Rws(), clms())
Let Worksheets("successful").Range("A1").Resize(UBound(arrOut(), 1), UBound(arrOut(), 2)).Value = arrOut()
'Output UnSuc (Spit)
Let clms() = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13) ' column A to column J & M
Let strRws() = Split(strSpit, " ", -1, vbBinaryCompare)
ReDim Rws(1 To UBound(strRws(), 1) + 1, 1 To 1)
    For cnt = 1 To UBound(strRws(), 1) + 1
     Let Rws(cnt, 1) = strRws(cnt - 1)
    Next cnt
Let arrOut() = Application.Index(Worksheets("Source").Cells, Rws(), clms())
Let Worksheets("Unsuccessful").Range("A1").Resize(UBound(arrOut(), 1), UBound(arrOut(), 2)).Value = arrOut()
End Sub
But the only problem is that the number of rows may increase or decrease in the Source sheet
so the old data must be cleard first before transferring the new data in Two worksheets ( successful & Unsuccessful )
Is there any additional lines should be add to achieve that? Thank you all.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

Add the following two lines at the beginning of the macro, directly below the line Sub ...:

Code: Select all

Worksheets("successful").Cells.Clear
Worksheets("Unsuccessful").Cells.Clear
Best wishes,
Hans

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Thanks a lot Mr. Hans for the addition ... What if I want to Transfer columns M & N With the division of values into two cells
Please see expected output sheet .... Many thanks in advance for your help.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

Doc.AELstein may have a better method, but see the attached version.

division of values into two cells.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Thanks a lot Hans for helping me in solving this issue
I tried to change the direction of the worksheets from right to left As in the expected output sheet attached above
But I'm having a problem with that ... Please see the image below
Another point please ... I need to add a condition
if columns cells ( M & N together ) contain empty cells This rows must be deleted in both worksheets Successful & Unsuccessful
Thank you so much for your time. Really appreciate that.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

New version:

division of values into two cells.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Thank you very much Mr. Hans ..... I am so sorry I may have not explained you properly
I mean to Transfer columns M & N With the division of values into two cells from right to left
the integer numbers in column E & G .... the decimal places in column D & F .... Please see the page layout in the image above
As for adding the condition, it works great ... Thank you in advance for your effort

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Dear Hans
I've tried a number of things and This is what I've come up with so far
but I am having major difficulty figuring out the correct syntax to Transfer columns M & N With the division of values into two cells as shown above
how can get this working correctly? Thank you for taking time and helping me to find a solution.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

You didn't use the code from my previous reply...
Best wishes,
Hans

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

jakjo wrote:
17 Jan 2023, 00:46
Thank you very much Mr. Hans ..... I am so sorry I may have not explained you properly
I mean to Transfer columns M & N With the division of values into two cells from right to left
the integer numbers in column E & G .... the decimal places in column D & F .... Please see the page layout in the image above
As for adding the condition, it works great ... Thank you in advance for your effort
There is a problem that I explained in posts 5 and 7
I have spent too much time in front of the code so that I can solution and This is what I've come up with so far
I tried to complete the solution but could not do that ... I'm sorry for taking you so much time ... Thanks again.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

I solved the problem of splitting the numbers into columns D/E and F/G, even in a right-to-left environment. If you don't want to use that, it's OK, but then I cannot help you.
Best wishes,
Hans

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

I am so sorry for disturbing you again Mr. Hans
The problem is that the values are inverted in a right-to-left environment.
see screen shot below to see what I mean
Sorry I didn't know a better way to describe this problem.... accept my apologies and I very much appreciate your further contributions to this topic.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by HansV »

I know. That is why I added code in the attachment higher up in this thread to circumvent this problem. But you didn't use it in your version.
Best wishes,
Hans

User avatar
DocAElstein
4StarLounger
Posts: 584
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by DocAElstein »

Hello,
HansV wrote:
16 Jan 2023, 11:08
Doc.AElstein may have a better method..
The respected honourable Mr Doc.AElstein still can’t log in very easily to his old account, so maybe I can fill in for him :)
_._____________________________________


I have no idea if the alternative method I give here is any better or worse. So I just give it as an alternative, that's all. It’s the nearest I can think of as a solution in a similar vain to my (Doc .AElstein’s) original:
_1) First, the extra 2 columns required in the new requirement
Transfer columns M & N With the division of values ,.. whole and decimal .. into two cells ...
they are got initially from just doubling/duplicating the column number for M & N in the column idicle array,
So Clms() = Array(6, 5, 4, 13, 14) is changed to clms() = Array(6, 5, 4, 13, 13,14, 14)
_2) Then the duplicated columns need to be fiddled a bit so that for each pair
_ b)one displays the integer
and
_a) the other the decimal, ( as a whole number ) .
_2a) For the decimal… The first thing I Google’d suggested something like
celllvalue – TRUNC(cellvalue)
, for a spreadsheet formula solution. Modifying that a bit for the data given to get it shown as a whole number would be a formula in a cell something like
=(D19-TRUNC(D19))*100
That lent itself nicely to an Evaluate Range one liner type solution in VBA.
Something like
Evaluate("=(D19:D20-TRUNC(D19:D20))*100")
_2b) For the whole part… In a spreadsheet we would do =INT(E19) , so this lends itself nicely again to an Evaluate Range one liner
Something like
Evaluate("=INT(E19:E20)")



The version I give is a fairly simple form to demonstrate the method alternative. I can’t help you with the various formatting and left – right, page breaks, right left and centre stuff being considered, because I have little knowledge of all that. I expect you can figure that out from what Hans has been doing for you already, or what you seem to have done yourself in your latest attempt . (You should note however, that should there be any performance advantages from my method, then these are wasted once you start doing a lot of formatting, since all those spreadsheet interaction things tend to slow that down).

The demo file I upload is a shortened version of your last file, and the code example is a simplified stripped down version. It’s only intended to demonstrate my method alternative for the new requirement … Transfer columns M & N with the division of values ,.. whole and decimal fraction .. into two cells

Code: Select all

 Option Explicit
Sub TransferSomeClmsSourceShtTo2ShtsAndSptSomeRstClmsIntoIntAndDec()   '   https://eileenslounge.com/viewtopic.php?p=303374#p303374
' Worksheets and source datas Info
Dim wS As Worksheet, wM As Worksheet, wA As Worksheet, Em As Long
 Set wS = Worksheets("Source"): Set wM = Worksheets("Suksesful"): Set wA = Worksheets("UnSuksesful")
 Let Em = wS.Range("A" & Rows.Count & "").End(xlUp).Row
Dim arrK() As Variant: Let arrK() = wS.Range("K1:L" & Em).Value
'
Dim strY As String, strAnuver As String
Dim Clms() As Variant, strRws() As String, Rws() As String
Dim arrOut() As Variant
Dim Cnt As Long
 Let strY = "7"
 Let strAnuver = "7"
    For Cnt = 8 To Em
        If arrK(Cnt, 2) Like "*Excellent" Then
            If arrK(Cnt, 1) = "Y" Then
             Let strY = strY & " " & Cnt
            Else
             Let strAnuver = strAnuver & " " & Cnt
            End If
        End If
    Next Cnt

'Output successful
 Let Clms() = Array(6, 5, 4, 13, 13, 14, 14)
 Let strRws() = Split(strY)
ReDim Rws(1 To UBound(strRws()) + 1, 1 To 1)
    For Cnt = 1 To UBound(strRws()) + 1
        Rws(Cnt, 1) = strRws(Cnt - 1)
    Next Cnt
 Let arrOut() = Application.Index(wS.Cells, Rws(), Clms())
'wM.Activate
wM.Cells.Clear
    With wM.Range("A18").Resize(UBound(arrOut, 1), UBound(arrOut, 2))
     .Value2 = arrOut()
    End With
'  the duplicated columns need to be fiddled a bit so that one displays the integer and the other the decimal
  Let Em = UBound(arrOut(), 1)
    With wM.Range("D19:D" & 19 + Em - 1 - 1 & "")
     .Value2 = wM.Evaluate("=(D19:D" & 19 + Em - 1 - 1 & "-IF({1},Trunc(D19:D" & 19 + Em - 1 - 1 & ")))*100")
    End With
    With wM.Range("E19:E" & 19 + Em - 1 - 1 & "")
     .Value2 = wM.Evaluate("=IF({1},INT(E19:E" & 19 + Em - 1 - 1 & "))")
    End With
    With wM.Range("F19:F" & 19 + Em - 1 - 1 & "")
     .Value2 = wM.Evaluate("=(F19:F" & 19 + Em - 1 - 1 & "-IF({1},Trunc(F19:F" & 19 + Em - 1 - 1 & ")))*100")
    End With
    With wM.Range("G19:G" & 19 + Em - 1 - 1 & "")
     .Value2 = wM.Evaluate("=IF({1},INT(G19:G" & 19 + Em - 1 - 1 & "))")
    End With


'Output Unsuccessful
'Clms = Array(6, 5, 4, 13, 14)
 Let strRws() = Split(strAnuver)
ReDim Rws(1 To UBound(strRws(), 1) + 1, 1 To 1)
    For Cnt = 1 To UBound(strRws(), 1) + 1
        Rws(Cnt, 1) = strRws(Cnt - 1)
    Next Cnt
 Let arrOut() = Application.Index(wS.Cells, Rws(), Clms())
'wA.Activate
wA.Cells.Clear
    With wA.Range("A18").Resize(UBound(arrOut, 1), UBound(arrOut, 2))
     .Value2 = arrOut()
    End With
'  the duplicated columns need to be fiddled a bit so that one displays the integer and the other the decimal
  Let Em = UBound(arrOut(), 1)
    With wA.Range("D19:D" & 19 + Em - 1 - 1 & "")
     .Value2 = wA.Evaluate("=(D19:D" & 19 + Em - 1 - 1 & "-IF({1},Trunc(D19:D" & 19 + Em - 1 - 1 & ")))*100")
    End With
    With wA.Range("E19:E" & 19 + Em - 1 - 1 & "")
     .Value2 = wA.Evaluate("=IF({1},INT(E19:E" & 19 + Em - 1 - 1 & "))")
    End With
    With wA.Range("F19:F" & 19 + Em - 1 - 1 & "")
     .Value2 = wA.Evaluate("=(F19:F" & 19 + Em - 1 - 1 & "-IF({1},Trunc(F19:F" & 19 + Em - 1 - 1 & ")))*100")
    End With
    With wA.Range("G19:G" & 19 + Em - 1 - 1 & "")
     .Value2 = wA.Evaluate("=IF({1},INT(G19:G" & 19 + Em - 1 - 1 & "))")
    End With
End Sub


Alan
(aka Doc.AElstein , DocAElstein , dr.aelstein )
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 17 Jan 2023, 14:33, edited 10 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Mr. Hans .... I used this file And I discovered the problem As shown in the screen shot above ... but please refer to #post 5
The idea is that I'm trying with you to find a solution So I attached the file above .... that's all ... Regards

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

DocAElstein Excellent start .... I'm exhausted now so I'll get back to you later... Have a nice time

jakjo
Lounger
Posts: 25
Joined: 28 May 2022, 00:57

Re: Transfer Specific Columns From Source Sheet To Two Sheets

Post by jakjo »

Thank you very much Mr Doc.AElstein
it seems for me that it will be difficult to deal with such case but I will try to apply it on my case on my own .. And, of course, Thank you all!