473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Duplicate data in excel sheet and filter the data according to the name

kirubagari
158 100+
Hai experts,

How to duplicate the data from 1 excel sheet to another excel sheet 2.
Lets say

Expand|Select|Wrap|Line Numbers
  1. Name                   Voucher Value       Voucher Number     
  2. lee                      300.00                58419-58421
  3. meena                      300.00                58422-58424
  4. Tan                        300.00                58425-58427 

I would like to filter the data accoring to the voucher number and the result is like this in excel
Expand|Select|Wrap|Line Numbers
  1. 58419    lee      300.00    58419-58421
  2. 58420    lee      300.00    58419-58421
  3. 58421    lee      300.00    58419-58421
  4. 58422    meena    300.00    58422-58424
  5. 58423    meena    300.00    58422-58424
  6. 58424    meena    300.00    58422-58424
  7. 58425    Tan      300.00    58425-58427
  8. 58426    Tan      300.00    58425-58427
  9. 58427    Tan      300.00    58425-58427

I would like to filter the 1st sheet (input data) into second sheet in VBA MACRO.


The coding as below



Expand|Select|Wrap|Line Numbers
  1. Sub Duplicate
  2.   Dim oDoc As Object, oSheet As Object, oCell As Object, oCell2 As Object, oCell3 As Object, oString As String
  3.   Dim oCells As Object
  4.   Dim oCursors As Object
  5.   Dim aAddresss As Variant
  6.  
  7.   REM Define what sheet to used
  8.   oDoc   =ThisComponent
  9.   oSheet =oDoc.CurrentController.ActiveSheet
  10.   oSheet2=oDoc.Sheets.getByIndex(1) '2nd Sheet
  11.  
  12.   REM Get the value of the LastUsedRow & LastUsedColumn
  13.   oCells = oSheet.GetCellbyPosition(0, 0)
  14.   oCursors = oSheet.createCursorByRange(oCells)
  15.   oCursors.GotoEndOfUsedArea(True)
  16.   aAddress = oCursors.RangeAddress
  17.   LastUsedRow = aAddress.EndRow
  18.   LastUsedColumn = aAddress.EndColumn
  19.  
  20.   'Row2Print=row printed row
  21.   'l=last used row in Sheet2
  22.  
  23.  
  24.   For i=0 to LastUsedRow
  25.    'oCell2=ThisComponent.CurrentSelection.getCellAddress 'Currently Selected Cell
  26.    oSelect=ThisComponent.CurrentSelection.getRangeAddress
  27.    oString = oSheet.GetCellbyPosition(oSelect.StartColumn, i).getString() 'IMPORTANT (Need Revision)
  28.    'getCellByPosition(Column,Row)
  29.    oRight = Val(Right(oString,Len(oString)-InStr(1, oString, "-")))
  30.    oLeft =  Val(Left(oString,Len(oString)-InStr(1, oString, "-")))
  31.    Row2Print = oRight - oLeft
  32.    oRangeOrg = oSheet.getCellRangeByName("A"&(i+1)&":O"&(i+1)).RangeAddress   ' copy range
  33.  
  34.     REM Begin Pasting the Value 
  35.     For j=0 to Row2Print
  36.    k=k+1
  37.      oRangeCpy = oSheet2.getCellRangeByName("B"&k).RangeAddress ' insert range
  38.      oCellCpy = oSheet2.getCellByPosition(oRangeCpy.StartColumn,oRangeCpy.StartRow).CellAddress ' insert position
  39.      oSheet.CopyRange(oCellCpy, oRangeOrg) ' copy
  40.     Next
  41.  
  42.    'oSheet2=oDoc.Sheets.getByIndex(1) '2nd Sheet 
  43.    'oCells = oSheet2.GetCellbyPosition(0, 0)
  44.    'oCursors = oSheet2.createCursorByRange(oCells)
  45.    'oCursors.GotoEndOfUsedArea(True)
  46.    'aAddress = oCursors.RangeAddress
  47.    'LastUsedRow = aAddress.EndRow
  48.    'LastUsedColumn = aAddress.EndColumn 
  49.  
  50.     For l=0 to Row2Print
  51.      oCell4=oSheet2.getCellByPosition(0,m) 'A1
  52.      oCell4.setString(oLeft)
  53.      oLeft=oLeft+1
  54.      m=m+1
  55.     Next 
  56.   Next i
  57.   'oCell.NumberFormat=2   '23658.00
  58.   'oCell.SetValue(12345)
  59.   'oCell.SetString("oops")
  60.   'oCell.setFormula("=FUNCTION()")
  61.   'oCell.IsCellBackgroundTransparent = TRUE
  62.   'oCell.CellBackColor = RGB(255,141,56)
  63. End Sub
  64.  
  65. Function GetLastUsedRow(oSheets as Object) as Integer
  66. Dim oCells As Object
  67. Dim oCursors As Object
  68. Dim aAddresss As Variant
  69.  
  70. oCells = oSheets.GetCellbyPosition(0, 0)
  71. oCursors = oSheets.createCursorByRange(oCells)
  72. oCursors.GotoEndOfUsedArea(True)
  73. aAddresss = oCursors.RangeAddress
  74. GetLastUsedRow = aAddresss.EndRow
  75. End Function
  76.  
  77. Function GetLastUsedColumn(oSheet as Object) as Integer
  78.   Dim oCell As Object
  79.   Dim oCursor As Object
  80.   Dim aAddress As Variant
  81.   oCell = oSheet.GetCellbyPosition( 0, 0 )
  82.   oCursor = oSheet.createCursorByRange(oCell)
  83.   oCursor.GotoEndOfUsedArea(True)
  84.   aAddress = oCursor.RangeAddress
  85.   GetLastUsedColumn = aAddress.EndColumn
  86. End Function
  87.  
  88. Sub SelRow()
  89.   Dim oSheet
  90.   Dim oRow
  91.   oSheet = ThisComponent.getSheets().getByIndex(0)
  92.   oRow = oSheet.getRows().getByIndex(2)
  93.   ThisComponent.getCurrentController().select(oRow)
  94. End Sub
  95.  
  96. Sub CopySpreadsheetRange
  97.   oSheet1 = ThisComponent.Sheets.getByIndex(0)    ' sheet no 1, original
  98.   oSheet2 = ThisComponent.Sheets.getByIndex(1)    ' sheet no 2
  99.  
  100.   oRangeOrg = oSheet1.getCellRangeByName("A1:C10").RangeAddress   ' copy range
  101.   oRangeCpy = oSheet2.getCellRangeByName("A1:C10").RangeAddress   ' insert range
  102.  
  103.   oCellCpy = oSheet2.getCellByPosition(oRangeCpy.StartColumn,_
  104.     oRangeCpy.StartRow).CellAddress ' insert position
  105.  
  106.   oSheet1.CopyRange(oCellCpy, oRangeOrg)                  ' copy
  107. End Sub
  108. '----------------------------------------------------------------------------------------
  109.  
  110. Function IsSpreadsheetDoc(oDoc) As Boolean
  111.   Dim s$ : s$ = "com.sun.star.sheet.SpreadsheetDocument"
  112.   IsSpreadsheetDoc = oDoc.SupportsService(s$)
  113. End Function
  114.  
  115. Sub checking( )
  116.   MsgBox IsSpreadsheetDoc(thisComponent)
  117. End Sub
  118.  
  119. Sub ExampleGetValue
  120.   Dim oDoc As Object, oSheet As Object, oCell As Object
  121.   oDoc=ThisComponent
  122.   oSheet=oDoc.Sheets.getByName("Sheet1")
  123.   oCell=oSheet.getCellByposition(0,0) 'A1
  124.   Rem a cell's contents can have one of the three following types:
  125.   Print oCell.getValue()
  126.   'Print oCell.getString()
  127.   'Print oCell.getFormula()
  128. End Sub
  129.  
  130. Sub SelectedCells 
  131.   oSelect=ThisComponent.CurrentSelection.getRangeAddress
  132.   oSelectColumn=ThisComponent.CurrentSelection.Columns
  133.   oSelectRow=ThisComponent.CurrentSelection.Rows
  134.  
  135.   CountColumn=oSelectColumn.getCount
  136.   CountRow=oSelectRow.getCount
  137.  
  138.   oSelectSC=oSelectColumn.getByIndex(0).getName
  139.   oSelectEC=oSelectColumn.getByIndex(CountColumn-1).getName
  140.  
  141.   oSelectSR=oSelect.StartRow+1
  142.   oSelectER=oSelect.EndRow+1
  143.   NoCell=(CountColumn*CountRow)
  144.  
  145.   If CountColumn=1 AND CountRow=1 Then
  146.     MsgBox("Cell " + oSelectSC + oSelectSR + chr(13) + "Cell No = " + NoCell,, "SelectedCells")
  147.   Else
  148.     MsgBox("Range(" + oSelectSC + oSelectSR + ":" + oSelectEC + oSelectER + ")" + chr(13) + "Cell No = " + NoCell,, "SelectedCells") 
  149.   End If
  150. End Sub
  151.  
  152. Sub Analize
  153.   sSum="=SUM("+GetAddress+")"
  154.   sAverage="=AVERAGE("+GetAddress+")"
  155.   sMin="=MIN("+GetAddress+")"
  156.   sMax="=MAX("+GetAddress+")"
  157.   CellPos(7,6).setString(GetAddress)
  158.   CellPos(7,8).setFormula(sSum)
  159.   CellPos(7,8).NumberFormat=2
  160.   CellPos(7,10).setFormula(sAverage)
  161.   CellPos(7,10).NumberFormat=2
  162.   CellPos(7,12).setFormula(sMin)
  163.   CellPos(7,12).NumberFormat=2
  164.   CellPos(7,14).setFormula(sMax)
  165.   CellPos(7,14).NumberFormat=2
  166. End sub
  167.  
  168. Function GetAddress  'selected cell(s)
  169.   oSelect=ThisComponent.CurrentSelection.getRangeAddress
  170.   oSelectColumn=ThisComponent.CurrentSelection.Columns
  171.   oSelectRow=ThisComponent.CurrentSelection.Rows
  172.  
  173.   CountColumn=oSelectColumn.getCount
  174.   CountRow=oSelectRow.getCount
  175.  
  176.   oSelectSC=oSelectColumn.getByIndex(0).getName
  177.   oSelectEC=oSelectColumn.getByIndex(CountColumn-1).getName
  178.  
  179.   oSelectSR=oSelect.StartRow+1
  180.   oSelectER=oSelect.EndRow+1
  181.   NoCell=(CountColumn*CountRow)
  182.  
  183.   If CountColumn=1 AND CountRow=1 then  
  184.     GetAddress=oSelectSC+oSelectSR  
  185.   Else  
  186.     GetAddress=oSelectSC+oSelectSR+":"+oSelectEC+oSelectER 
  187.   End If
  188. End Function
  189. Function CellPos(lColumn As Long,lRow As Long)
  190.   CellPos= ActiveSheet.getCellByPosition (lColumn,lRow)
  191. End Function
  192. Function ActiveSheet
  193.   ActiveSheet=StarDesktop.CurrentComponent.CurrentController.ActiveSheet
  194. End Function
  195. Sub DeleteDbRange(sRangeName As String)    
  196.   oRange=ThisComponent.DatabaseRanges
  197.   oRange.removeByName (sRangeName)
  198. End Sub 

KINDLY HELP ME SINCE THE CODING NOT WORKING INTEMETINLY..NEED HELP!!
Sep 22 '10 #1
1 2670
jimatqsi
1,271 Expert 1GB
Wrong forum, I think you want the Excel forum.

Jim
Sep 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Hi5 | last post by:
Hi, I am new to access I usedto work in Oracle and Mysql. I am after a way that enables me to populate a database I designed in access with lots of data which can be sorted in excel sheets, ...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
1
by: Mamatha | last post by:
Hi I want to export data from textfile to Excel sheet in VB.NET application.When i copy the text file into Excel sheet the data of text file are copied to only one cell of the Sheet. Now i...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
0
by: KK | last post by:
Hi, I am using Response object in vb.net to download an excel sheet with data. It worked well and I could download excel file with data. But suddenly It started downloading only blank excel...
1
by: kannabiran | last post by:
Hi everyone, Im using the C# asp.net as the front end and i need to use the Excel sheet as the back end.i want to select the records which is available in the excel sheet using query and...
0
by: uprakash14 | last post by:
i have excel sheet and i want to open the that data in datagrid or msfexgrid.. plz help me .. or give me code
1
by: pvenu | last post by:
Hi, I know basic perl (regular expressions, pattern matching, string manipulation, reading writing into text files). Yet, my requirement is to read an input text file -> process this input file...
3
by: inepu | last post by:
I have some excel sheets that I would like to manage in access. I've created the access tables and relationships; the excel sheet is only one table and with duplicate data, I'd like to make sure that...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.