VBA code to post data from one sheet to other but in different columns

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys, This is a little complicated for me to write the code.
Raw Sheet is a formatted sheet received to be converted into XML. The expected result is the workings sheet. Workings is the sheet where there are columns more than the Raw sheet as it contains all the columns needed. Workings is the sheet where I want the data to be posted column wise from the Raw Sheet, with the help of a code. The headings of taxes have one thing in common "input" and the rates are all different. The CGST, SGST and IGST may be anywhere in the value of Input - beginning, middle or end. The code has to search for the rates of sheet Raw and Post them in sheet Workings.
Voucher No. must be posted as Supplier Invoice No. in the workings sheet. The others columns in Working are for the columns of Raw sheet with amounts in figures which are not in Workings sheet. In this case, they are courier charges @5% and Cartage. In other cases it can be different names. Check for columns with amount for others.
Query 1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1Rajesh
2Kathriguppe
3Bangalore - 560042
4Ph: 000000000
5Purchase R Register
61-Dec-2021 to 31-Dec-2021
7DateParticularsAddressVoucher No.Supplier Invoice No.Supplier Invoice DateGSTIN/UINNarrationGross TotalPurchase GST LocalInput SGST @ 2.5%Input CGST @ 2.5%Round OffPurchase IGST InterstateInput IGST @ 5%Input IGST @ 18%Input CGST @ 9%Input SGST @ 9%Input IGST @ 12%Input SGST @ 6%Input CGST @ 6%Courier Charges @ 5%CartageInput CGST @ 14%Input SGST @ 14%
801-Dec-2021january44033801-12-2021abcde20.0012345.0093.3393.33-0.025.005.0010.0010.0020.00176.08176.08
901-Dec-2021february441319201-12-2021defg32.0040.00112.001234.0010.00
1001-Dec-2021march4424765701-12-202172.00-0.4836.00246.4830.00
1101-Dec-2021april443333901-12-202153.0080.00544.00
1202-Dec-2021may444183902-12-2021hij68.00-0.28665.003.7840.00
1302-Dec-2021june4451053202-12-202145.00-0.40478.0020.40
1402-Dec-2021july446156202-12-2021868.00-0.10922.0046.1050.00
1503-Dec-2021august4471650203-12-2021klmn714.006789.00170.10170.10-0.205.005.00
1603-Dec-2021september448191303-12-20211685.001010.110.291226.801226.8060.00
1703-Dec-2021october449385203-12-20212632.00-0.31215.00125.00
1803-Dec-2021november450385303-12-20211790.00160.001920.0070.00
1903-Dec-2021december45157703-12-2021rstu748.000.15712.0035.00
20Grand Total5764634.003093454.5676226.8376226.835.492379855.78113446.253516.482663.342663.3411363.54780.20780.203300.00-1.00176.08176.08
Raw


Query 1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQAR
1abcdefghijDateSupplier Invoice DateNarrationSupplier Invoice No.Voucher No.ParticularsGross TotalklmnopqrCGST INPUT @ 2.5 %SGST INPUT @ 2.5 %CGST INPUT @ 6 %SGST INPUT @ 6 %CGST INPUT @ 9 %SGST INPUT @ 9 %CGST INPUT @ 14 %SGST INPUT @ 14 %IGST INPUT @ 5 %IGST INPUT @ 12 %IGST INPUT @ 18 %IGST INPUT @ 28 %Round OffPurchase GST LocalPurchase IGST InterstateOthers 1Others 2Others 3Others 4
2#########01-12-2021abcde338338january20.0093.3393.3310.0010.005.005.00176.08176.08-0.0212345.0020.00
3#########01-12-2021defg31923192february32.00112.001234.0040.0010.00
4#########01-12-20214765747657march72.00246.48-0.4836.0030.00
5#########01-12-202133393339april53.00544.0080.00
6#########02-12-2021hij18391839may68.003.78-0.28665.0040.00
7#########02-12-20211053210532june45.0020.40-0.40478.00
8#########02-12-202115621562july868.0046.10-0.10922.0050.00
9#########03-12-2021klmn1650216502august714.00170.10170.105.005.00-0.206789.00
10#########03-12-202119131913september1685.001226.801226.800.291010.1160.00
11#########03-12-202138523852october2632.00125.00-0.31215.00
12#########03-12-202138533853november1790.001920.00160.0070.00
13#########03-12-2021rstu577577december748.0035.000.15712.00
Workings
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The most complicated part is the headings of some columns are different and only the percentage of rate matches. And the columns others1,2,3,4 should search for columns with amount and they shouldn't match with the other columns in the working sheet,
 
