| View previous topic :: View next topic |
| Author |
Message |
jaango123 General User

Joined: 12 Apr 2012 Posts: 12
|
Posted: Thu Apr 12, 2012 11:26 pm Post subject: [Solved] conversion from vba to openoffice basic |
|
|
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 |
|
 |
patel Power User

Joined: 14 Apr 2012 Posts: 54 Location: Italy
|
Posted: Sat Apr 14, 2012 1:47 am Post subject: |
|
|
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 |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8984 Location: Lexinton, Kentucky, USA
|
Posted: Sat Apr 14, 2012 6:19 pm Post subject: |
|
|
| 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 |
|
 |
patel Power User

Joined: 14 Apr 2012 Posts: 54 Location: Italy
|
Posted: Sat Apr 14, 2012 10:17 pm Post subject: |
|
|
| 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 |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8984 Location: Lexinton, Kentucky, USA
|
Posted: Sun Apr 15, 2012 11:18 am Post subject: |
|
|
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 |
|
 |
jaango123 General User

Joined: 12 Apr 2012 Posts: 12
|
Posted: Mon Apr 16, 2012 12:53 am Post subject: |
|
|
Thanks guys. It worked.  |
|
| Back to top |
|
 |
patel Power User

Joined: 14 Apr 2012 Posts: 54 Location: Italy
|
Posted: Mon Apr 16, 2012 2:27 am Post subject: |
|
|
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 |
|
 |
|
|
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
|