[left]
Hi to all,
/*****************************/
OS-WIn XP SP2
VB6 SP6
/*****************************/
Is their anyone who can help me with this:
Source code written on VB6.
Description:
When this procedure is fired. It will read and seach from an excel file and locate the INPUTTED text as search item. Searching will start from Column 'L' to 'Z' and from rows 'L6' to 'L2915'
(this is my old program in vb6 and due to business i cant focus on this)
This module is already working... problem is it eats a lot of resources ...
can anyone pls tell me how to re-write this code...?
any help is appreciated...
Thank you!
Private Sub Cmd_Click(Index As Integer)
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlBook2 As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim wksht As New Worksheet '*Ref to worksheet
Dim wksht2 As New Worksheet '*Ref to worksheet
Dim Var1 As String 'Holds the search string
Dim ctr As Long, Duplicate As Long, Counter As Long: Counter = 6
Dim c As Object, Bilang As Long, Bilang2 As Long, AddL As Currency: AddL = 0
Dim AddL2 As Currency: AddL2 = 0 'Monthly Dep.
Dim AddL3 As Currency: AddL3 = 0 'Lease Payable
Dim AddL4 As Currency: AddL4 = 0 'Interest
Dim BlnL6L9 As Boolean, OK_Item As Boolean 'Row six to nine
BlnL6L9 = False: OK_Item = False
Dim RowCntr As Long: RowCntr = 6
Dim IsNextCol As Boolean: IsNextCol = False
Dim Alphabet As Integer
Dim Letters As Integer
Dim A
On Error GoTo ErrDisplay
Select Case Index
Case 0 '*Display
Screen.MousePointer = vbHourglass
If ((txtLN.Enabled = True Or txtLN.Enabled = False) And Len(txtLN.Text) = 0) Then 'Or ((CboGroups.Enabled = True Or CboGroups.Enabled = False) And Len(CboGroups.Text) = 0) Then
If CboGroups.Enabled = False Then
MsgBox "Input Required.", vbInformation
Screen.MousePointer = vbDefault
Exit Sub
End If
End If
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open("C:\Fix assets\Asset Managment (2) (2) 10 03 2006.xls")
If CboGroups.Enabled = True Then _
If CboGroups.Text = "ALL" Then xlApp.Visible = True: GoTo EndShow
Set xlSheet = xlApp.Worksheets("summary")
xlSheet.Visible = xlSheetVisible
'*********************************Groups
If Opt(0).Value = True Then 'Groups 'where =trim(CboGroups.text)
'Copy Worksheet Summary
xlApp.Worksheets.Copy xlApp.Worksheets("summary")
Set wksht = xlApp.Worksheets("summary (2)") 'xlBook.Worksheets(1)
wksht.Activate
Var1 = Trim(CboGroups.Text)
Set foundcell = xlBook.Worksheets("summary").Columns("L").Find(Var 1)
'If in column L did not find then move to other columns (M-Z)
If foundcell Is Nothing Then
For Letters = 77 To 90
Set foundcell = xlBook.Worksheets("summary").Columns(Chr$(Letters) ).Find(Var1)
If Not foundcell Is Nothing Then Exit For
Next Letters
End If
'If not found from columns(A-Z)
If foundcell Is Nothing Then
MsgBox "Not found.", vbInformation
Screen.MousePointer = vbDefault
Exit Sub
End If
Duplicate = 0 'Set counter to 0 trace how many duplicates
Counter = 6 'Rows indicators/Count ...starts with rows 6
Bilang2 = 1 'Counts 1 to 4 rows
Dim blnFirst As Boolean: blnFirst = True
Dim blnUna As Boolean
Dim Merun As Boolean
'*********************************** EXCEL TEMPLATE
Set xlBook2 = xlApp.Workbooks.Open(App.Path & "\Asset Managment Template.xls")
'*********************************** EXCEL TEMPLATE
'***************************************
Set wksht2 = xlBook2.Worksheets("summary")
wksht2.Activate
xlApp.DisplayAlerts = False
xlBook2.Saved = True
'***************************************
For Each c In wksht.Range("L6:L2915") 'Find range and instance
Screen.MousePointer = vbHourglass
If Trim(CStr(wksht.Range("K" & Counter))) = "Group" Then 'And OK_Item = True Then
If Trim(CStr(wksht.Range("K" & Counter))) = "Group" Then 'And Not IsNull(Trim(CStr(wksht.Range("L" & Counter)))) Then
For Alphabet = 76 To 90 'L-Z
If Trim(CStr(wksht.Range(Chr$(Alphabet) & Counter))) = Trim(Var1) Then _
Merun = True
Next Alphabet
If Merun = True Then
Duplicate = Duplicate + 1
Bilang = 7 '9
BlnL6L9 = True
wksht.Range("A" & (Counter - 4)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
wksht.Range("A" & (Counter - 3)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
wksht.Range("A" & (Counter - 2)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
wksht.Range("A" & (Counter - 1)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
wksht.Range("A" & (Counter)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
'************************************************* *****************
wksht.Range("A" & (Counter + 1)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
wksht.Range("A" & (Counter + 2)).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
'************************************************* *****************
End If
Bilang2 = 1
Merun = False
End If
End If
Counter = Counter + 1
Screen.MousePointer = vbDefault
Next c
'************************************************* **************************************
Counter = 0
Bilang = 0
Bilang2 = 0
xlApp.Visible = True
Set xlSheet = xlBook.Worksheets("summary")
xlSheet.Visible = xlSheetHidden
ElseIf Opt(1).Value = True Then 'Lease Nos. 'where =trim(TxtLN.text)
'**********************************Lease Nos.
'Copy Worksheet Summary
xlApp.Worksheets.Copy xlApp.Worksheets("summary")
Set wksht = xlApp.Worksheets("summary (2)") 'xlBook.Worksheets(1)
wksht.Activate
'Set Var1 = xlBook.Worksheets("summary").Range("c6")
Var1 = UCase(Trim(txtLN.Text))
Set foundcell = xlBook.Worksheets("summary").Columns("C").Find(Var 1)
If foundcell Is Nothing Then
MsgBox "Not found.", vbInformation
Screen.MousePointer = vbDefault
Exit Sub
End If
Dim HasCaption As Boolean: HasCaption = True
Duplicate = 0 'Set counter to 0
Counter = 6 'Rows indicators/Count
Bilang2 = 1 'Counts 1 to 4 rows
'*********************************** EXCEL TEMPLATE
Set xlBook2 = xlApp.Workbooks.Open(App.Path & "\Asset Managment Template.xls")
'*********************************** EXCEL TEMPLATE
'***************************************
Set wksht2 = xlBook2.Worksheets("summary")
wksht2.Activate
xlApp.DisplayAlerts = False
xlBook2.Saved = True
'***************************************
For Alphabet = 76 To 90 'L-Z
For Each c In wksht.Range("C6:C2915") 'Find range and instance
Screen.MousePointer = vbHourglass
If c.Value = Var1 Then
Duplicate = Duplicate + 1
Bilang = 7
Bilang2 = 1
AddL = AddL + Round(wksht.Range(Chr$(Alphabet) & Counter))
BlnL6L9 = True
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr) '.Range("A" & Counter).Rows 'wksht2.Range("A" & Counter, "BG" & Counter).Row
RowCntr = RowCntr + 1
End If
Else
If Bilang > 0 Then
Bilang = Bilang - 1
If BlnL6L9 = True Then
Bilang2 = Bilang2 + 1
If Bilang2 <= 7 Then '4 Then
If Bilang2 = 2 Then
AddL2 = AddL2 + Round(wksht.Range(Chr$(Alphabet) & Counter))
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
ElseIf Bilang2 = 3 Then
AddL3 = AddL3 + Round(wksht.Range(Chr$(Alphabet) & Counter))
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
ElseIf Bilang2 = 4 Then
AddL4 = AddL4 + Round(wksht.Range(Chr$(Alphabet) & Counter))
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
'************************************************* *****************
ElseIf Bilang2 = 5 Then
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
ElseIf Bilang2 = 6 Then
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
ElseIf Bilang2 = 7 Then
If IsNextCol = False Then
wksht.Range("A" & Counter).EntireRow.Copy '.Copy
xlBook2.ActiveSheet.Paste Destination:=xlBook2.Worksheets("summary").Range(" A" & RowCntr)
RowCntr = RowCntr + 1
End If
'Bilang2 = 1
'************************************************* *****************
End If
Else
BlnL6L9 = False
Bilang2 = 1
End If
End If
End If
End If
Counter = Counter + 1
Screen.MousePointer = vbDefault
Next c
If HasCaption Then
With wksht2
.Range("K" & RowCntr).Font.Size = 10
.Range("K" & RowCntr).Font.Bold = True
.Range("K" & RowCntr).HorizontalAlignment = 2 'Align left
.Range("K" & RowCntr).Value = "Book Value:"
.Range("K" & (RowCntr + 1)).Font.Size = 10
.Range("K" & (RowCntr + 1)).Font.Bold = True
.Range("K" & (RowCntr + 1)).HorizontalAlignment = 2 'Align left
.Range("K" & (RowCntr + 1)).Value = "Monthly Dep.:"
.Range("K" & (RowCntr + 2)).Font.Size = 10
.Range("K" & (RowCntr + 2)).Font.Bold = True
.Range("K" & (RowCntr + 2)).HorizontalAlignment = 2 'Align left
.Range("K" & (RowCntr + 2)).Value = "Lease Payable:"
.Range("K" & (RowCntr + 3)).Font.Size = 10
.Range("K" & (RowCntr + 3)).Font.Bold = True
.Range("K" & (RowCntr + 3)).HorizontalAlignment = 2 'Align left
.Range("K" & (RowCntr + 3)).Value = "Interest:"
End With
HasCaption = False
End If
With wksht2
.Range(Chr$(Alphabet) & RowCntr).Font.Size = 10
.Range(Chr$(Alphabet) & RowCntr).Font.Bold = True
.Range(Chr$(Alphabet) & RowCntr).HorizontalAlignment = 1 'Align Right
.Range(Chr$(Alphabet) & RowCntr).Value = Format(AddL, "#,###,###.#0")
.Range(Chr$(Alphabet) & RowCntr + 1).Font.Size = 10
.Range(Chr$(Alphabet) & RowCntr + 1).Font.Bold = True
.Range(Chr$(Alphabet) & RowCntr + 1).HorizontalAlignment = 1 'Align Right
.Range(Chr$(Alphabet) & RowCntr + 1).Value = Format(AddL2, "#,###,###.#0")
.Range(Chr$(Alphabet) & RowCntr + 2).Font.Size = 10
.Range(Chr$(Alphabet) & RowCntr + 2).Font.Bold = True
.Range(Chr$(Alphabet) & RowCntr + 2).HorizontalAlignment = 1 'Align Right
.Range(Chr$(Alphabet) & RowCntr + 2).Value = Format(AddL3, "#,###,###.#0")
.Range(Chr$(Alphabet) & RowCntr + 3).Font.Size = 10
.Range(Chr$(Alphabet) & RowCntr + 3).Font.Bold = True
.Range(Chr$(Alphabet) & RowCntr + 3).HorizontalAlignment = 1 'Align Right
.Range(Chr$(Alphabet) & RowCntr + 3).Value = Format(AddL4, "#,###,###.#0")
End With
AddL = 0
AddL2 = 0
AddL3 = 0
AddL4 = 0
Duplicate = 0
Counter = 6
IsNextCol = True
Next Alphabet
With wksht2
.Range("B" & (RowCntr - 1), "I" & (RowCntr - 1)).Borders.LineStyle = 1
.Range("B" & (RowCntr - 1), "I" & (RowCntr - 1)).Borders.Weight = 3
End With
'************************************************
RowCntr = 6
'************************************************
xlApp.Visible = True
Set xlSheet = xlBook.Worksheets("summary")
xlSheet.Visible = xlSheetHidden
'*Rename the worksheet
With xlBook
.Sheets("summary (2)").Name = "fsummary" 'rename the sheet
Set wksht = .Sheets("fsummary") 'make this the active sheet
End With
End If '*Lease No
xlBook.Close
Screen.MousePointer = vbDefault
Case 1 '*Cancel
Unload Me
End Select
EndShow:
xlApp.WindowState = xlMaximized
Alphabet = 0
Letters = 0
Set c = Nothing
Set foundcell = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Set xlBook2 = Nothing '*
Set xlSheet = Nothing
Set wksht = Nothing
Set wksht = Nothing '*
Screen.MousePointer = vbDefault
Exit Sub
ErrDisplay:
MsgBox Err.Description & "" & Err.Source, vbInformation
Screen.MousePointer = vbDefault
End Sub