Upvote 0
How about:

VBA Code:
Sub MoveColumnsToAnotherSheet()
'
    Application.ScreenUpdating = False                                                              ' Turn ScreenUpdating off
'
Dim StartTime                           As Single
StartTime = Timer                                                                                   ' Start the stop watch
'
    Dim DestinationArray1ColumnNumber   As Long, DestinationArray2ColumnNumber  As Long
    Dim SourceHeaderRow                 As Long, SourceLastRow                  As Long, SourceRows     As Long
    Dim DestinationLastRow              As Long
    Dim SourceLastColumnNumber          As Long
    Dim cell                            As Range
    Dim DestinationLastColumnLetter     As String
    Dim SourceLastColumnLetter          As String
    Dim DestinationArray1               As Variant, DestinationArray2           As Variant, SourceArray As Variant
    Dim DestinationWS                   As Worksheet, SourceWS                  As Worksheet
'
    Set DestinationWS = Sheets("Workings")                                                          ' <--- Set this to the destination sheet
         Set SourceWS = Sheets("Raw")                                                               ' <--- Set this to the source sheet
'
    SourceLastRow = SourceWS.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row - 1          ' Get the last source row of data minus the total row
    SourceLastColumnLetter = Split(Cells(1, (SourceWS.Cells.Find("*", , xlFormulas, , _
            xlByColumns, xlPrevious).Column)).Address, "$")(1)                                      ' Get last column letter used in the source sheet
    SourceLastColumnNumber = SourceWS.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column   ' Get last column number used in the source sheet
'
    With SourceWS.Range("A1:" & SourceLastColumnLetter & SourceLastRow)                             ' Look through the source sheet for the header row
        Set cell = .Find("Date", LookIn:=xlValues)                                                  '   Find the header called 'Date'
'
        If Not cell Is Nothing Then                                                                 '   If 'Date' is found then ...
            SourceHeaderRow = cell.Row                                                              '       Save the row # into SourceHeaderRow
        End If
    End With
'
    SourceRows = SourceLastRow - SourceHeaderRow + 1                                                ' Get # of SourceRows including the HeaderRow
    SourceArray = SourceWS.Range("A" & SourceHeaderRow & ":" & SourceLastColumnLetter & SourceLastRow) ' Load Source page starting with Header Row down to last row
'
    DestinationLastRow = DestinationWS.Range("K" & Rows.Count).End(xlUp).Row                        ' Get last row of data of destination page
    DestinationLastColumnLetter = Split(Cells(1, (DestinationWS.Cells.Find("*", , xlFormulas, , _
            xlByColumns, xlPrevious).Column)).Address, "$")(1)                                      ' Get last column letter used in the destination sheet
'
'--------------------------------------------------------------------------------------------------
'
    ReDim DestinationArray1(1 To SourceRows, 1 To 7)                                                ' Set DestinationArray1 row and column size
'
    For DestinationArray1ColumnNumber = 1 To UBound(DestinationArray1, 2)                           ' Loop through DestinationArray1ColumnNumbers
        DestinationArray1(1, DestinationArray1ColumnNumber) = _
                DestinationWS.Cells(1, 10 + DestinationArray1ColumnNumber)                          '   Load Destination K1:Q1 headers into DestinationArray1
    Next                                                                                            ' Loop back
'
    DestinationWS.Range("K2:Q" & DestinationLastRow).Clear                                          ' Clear the range to be written to
'
'--------------------------------------------------------------------------------------------------
'
    ReDim DestinationArray2(1 To SourceRows, 1 To 17)                                               ' Set DestinationArray2 row and column size
'
    For DestinationArray2ColumnNumber = 1 To UBound(DestinationArray2, 2)                           ' Loop through DestinationArray2ColumnNumbers
        DestinationArray2(1, DestinationArray2ColumnNumber) = _
                DestinationWS.Cells(1, 25 + DestinationArray2ColumnNumber)                          '   Load Destination Z1:AP1 into DestinationArray2
    Next                                                                                            ' Loop back
'
    DestinationWS.Range("Z2:AP" & DestinationLastRow).Clear                                         ' Clear the range to be written to
'
'--------------------------------------------------------------------------------------------------
'
    For I = 1 To SourceLastColumnNumber                                                             ' Loop throug all columns of the source sheet
        Select Case SourceArray(1, I)                                                               '   Find a match to ...
            Case Is = "Date"                                                                        '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 1) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Supplier Invoice Date"                                                       '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 2) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Narration"                                                                   '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 3) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Supplier Invoice No."                                                        '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 4) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Voucher No."                                                                 '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 5) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Particulars"                                                                 '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 6) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Gross Total"                                                                 '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray1(J, 7) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
'
            Case Is = "Input CGST @ 2.5%"                                                           '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 1) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input SGST @ 2.5%"                                                           '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 2) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input CGST @ 6%"                                                             '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 3) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input SGST @ 6%"                                                             '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 4) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input CGST @ 9%"                                                             '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 5) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input SGST @ 9%"                                                             '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 6) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input CGST @ 14%"                                                            '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 7) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input SGST @ 14%"                                                            '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 8) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input IGST @ 5%"                                                             '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 9) = SourceArray(J, I)                                     '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input IGST @ 12%"                                                            '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 10) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input IGST @ 18%"                                                            '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 11) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Input IGST @ 28%"                                                            '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 12) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Round Off"                                                                   '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 13) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Purchase GST Local"                                                          '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 14) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Purchase IGST Interstate"                                                    '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 15) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Courier Charges @ 5%"                                                        '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 16) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
'
            Case Is = "Cartage"                                                                     '       Source Header to match
                For J = 2 To SourceRows                                                             '           When match is found, Loop through the rows and
                    DestinationArray2(J, 17) = SourceArray(J, I)                                    '               Write the values to the DestinationArray
                Next                                                                                '           Loop back for next row
        End Select
    Next
'
'--------------------------------------------------------------------------------------------------
'
    DestinationWS.Range("K1:Q" & SourceRows) = DestinationArray1                                    ' Display results of DestinationArray1 to destination sheet
    DestinationWS.Range("Z1:AP" & SourceRows) = DestinationArray2                                   ' Display results of DestinationArray2 to destination sheet
'
    Application.ScreenUpdating = True                                                               ' Turn ScreenUpdating back on
'
MsgBox "  " & SourceRows & " Rows Processed in " & Timer - StartTime & "Seconds."                   ' Display the time it took to run the script
End Sub
 
Upvote 0
Solution
OMG! That looks like a real long code. Let me try. Will check now
 
Upvote 0
How about five stars for the code. It is perfect JohnnyL. Thank you very much. Another one hour of work in a second.❤❤
 
Upvote 0
Glad to help.

P.S. Don't tell your boss, that gives you an hour to mess around and do other things. ;)
 
Upvote 0
I save time to learn more. I go to the office for only 1-2 hours only, finish my work which would could take normally 15 days to complete. I have just begun learning accounts. So whatever I am asked to do, mostly entering data into the server I try to find an easier way to finish the work with the help of applications which I have created with the help of these codes. I am enjoying my work so there is no place for complaints or getting more work.
 
Upvote 0
The more tough jobs the merrier. It is like accepting challenging work.
 
Upvote 0
You have solved 2 queries with this one. The next project has output in place of input. I will try to edit the code and finish that too. If required will message you.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top