Consulting

Results 1 to 16 of 16

Thread: Calculation by VBA

  1. #1
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location

    Calculation by VBA

    Question: How to calculate the means value of the Range(B4:B10) and transfer to the range in my excel file~~
    http://img519.imageshack.us/my.php?i...5760948lb8.jpg
    Also my vba command are show below:
    [VBA]Private Sub cmdAnalysis_Click()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Workbooks.Open Filename:=Me.ListBox1.List(Me.ListBox1.ListIndex)
    If ListBox2.Value = ("115 Vac 60 Hz") Then
    Range("B4:B10" / 7).Select
    Selection.Copy
    wb.Activate
    Range("c4").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
    End Sub[/VBA]
    http://img519.imageshack.us/my.php?i...1184488jr3.jpg

    Of course the Range("B4:B10" / 7) is impossible in vba. Therefore, i want to get this value, then transfer to the range in my excel file~~

    P.s. i am the beginner of the VBA~~That' why i have many questions~~Sorry

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdAnalysis_Click()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Workbooks.Open Filename:=Me.ListBox1.List(Me.ListBox1.ListIndex)
    If ListBox2.Value = ("115 Vac 60 Hz") Then
    Range("C4").Value = Application.Sum(Range("B4:B10")) / 7
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Thank you xld~~
    Also, i want to know that the result must be the integer or not??
    If i want the result should be contain 3 decimal places~~ What can i do it in VBA setting~~

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Actually, I'm dumb, should have used

    [vba]

    Range("C4").Value = Application.Average(Range("B4:B10"))
    [/vba]

    What does the other question mean?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    OK~~Thank first~
    I means that my final value by the vba calculation in the range is "9". But actutally, the correct value is "8.879" (3 decimal places). I had checked that the formula cells in excel which is either "General" or "Number". Why have this observation. Also, have any suggestion to solve its problem. Thank You~~

    P.S. You do not dumb, i think you are very smart as well.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ADd

    [vba]

    Range("C4").NumberFormat = "#,##0.000"
    [/vba]

    and see if that changes it
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Unfortunately, the result is no change~~~

    If u don't mind, i can send my file to you to check it~~

    http://download3-5.files-upload.com/...MycsvMarco.zip

    ThankS

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Says page is unavailable. Why not just post it here?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    OK~~I post here
    [VBA]Private Sub cmdOpen_Click()
    Dim sFiles As Variant
    Dim sFileShort As String
    Dim i As Long
    Dim Title As String
    Dim Finfo As String
    Dim Msg As String

    ' Setup lists of file filters
    Finfo = "Comma separated Files (*.csv),*.csv," & "All Files (*.*),*.*"

    ' Set the dialog box caption
    Title = "select a File to Import"

    sFiles = Application.GetOpenFilename(Finfo, , Title, MultiSelect:=True)

    ' Exit if dialog box canceled
    If Not IsArray(sFiles) Then
    MsgBox "No file was selected."
    Exit Sub
    End If
    'Display full path and name of the files
    For i = LBound(sFiles) To UBound(sFiles)
    sFileShort = Right(sFiles(i), Len(sFiles(i)))
    With Me.ListBox1
    .AddItem sFileShort
    .List(.ListCount - 1, 1) = sFiles(i)
    End With
    Msg = Msg & sFiles(i) & vbCrLf
    Next i
    MsgBox "You selected:" & vbCrLf & Msg

    End Sub
    Private Sub cmdDelete_Click()
    Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
    End Sub
    Private Sub cmdEnd_Click()
    Unload UserForm1
    MsgBox "Bye!!! End of the Adaptor Msg Log User Interface"
    End
    End Sub
    Private Sub cmdAnalysis_Click()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim Idc As Integer
    Dim Vdc As Integer
    Workbooks.Open Filename:=Me.ListBox1.List(Me.ListBox1.ListIndex)
    ' (i) Case for "115 Vac 60Hz"
    If ListBox2.Value = ("115 Vac 60 Hz") Then
    ' Extract the means DC Current(Idc) from this file
    Idc = Application.Average(Range("H4:H28"))
    wb.Activate
    Sheet1.Range("b4").NumberFormat = "#,##0.0000"
    Sheet1.Range("b4").Value = Idc
    ' Extract the means DC voltage (Vdc) from this file
    Workbooks.Open Filename:=Me.ListBox1.List(Me.ListBox1.ListIndex)
    Vdc = Application.Average(Range("I4:I28"))
    wb.Activate
    Sheet1.Range("C4").NumberFormat = "#,##0.0000"
    Sheet1.Range("C4").Value = Vdc
    End If
    End Sub[/VBA]

  10. #10
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Have other question is:
    [VBA]' Calculate the means Efficiency (n) in "115 Vac 60Hz/400 mAdc" Measurement
    n = Range("D9").Value / Range("E9").Value
    Sheet1.Range("F9") = n[/VBA]
    In the above case , i want the n result =n*100~~What action can be take in VBA calculation~~

    Thx~

  11. #11
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Oh.....Too Big Problem for me~~
    xld~~ Plz help me~~

    Since my supervisor said that all extract data from the workbooks are standard form. It means that All data either in row and column are the same.
    Such as this
    http://img174.imageshack.us/my.php?i...9356301tw9.jpg
    All workbooks should be the same in row and column
    However, i find that the data in row which is different in the different workbooks~~Some part may be more or less~~
    http://img403.imageshack.us/my.php?i...3398630qj7.jpg
    Therefore, i can't use the above VBA way to analysis..~~ It means that it should be write the many VBA command to Search the region in different work book to do it.
    Oh my god~~Plz help me~~

  12. #12
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    i have the idea about this but i don't know how to write in VBA~
    http://img166.imageshack.us/my.php?i...ntitledvb6.jpg
    Hope can help u

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Post the workbook here, not just the code, not just an image, but the workbook as you did on the other site.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14

  15. #15
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Attachment 6199

    Open "Marco.ver.1.4.1.xls" to get the data from the Library.xls

  16. #16
    Steve~
    U try to change "Dim Idc as Integer" to "Dim Idc as Single"~~
    It may solve the decimal places problem~~

Posting Permissions

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