I'm trying to fix an sub routine in an VB module that basically reads in a MS database and writes it to an Excel Spread sheet. It works just fine except that the data isn't sorted correctly. I have no experience at all in excel or the vb code to access excel. I found a few lines of code by searching on how to sort in vb for excel and the code with the new sort logic works fine the first time you run it, but run it twice and you get the titled error. Here is the code they created and I marked the code I added. Any help would be deeply appreciated. Sorry ahead of time for length of code. -
Public Sub excel()
-
-
Dim indx As Integer
-
Dim rowIndex As Integer
-
Dim colIndex As Integer
-
Dim recordCount As Integer
-
Dim fieldCount As Integer
-
Dim MSG As String
-
Dim avRows As Variant
-
Dim excelVersion As Integer
-
Dim transType As String
-
Dim system As String
-
Dim sql As String
-
Dim TcrRecs()
-
Dim Oput As String
-
-
system = lstLOB.Text
-
transType = lstTransacType.Text
-
-
'CHECK FOR SEARCH TYPE
-
openconn
-
-
If lstLOB.SelCount = 0 And lstTransacType.SelCount = 0 Then
-
MsgBox "Search Requires A System/Transaction Or Both!", vbExclamation, "Error"
-
closeconn
-
Exit Sub
-
End If
-
-
If lstTransacType.SelCount > 0 Then
-
For a = 0 To lstTransacType.ListCount - 1
-
If lstTransacType.Selected(a) Then
-
If sql = "" Then
-
sql = " AND (TransacType = '" & lstTransacType.List(a) & "'"
-
Else
-
sql = sql + " or TransacType = '" & lstTransacType.List(a) & "'"
-
End If
-
End If
-
Next
-
-
Call rs("SELECT a.Name, b.TransacType, b.TestCaseNum, c.PolicyNum, b.TestScenarioDescription, c.Impact, c.ExpectedResults FROM Areas a, TestCases b, TestCaseExecution c WHERE a.AreaID = b.AreaID AND b.TestCaseID = c.TestCaseID " & sql & ") ORDER BY a.NAME, b.TransacType, b.TestCaseNum ASC")
-
-
End If
-
-
If lstLOB.SelCount > 0 And lstTransacType.SelCount = 0 Then
-
For a = 0 To lstLOB.ListCount - 1
-
If lstLOB.Selected(a) Then
-
If sql = "" Then
-
sql = " AND (name = '" & lstLOB.List(a) & "'"
-
Else
-
sql = sql + " or name = '" & lstLOB.List(a) & "'"
-
End If
-
End If
-
Next
-
-
Call rs("SELECT a.Name, b.TransacType, b.TestCaseNum, c.PolicyNum, b.TestScenarioDescription, c.Impact, c.ExpectedResults FROM Areas a, TestCases b, TestCaseExecution c WHERE a.AreaID = b.AreaID AND b.TestCaseID = c.TestCaseID " & sql & ") ORDER BY a.NAME, b.TransacType, b.TestCaseNum ASC")
-
-
End If
-
-
If adoRS.recordCount = 0 Then
-
MsgBox "There Were No Test Cases Found Matching Your Criteria", vbInformation, "Error"
-
closeconn
-
Exit Sub
-
End If
-
-
'THROWS THE RECORDSET INTO AN ARRAY
-
avRows = adoRS.GetRows()
-
-
recordCount = UBound(avRows, 2) + 1
-
fieldCount = UBound(avRows, 1) + 1
-
-
'CREATE REDERENCE VARIABLE FOR THE SPREADSHEET
-
Set objExcel = CreateObject("Excel.Application")
-
objExcel.Visible = True
-
objExcel.Workbooks.add
-
-
Set objTemp = objExcel
-
-
excelVersion = Val(objExcel.Application.Version)
-
If (excelVersion >= 8) Then
-
Set objExcel = objExcel.ActiveSheet
-
End If
-
-
'PLACE THE NAMES OF THE FIELDS AS COLUMN HEADERS
-
-
With objExcel.Cells(1, 1)
-
.Value = "System"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 2)
-
.Value = "Trans Type"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 3)
-
.Value = "TC Nbr"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 4)
-
.Value = "In Prog"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 5)
-
.Value = "Req Nbr"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 6)
-
.Value = "Policy Nbr"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 7)
-
.Value = "Date"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 8)
-
.Value = "Tstr Intls"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 9)
-
.Value = "Test Scenario Description"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 10)
-
.Value = "Impact"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
With objExcel.Cells(1, 11)
-
.Value = "Expected Results"
-
.VerticalAlignment = xlVAlignTop
-
With .Font
-
.Name = "Arial"
-
.Bold = True
-
.Size = 11
-
.Italic = True
-
End With
-
End With
-
-
'MEMORY MANAGEMENT
-
adoRS.Close
-
Set adoRS = Nothing
-
-
'ADD THE DATA
-
With objExcel
-
For rowIndex = 2 To recordCount + 1
-
-
Oput = IIf(IsNull(avRows(1 - 1, rowIndex - 2)), "", avRows(1 - 1, rowIndex - 2))
-
'Oput = avRows(1 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 1).Value = Oput
-
-
' .Cells(rowIndex, 1).Value = avRows _
-
' (1 - 1, rowIndex - 2)
-
Oput = IIf(IsNull(avRows(2 - 1, rowIndex - 2)), "", avRows(2 - 1, rowIndex - 2))
-
'Oput = avRows(2 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 2).Value = Oput
-
-
' .Cells(rowIndex, 2).Value = avRows _
-
' (2 - 1, rowIndex - 2)
-
Oput = IIf(IsNull(avRows(3 - 1, rowIndex - 2)), "", avRows(3 - 1, rowIndex - 2))
-
'Oput = avRows(3 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 3).Value = Oput
-
-
' .Cells(rowIndex, 3).Value = avRows _
-
' (3 - 1, rowIndex - 2)
-
-
.Cells(rowIndex, 4).Value = " "
-
.Cells(rowIndex, 5).Value = " "
-
-
Oput = IIf(IsNull(avRows(4 - 1, rowIndex - 2)), "", avRows(4 - 1, rowIndex - 2))
-
'Oput = avRows(4 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 6).Value = Oput
-
-
' .Cells(rowIndex, 6).Value = avRows _
-
' (4 - 1, rowIndex - 2)
-
-
.Cells(rowIndex, 7).Value = " "
-
.Cells(rowIndex, 8).Value = " "
-
-
Oput = IIf(IsNull(avRows(5 - 1, rowIndex - 2)), "", avRows(5 - 1, rowIndex - 2))
-
'Oput = avRows(5 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 9).Value = Oput
-
-
' .Cells(rowIndex, 9).Value = avRows _
-
' (5 - 1, rowIndex - 2)
-
-
Oput = IIf(IsNull(avRows(6 - 1, rowIndex - 2)), "", avRows(6 - 1, rowIndex - 2))
-
'Oput = avRows(6 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 10).Value = Oput
-
-
' .Cells(rowIndex, 10).Value = avRows _
-
' (6 - 1, rowIndex - 2)
-
Oput = avRows(7 - 1, rowIndex - 2)
-
Oput = Replace(Oput, Chr(13), "")
-
Oput = Replace(Oput, Chr(9), "")
-
.Cells(rowIndex, 11).Value = Oput
-
' .Cells(rowIndex, 11).Value = avRows _
-
' (7 - 1, rowIndex - 2)
-
-
Next
-
End With
-
-
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
-
objExcel.Cells(1, 1).CurrentRegion.VerticalAlignment = xlVAlignTop
-
objExcel.Cells(1, 1).CurrentRegion.WrapText = True
-
-
' This what I added*******************************************************
-
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1") _
-
, Order2:=xlAscending, Key3:=Range("B1"), Order3:=xlAscending, Header:= _
-
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
-
'*************************************************************************
-
-
closeconn
-
-
End Sub
-
-
1 5111
Hi
Without ploughing through all your code, and based on my the interpritation that it ALWAYS works without the sort, and works just ONCE with the the sort code, I suggest this mod
Change this - ' This what I added********************************************* **********
-
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1") _
-
, Order2:=xlAscending, Key3:=Range("B1"), Order3:=xlAscending, Header:= _
-
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
-
'************************************************* ************************
to this - ' This what I added********************************************* **********
-
objExcel.Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1") _
-
, Order2:=xlAscending, Key3:=Range("B1"), Order3:=xlAscending, Header:= _
-
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
-
'************************************************* ************************
This suggestion is based on very similar problem I had exporting a report to Word (which IMHO is much worse to automate that Excel, as the native object are not so obvious!).
The above suggestion is based on a similar once pass only is OK problem, the explanation being that the native Word/Excel object that are not excplicitly refered to the Woed/Excel object stay referenced to the first instance of Word/Excel, which changes to a different instance for the second pass. Therefore you need to referance the active Word/Excel object that is current instance !!!???
Be interested to see if it solves your problem.
MTB
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bill R via AccessMonster.com |
last post by:
I get this Run-Time Error 1004 whenevery the following code runs:
On Error GoTo XLSheet2
Set objXL = CreateObject("Excel.Application")
With objXL
Set objWkb = .Workbooks.Open(strPath)
With objWkb
Set objSht1 = .Worksheets("Actual_Releases_by_Week")
With objSht1
If IsNull(.Range("A2")) Or .Range("A2") = "" Then GoTo
|
by: richilli |
last post by:
Hi
Any help on this would be appreciated cos its driving me insane.
I have a function in VB.NET that takes in an excel range and tries to
delete rows where the first column starts with a string. Only it
doesnt work and all i get is "Delete method of Range class failed"
whatever i do.
Any suggestions?
|
by: Ike |
last post by:
Can someone please illuminate to me why, in the following snippet of script,
the alert statement in the try-catch gives me ? The file
'http://localhost:1222/roomx1/getdata.php' truly does exist. I'm really lost
here and would appreciate anyone's good eye. Thanks, Ike
if(typeof window.ActiveXObject != 'undefined'){
req = new ActiveXObject("Microsoft.XMLHTTP");
req.onreadystatechange=processReqChange;
}else{
|
by: eyoung |
last post by:
I call a function that takes the unit price and quantity ordered to
create an amount...it looks something like this.
function calculateCost()
{
quantity=document.RFO.quantity.value;
unitPrice=document.RFO.unitPrice.value;
total=0;
if(isPositiveInteger(quantity))
{
|
by: SpreadTooThin |
last post by:
I have a list and I need to do a custom sort on it...
for example:
a = #Although not necessarily in order
def cmp(i,j): #to be defined in this thread.
a.sort(cmp)
print a
| |
by: smichr |
last post by:
It seems to me that the indices() method for slices is could be
improved. Right now it gives back concrete indices for a range of
length n. That is, it does not return any None values. Using an example
from clpy about this the indices for a 'None, None, -2' slice for a
range of length 10 are given as '9, -1, -2'. The problem is that these
concrete values cannot be fed back into a slice so that a slice will
extract the same elements that...
|
by: dilau |
last post by:
I have a problem.
The error in subject appear when i run the macro
Can u tell me why
Case "CENTRALIZATOR SURVEY"
Dim SHT As Object
Set SHT = Application.ThisWorkbook.Sheets("CENTRALIZATOR SURVEY")
SHT.Cells(10, 1) = UCase(n)
If IsEmpty(SHT.Cells(11, 1)) = False Then
|
by: ielamrani |
last post by:
Hi,
I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I get the error: Runtime Error 1004: Method ‘Cells’ of Object ‘_Global’ failed
It highlight this line:
Range("A1:L1").Select
sorry the code behind the button is long:
.
Dim stDocName As String
stDocName = "QFinal4"
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |