How to Print the Two sorted array?
HELLO,
I struggle up for last two days for this issue. Here i explained it and give my code. Please anyone look into this and help me to resolve.
Requirements:
I have Array() values having only Year information
Array(0) = (Deepak) 1987
Array(1) = (G.Lakshmi) 1985
Array(2) = (U.Dev) 1988
Array(3) = (G.Kannan) 1985
Array(4) = (D.Dhana) 1988
I sorted this array Chronologically
Code:
WordBasic.SortArray Array()
So that i Got the values:
Sortarray(0) = (G.Lakshmi) 1985
Sortarray(1) = (G.Kannan) 1985
Sortarray(2) = (G.Deepak) 1987
Sortarray(3) = (U.Dev) 1988
Sortarray(4) = (D.Dhana) 1988
Again i have to sort this array values alphabetically So that i need to print this values
Sortarray1(0) = (G.Kannan) 1985
Sortarray1(1) = (G.Lakshmi) 1985
Sortarray1(2) = (G.Deepak) 1987
Sortarray1(3) = (D.Dhana) 1988
Sortarray1(4) = (G.Dev) 1988
Dim II As Integer
Dim JJ As Integer
Dim KK As Integer
Dim N As Integer
Dim Counter As Integer
For II = 0 To UBound(adarry())
Counter = 1
For JJ = II + 1 To UBound(adarry())
If adarry(II) = adarry(JJ) Then
Counter = Counter + 1
MsgBox (Counter)
End If
Next JJ
'Lop1:
If Counter = 1 Then
'For KK = 0 To UBound(adarry())
Code:
Dim i1 as integer
Dim j1 as integer
Dim II As Integer
Dim JJ As Integer
Dim KK As Integer
Dim N As Integer
Dim Counter As Integer
i1 having the value of LBArray and J1 the value of UBArray
For II = 0 To UBound(SortArray())
Counter = 1
For JJ = II + 1 To UBound(SortArray())
If SortArray(II) = SortArray(JJ) Then
Counter = Counter + 1
MsgBox (Counter)
End If
Next JJ
'Lop1:
If Counter = 1 Then
For N = i1 To J1
If SortArray(II) = ReferenceYear(N) Then
Selection.TypeText Text:="(" & Authorname(N) & Array(N) & ")"
End If
Next N
End If
II = II + Counter
Next II
It print only the (Name and Year) improperly. Did Anyone can look into this?
Re: How to Print the Two sorted array?
Deepak,
I hav done this kind of sorting in excel, see the code, if u get the logic of that u may get idea
Column A = Name, Column B = Year
Code:
Sub SortRange()
Dim i As Long, j As Long
Dim FirstCell As Long, LastCell As Long, LastRow As Long
With Sheet1
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
'sort by the Column B (By Years)
.Range("A1:B" & LastRow).Sort _
Key1:=.Range("B1"), order1:=xlAscending
FirstCell = 1
LastCell = 2
n1:
'Find Duplicate and sort the Duplicates by column A (by name)
For i = FirstCell To LastRow 'find duplicate values
For j = i + 1 To LastRow
If .Range("B" & i) = .Range("B" & j) Then
FirstCell = i
LastCell = j
'sort
.Range("A" & FirstCell & ":B" & LastCell).Sort _
Key1:=.Range("A1"), order1:=xlAscending
Else
FirstCell = j
GoTo n1
End If
Next
Next
End With
End Sub
Re: How to Print the Two sorted array?
Thank you Seenu,
I will Refer this Code.
Thanks,
DeepakGopikannan
Re: How to Print the Two sorted array?
It is a Timely Help for me Seenu.
I Resolve the issue.
Regards,
DeepakGopikannan
Re: How to Print the Two sorted array?
@ Seenu,
Hello ji, Is there any way to call word arguments into excel vba? How to do that?
Regards,
DeepakGopikannan
Re: How to Print the Two sorted array?
what code r u using to copy the array to excel?
Re: How to Print the Two sorted array?
this writes the array from excel to word, add a command button in sheet1, and fill the datas in Column A & B (Name & Year).
Code:
Private Sub CommandButton1_Click()
Dim Ar1() As String, LastRow As Integer
Dim WrdApp As Object, WrdDoc As Object, WrdSel As Object
SortRange 'sort the range by call the sub
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'load range data to the array
ReDim Ar1(1 To 2, 1 To LastRow) As String
For i = 1 To LastRow
Ar1(1, i) = Cells(i, "A") 'Name
Ar1(2, i) = Cells(i, "B") 'Year
Next
'fil the array to word
Set WrdApp = CreateObject("Word.Application")
WrdApp.Visible = True
Set WrdDoc = WrdApp.Documents.Add ' create a new document
Set WrdSel = WrdApp.Selection
With WrdSel
For i = LBound(Ar1, 2) To UBound(Ar1, 2)
.TypeText Text:=Ar1(1, i) & " - " & Ar1(2, i) & vbCrLf
Next
End With
Set WrdApp = Nothing
Set WrdDoc = Nothing
Set WrdSel = Nothing
End Sub
Sub SortRange()
Dim i As Long, j As Long
Dim FirstCell As Long, LastCell As Long, LastRow As Long
With Sheet1
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
'sort by the Column B (By Years)
.Range("A1:B" & LastRow).Sort _
Key1:=.Range("B1"), order1:=xlAscending
FirstCell = 1
LastCell = 2
n1:
'Find Duplicate and sort the Duplicates by column A (by name)
For i = FirstCell To LastRow 'find duplicate values
For j = i + 1 To LastRow
If .Range("B" & i) = .Range("B" & j) Then
FirstCell = i
LastCell = j
'sort
.Range("A" & FirstCell & ":B" & LastCell).Sort _
Key1:=.Range("A1"), order1:=xlAscending
Else
FirstCell = j
GoTo n1
End If
Next
Next
End With
End Sub
Re: How to Print the Two sorted array?
Hai,
I used Word Vba for coding not on Excel. My source data is too from word only. I have to fetch name and year information from Word document Where the name and year info is sited in numbers.
In the abstract this Reference year and Name value is mentioned as Numbered, hence i have to read the the numbers. as the numbered reference i have to print the Name and year value in Sorted form (Mentioned earlier)
http://www.vbforums.com/showthread.php?p=4184851#post4184851
I posted the code in above mention link. If it possible please guide me. it would be great help for this time.
Thanks in advance,
DeepakGopikannan