S
spunkymuffmonkey
Hi community.
I wonder if anybody would be willing to have a quick look at my code below
and offer me any suggestions for fine tuning/making faster?
What you need to know:
I have a userform which has two listboxes, the listbox values are:
listbox1) values derived from oSourceDoc table rows, (oSourceDoc has a
fourcolumn table with many rows I call entries)
listbox2) values derived from name of all open documents in a particular
network folder (U:\checkout).
The code below does work, what it is designed to do is read which listitems
the user has selected from listbox1, copy the relevant table rows from
oSourceDoc and then paste them into the documents selected in listbox2.
My goal would be to make this a faster process if possible, it's currently
taking about 1 second to copy 1 row into two documents.
Full code below (for this part of the project), btw, please don't snigger at
my efforts as I'm still learning lol
===================================================
Public Function CopyRecords()
Dim oRow As Row
frmCopyProgress.Show
On Error GoTo ErrHandler
'stop screenupdates
Application.ScreenUpdating = False
'this is the selection for the rows of the source doc
For x = 0 To lbxEntries.ListCount - 1
If lbxEntries.Selected(x) = True Then
y = x + 2
With oSourceDoc.Tables(2).Rows(y)
Set myRange = .Cells(1).Range
myRange.End = .Cells(4).Range.End
'myRange.End = .Cells(.Cells.Count).Range.End
End With
myRange.Font.Name = "Arial"
myRange.Copy
'this loops through which target docs the user wants to paste in
'then pastes the row at the end
For z = 0 To lbxTargetDocs.ListCount - 1
If lbxTargetDocs.Selected(z) = True Then
k = z + 1
If Application.Documents(k).Name <> oSourceDoc.Name Then
Select Case Application.Documents(k).Tables.Count
Case 1: ' for old style runners
With Application.Documents(k).Tables(1)
.Rows.Last.Select
Selection.InsertRowsAbove
Selection.Paste
End With
Case 2 ' for new style runners
With Application.Documents(k).Tables(2)
If .Rows.Count = 1 Then
.Rows.Add
.Rows.Last.HeightRule = wdRowHeightAtLeast
.Rows.Last.Height = 20
.Rows.Last.Select
Selection.Font.Name = "Arial"
Else
.Rows.Add
.Rows.Last.Select
End If
Selection.Paste
End With
Case Else
j = Application.Documents(k).Tables.Count
Application.Documents(k).Tables(j).Rows.Last.Select
Selection.Paste
End Select
End If
End If
UpdateCopyProgressBar y
Next z
End If
Next x
Selection.Collapse
If chkSort.Value = True Then
DateSort k
End If
If chkDelete.Value = True Then
DeleteBlankRows k
End If
Set myRange = Nothing
Unload frmCopyProgress
Unload frmCopyRecord
MsgBox "Records copied sucessfully!", vbInformation, "Complete"
ErrHandler:
If Err.Number = 5941 Then
MsgBox "No Running Record table in the target document, please call
the Castle Helpdesk, click OK for more information", vbCritical, "Error 5941"
frmHelp.Show
Exit Function
End If
End Function
===================================================
Any advice and assitance offered will be most gratefully received.
Thanks for your time.
I wonder if anybody would be willing to have a quick look at my code below
and offer me any suggestions for fine tuning/making faster?
What you need to know:
I have a userform which has two listboxes, the listbox values are:
listbox1) values derived from oSourceDoc table rows, (oSourceDoc has a
fourcolumn table with many rows I call entries)
listbox2) values derived from name of all open documents in a particular
network folder (U:\checkout).
The code below does work, what it is designed to do is read which listitems
the user has selected from listbox1, copy the relevant table rows from
oSourceDoc and then paste them into the documents selected in listbox2.
My goal would be to make this a faster process if possible, it's currently
taking about 1 second to copy 1 row into two documents.
Full code below (for this part of the project), btw, please don't snigger at
my efforts as I'm still learning lol
===================================================
Public Function CopyRecords()
Dim oRow As Row
frmCopyProgress.Show
On Error GoTo ErrHandler
'stop screenupdates
Application.ScreenUpdating = False
'this is the selection for the rows of the source doc
For x = 0 To lbxEntries.ListCount - 1
If lbxEntries.Selected(x) = True Then
y = x + 2
With oSourceDoc.Tables(2).Rows(y)
Set myRange = .Cells(1).Range
myRange.End = .Cells(4).Range.End
'myRange.End = .Cells(.Cells.Count).Range.End
End With
myRange.Font.Name = "Arial"
myRange.Copy
'this loops through which target docs the user wants to paste in
'then pastes the row at the end
For z = 0 To lbxTargetDocs.ListCount - 1
If lbxTargetDocs.Selected(z) = True Then
k = z + 1
If Application.Documents(k).Name <> oSourceDoc.Name Then
Select Case Application.Documents(k).Tables.Count
Case 1: ' for old style runners
With Application.Documents(k).Tables(1)
.Rows.Last.Select
Selection.InsertRowsAbove
Selection.Paste
End With
Case 2 ' for new style runners
With Application.Documents(k).Tables(2)
If .Rows.Count = 1 Then
.Rows.Add
.Rows.Last.HeightRule = wdRowHeightAtLeast
.Rows.Last.Height = 20
.Rows.Last.Select
Selection.Font.Name = "Arial"
Else
.Rows.Add
.Rows.Last.Select
End If
Selection.Paste
End With
Case Else
j = Application.Documents(k).Tables.Count
Application.Documents(k).Tables(j).Rows.Last.Select
Selection.Paste
End Select
End If
End If
UpdateCopyProgressBar y
Next z
End If
Next x
Selection.Collapse
If chkSort.Value = True Then
DateSort k
End If
If chkDelete.Value = True Then
DeleteBlankRows k
End If
Set myRange = Nothing
Unload frmCopyProgress
Unload frmCopyRecord
MsgBox "Records copied sucessfully!", vbInformation, "Complete"
ErrHandler:
If Err.Number = 5941 Then
MsgBox "No Running Record table in the target document, please call
the Castle Helpdesk, click OK for more information", vbCritical, "Error 5941"
frmHelp.Show
Exit Function
End If
End Function
===================================================
Any advice and assitance offered will be most gratefully received.
Thanks for your time.