Results 1 to 9 of 9

Thread: Is there a faster way? (vb and Excel)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Posts
    306

    Is there a faster way? (vb and Excel)

    I have Created an app that can open up an excel template, let the user enter info and then I save the entered info to my SQL database. What I do for easy cell access I have defined a name for each cell the user can enter info into so i can track down where the info came from. here is my code to save the excel info into SQL.

    VB Code:
    1. Private Sub SaveInfo()
    2. On Error GoTo Handler
    3.     Dim info() As String, test() As String
    4.     Dim strSummary As String
    5.     Dim i As Integer
    6.     Dim strPricing As String
    7.    
    8.     'MouseIcon = vbHourglass
    9.    
    10.     ReDim info(0)
    11.     With xls
    12.         Dim a As String, b As String, c As String
    13.        
    14.         '--Set intial and max value for progress bar
    15.         pbExcel.Value = 0
    16.         pbExcel.Max = .Workbooks(1).Names.Count
    17.        
    18.         '--loop thru all the Excel work book Names
    19.         '---This also includes some names that were probably pre-defined
    20.         For i = 1 To .Workbooks(1).Names.Count
    21.             Debug.Print i
    22.            
    23.             '--This function Figures out which sheet the Name came from.
    24.             '--If the Name is not a name I defined the return value is "Stop"
    25.             a = GetSheet(.Workbooks(1).Names(i).Name)
    26.             Debug.Print .Workbooks(1).Names(i).Name
    27.            
    28.             '--If the defined name leads with "S_" then add value to strSummary
    29.             If Left(.Workbooks(1).Names(i).Name, 2) = "S_" Then
    30.                 'Debug.Assert .Workbooks(1).Names(i).Name <> "S_OtherHO"
    31.                 strSummary = strSummary & .Sheets(a).Range(.Workbooks(1).Names(i).Name).Value & "@" & .Workbooks(1).Names(i).Name & ";"
    32.             Else
    33.                 '--If Sheet doesn't equal Stop then it is a name I assigned to the cell
    34.                 If a <> "Stop" Then
    35.                                        
    36.                     '--EX: 1.25 @ FLD_CELL
    37.                     strPricing = .Sheets(a).Range(.Workbooks(1).Names(i).Name).Value & "@" & .Workbooks(1).Names(i).Name & ","
    38.                                     End If
    39.             End If
    40.             pbExcel.Value = i
    41.         Next i
    42.     End With
    43.        
    44.     AddPricingInfo strPricing, strSummary
    45.     'Screen.MousePointer = 99
    46.     MsgBox "Saved Successfully", vbInformation
    47. Exit Sub
    48. Handler:
    49.     Select Case Err.Number
    50.         Case 13
    51.             Resume Next
    52.         Case Else
    53.             Err.Raise Err.Number, Err.Source, Err.Description
    54.     End Select
    55. End Sub

    The code is a bit ugly, but it works. The only problem is there are roughly 600 cells this thing has to go thru and it take about 15-20 seconds to do so. Can anyone come up with a faster way of doing this?

    The reason I Concatinate all the cell values together is, so i can store the value in only one field in my database. The alternative would be to make 600 columns, not a very nice option.

    again, any suggestions on how to speed this up would be GREATLY appreciated.

    Scott

  2. #2
    Hyperactive Member beasty1711's Avatar
    Join Date
    Mar 2001
    Posts
    418
    excel is very slow at the best of times, i would use a VB app and stick the data into arrays

    it will run a lot quicker then
    "...They even have the internet on computers..." :- Homer Simpson

    "Second Place is First Looser" :- No Fear

  3. #3
    DerFarm
    Guest
    I'm not sure that you CAN speed the process up, except possibly
    marginally with more efficient code.

    1. You define a to be the workbook name and yet you
    use .workbook.name....... to make a comparision against "S_"

    Unless I'm mistaken, you should be using a there.

    2. Check for Stop immediately. You go through two checks prior,
    each of which costs time.

    Like I said, this won't help much, but I'm going to keep thinking.

    Interesting problem.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Posts
    306

    ugh

    The Excel Template is a Pre-existing Detailed Financial spreadsheet that has tons of info and calculations. I would REALLY REALLY hate having to recreate it in VB.

    I guess there is no way of making my way go faster. Maybe I can incrementaly save parts of it. That would help.

    Does anyone know how to only select defined Cell names from a particular Page?

    excel.application.workbook(1).Names(i).Name will get them from the entire workbook I know for sure.(if you run a loop with i)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Posts
    306

    DerFarm - Response to #1

    The Reason i need to go get the Workbook Name is that the name I defined for each cell has in it a reference to the Sheet the Cell is located on.

    VB Code:
    1. Private Function GetSheet(ByVal s As String) As String
    2. On Error GoTo Handler
    3.     Dim Field As String
    4.     Field = GetField(s)
    5.     Select Case Left(Field, InStr(Field, "_") - 1)
    6.         Case "Summary", "S"
    7.             GetSheet = "Summary"
    8.         Case "HO"
    9.             GetSheet = "Home Office Svcs. "
    10.         Case "HO1"
    11.             GetSheet = "H.O. Phase1"
    12.         Case "HO2", "NO2"
    13.             GetSheet = "H.O. Phase 2"
    14.         Case "FSS"
    15.             GetSheet = "Fld.Svcs.& Support"
    16.         Case "ME"
    17.             GetSheet = "Mat's.& Equip."
    18.         Case "C"
    19.             GetSheet = "Construction"
    20.         Case Else
    21.             'MsgBox Left(Field, InStr(Field, "_") - 1)
    22.             GetSheet = "Stop"
    23.     End Select
    24. Exit Function
    25. Handler:
    26.     Select Case Err.Number
    27.         Case 5 '-invalid procedure call or argument
    28.             Resume Next
    29.         Case Else
    30.             Err.Raise Err.Number, Err.Source, Err.Description
    31.     End Select
    32. End Function

    an example of a cell name would be: HO_ManHours

    I do it this way becuase I don't yet know how to determine the Cell location (which page it's on) thru VB yet. If it's possible.

  6. #6
    DerFarm
    Guest
    Would it be possible for you to take a screen shot of one of the
    Excell sheets you are using. I'm very optically oriented and I'm
    having difficulty "seeing" the problem.

    Maybe you could attach a spreadsheet?

  7. #7
    Member
    Join Date
    Nov 2000
    Posts
    43
    The simplest way would be to add one more sheet to excel which contains a single range name consisting of all the vaules referenced from the source sheets. This way you would be able to use column names to explain the data value and a simple way to retreive the data from VB

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Posts
    306

    RefersTo

    I just found that if you use:

    .Workbooks(1).Names(i).RefersTo


    it will give you something like this

    =HomeOffice!$G$37


    This will help me out, I wan't have to figure out which sheet it is on now.
    Attached Images Attached Images  

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Posts
    306

    CUT IN HALF

    I Re-Wrote the code to use the ReferTo property and it seems to have cut the save time in half.

    VB Code:
    1. Private Sub SaveInfo2()
    2. On Error GoTo Handler
    3.     Dim Info() As String
    4.     Dim strPricing As String, strSummary As String
    5.     Dim i As Integer
    6.        
    7.     ReDim Info(0)
    8.     With xls
    9.         '--Set intial and max value for progress bar
    10.         pbExcel.Value = 0
    11.         pbExcel.Max = .Workbooks(1).Names.Count
    12.        
    13.         For i = 1 To .Workbooks(1).Names.Count
    14.             Debug.Print .Workbooks(1).Names(i).RefersTo
    15.             'Debug.Print Replace(Left(.Workbooks(1).Names(i).RefersTo, InStr(.Workbooks(1).Names(i).RefersTo, "!") - 1), "=", "")
    16.                    
    17.             If InStr(.Workbooks(1).Names(i).RefersTo, "S_") Then
    18.                     strSummary = strSummary & Replace(.Range(Replace(.Workbooks(1).Names(i).RefersTo, "=", "")).Value & .Workbooks(1).Names(i).RefersTo, "=", "@")
    19.                 If Info(0) <> "" Then ReDim Preserve Info(UBound(Info) + 1)
    20.                 '--EX: 1.25 @ FLD_CELL
    21.                 Info(UBound(Info)) = Replace(.Range(Replace(.Workbooks(1).Names(i).RefersTo, "=", "")).Value & .Workbooks(1).Names(i).RefersTo, "=", "@")
    22.                 Debug.Print Info(UBound(Info))
    23.             End If
    24.             pbExcel.Value = i
    25.         Next i
    26.     End With
    27.    
    28.     For i = 0 To UBound(Info)
    29.         strPricing = strPricing & Info(i) & ","
    30.     Next i
    31.    
    32.     AddPricingInfo strPricing, strSummary
    33.     'Screen.MousePointer = 99
    34.     MsgBox "Saved Successfully", vbInformation
    35.    
    36. Exit Sub
    37. Handler:
    38.     Resume Next
    39.     Err.Raise Err.Number, Err.Source, Err.Description
    40. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width