OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] conversion from vba to openoffice basic

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
jaango123
General User
General User


Joined: 12 Apr 2012
Posts: 12

PostPosted: Thu Apr 12, 2012 11:26 pm    Post subject: [Solved] conversion from vba to openoffice basic Reply with quote

Hi All,

Please help me with the open office basic code from excel vba

iRow variable in vba contains the number of non empty rows count of Sheet1, column A.


What me doing in VBA is

1)Search if the cell in Column A sheet1 contains comma(,) using Instr function, then change the string, 's' as explained in the below line.

example P=lk,jk will change to P="lk,jk" and assigned to string s. Note:Chr(34) corresponds to ".


2)iterate the loop till end of the row and assign them to string s, after each iteration ", " to be added to the string, unless it is the last row.

3)Finally assign the string to worksheet2, first cell


Here is the vba code snippet

Code:
Dim s as string
For i = 1 To iRow
         If InStr(1, Cells(i, 1).Value, ",") > 0 Then
                    o = InStr(1, Cells(i, 1).Value, "=")
                    s = s & Left(Cells(i, 1).Value, o) & Chr(34) & Right(Cells(i, 1).Value, Len(Cells(i, 1).Value) - o) & Chr(34)
                 Else
                     s = s & Trim(Cells(i, 1).Value)
                 End If
         If i <> iRow Then
           s = s & ", "
         End If
         Worksheets(2).Cells(1, 1) = s
         Next


example

Column A contains

P=lk,jk
S=hjk


sheet2 cell 1,1 should contain P="lk,jk", S=hjk after running the open office basic macro.

Also how do i get the iRow variable in Basic(non empty count of column A)



Moderation probe1: moved to MACROS AND API section, where all macro related questions belong to; CODE SNIPPETS is for working code examples only

Tagged as solved - floris v, moderator
Back to top
View user's profile Send private message
patel
Power User
Power User


Joined: 14 Apr 2012
Posts: 54
Location: Italy

PostPosted: Sat Apr 14, 2012 1:47 am    Post subject: Reply with quote

try this
Code:
REM  *****  BASIC  *****

Sub Main
Dim s As String
Doc = ThisComponent
Sheet = Doc.getSheets().getByIndex(0)
Sheet2 = Doc.getSheets().getByIndex(1)
maxRow = 10
col=0
For i = 0 To maxRow
   Cell = Sheet.getCellByPosition(col, i)
    If InStr(1, Cell.String, ",") > 0 Then
       o = InStr(1, Cell.String, "=")
       s = s & Left(Cell.String, o) & Chr(34) & Right(Cell.String, Len(Cell.String) - o) & Chr(34)
    Else
       s = s & Trim(Cell.String)
    End If
    Cell = Sheet2.getCellByPosition(0,0)
    Cell.String= s
Next
End Sub
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Sat Apr 14, 2012 6:19 pm    Post subject: Reply with quote

Quote:
Also how do i get the iRow variable in Basic(non empty count of column A)

Find the last used cell in a range. Well, actually this returns the last used row in the sheet but will probably do for your purposes.
Code:
Sub Main
oSheet = ThisComponent.Sheets(0)
r = oSheet.getCellRangeByName("A1:A1000")
c = oSheet.createCursorByRange(r)
c.gotoEndOfUsedArea(false)
LastRow = c.RangeAddress.EndRow
print LastRow
End Sub
Back to top
View user's profile Send private message
patel
Power User
Power User


Joined: 14 Apr 2012
Posts: 54
Location: Italy

PostPosted: Sat Apr 14, 2012 10:17 pm    Post subject: Reply with quote

JohnV wrote:

Find the last used cell in a range. Well, actually this returns the last used row in the sheet ....[/code]

very good !
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Sun Apr 15, 2012 11:18 am    Post subject: Reply with quote

Because creating the cursor by range didn't limit the last cell found to that range we can write a shorter version that does the same thing.
Code:
Sub Main
oSheet = ThisComponent.Sheets(0)
c = oSheet.createCursor
c.gotoEndOfUsedArea(false)
LastRow = c.RangeAddress.EndRow
print LastRow
End Sub
Back to top
View user's profile Send private message
jaango123
General User
General User


Joined: 12 Apr 2012
Posts: 12

PostPosted: Mon Apr 16, 2012 12:53 am    Post subject: Reply with quote

Thanks guys. It worked. Smile
Back to top
View user's profile Send private message
patel
Power User
Power User


Joined: 14 Apr 2012
Posts: 54
Location: Italy

PostPosted: Mon Apr 16, 2012 2:27 am    Post subject: Reply with quote

please add "[Solved]" to the beginning of your first post title (edit button).
_________________
If your problem has been solved please add "[Solved]" to the beginning of your first post title (edit button).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group