Range Multiplication VB.NET (What is wrong with this code?)

有些话、适合烂在心里 提交于 2019-12-12 17:14:47

问题


(VB Express Level: Beginner)

I want to do following,

A column from Workbook 1
a

b

c

d

A column from Workbook2
e

f

g

h

Output to a single cell

ae+bf+cg+dh

(The output is a Sumproduct.)

There are 44 rows in workbook 1 and 44 rows in workbook 2. But there are 3 columns in workbook 1 and 104 columns in workbook 2. Each column in workbook 3 must be multiplied with 104 columns from workbook 2.

Following is my effort, which writes sames values in all the cells of a column. My understanding is my for loop is wrong somewhere. But I am not able to figure out what is wrong.

'link to workbooks
oWB5 = oXL5.Workbooks.Open("D:\1.xlsx")
oWB6 = oXL6.Workbooks.Open("D:\2.xlsx")
oWB7 = oXL7.Workbooks.Open("D:\outputs.xlsx")

'link to worksheets
oSheet5 = oWB5.Worksheets("Inputs")
oSheet6 = oWB6.Worksheets("Coef")
oSheet7 = oWB7.Worksheets("Sheet1")

' ranges to be considerd,
' oWB5 range C22 to C66
' oWB6 range C3 to C47
' oWB7 range C2 to C104 (because there are 104 columns in oWB6) 

'multiplication of ranges

For j = 3 To 47
For i = 2 to 104
For k = 2 to 4 
For l = 22 to 66
oSheet7.Cells(i, k).Value = (oSheet5.Cells(l, k).value * oSheet6.Cells(j, i+1).value) + (oSheet5.Cells(l+1, k).value * oSheet6.Cells(j + 1, i+1).value)
Next
Next
Next
Next 

Help will be really appreciated. I am struggling with this for two days now.


回答1:


Here is a simple method using the Excel's SUMPRODUCT formula. This way you let Excel do the dirty work for you. The advantage of this is that it saves you a lot of looping :)

TRIED AND TESTED

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim xlApp As New Excel.Application    
        Dim oWB5 As Excel.Workbook = xlApp.Workbooks.Open("C:\1.xlsx")
        Dim oWB6 As Excel.Workbook = xlApp.Workbooks.Open("C:\2.xlsx")
        Dim oWB7 As Excel.Workbook = xlApp.Workbooks.Open("C:\outputs.xlsx")    
        Dim oSheet7 As Excel.Worksheet = oWB7.Worksheets("Sheet1")

        With oSheet7
            For i = 1 To 8
                For j = 1 To 104
                    Dim Col1Nm As String = Split(.Cells(, j).Address, "$")(1)
                    Dim Col2NM As String = Split(.Cells(, i).Address, "$")(1)
                    .Cells(i, j).Value = xlApp.Evaluate("=SUMPRODUCT(([1]Inputs!" & Col1Nm & "1:" & _
                    Col1Nm & "44)*([2]Coef!" & Col2NM & "1:" & Col2NM & "44))")
                Next
            Next    
        End With

        '~~> Close workbook and quit Excel
        oWB5.Close (False)
        oWB6.Close (False)
        oWB7.Close (True)

        xlApp.Quit()

        '~~> Clean Up
        releaseObject (oWB5)
        releaseObject (oWB6)
        releaseObject (oWB7)
        releaseObject (xlApp)

        MessageBox.Show("done")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

IMP NOTE: If your actual file names are not 1.xlsx and 2.xlsx then change this part of the code

xlApp.Evaluate("=SUMPRODUCT(([1]Inputs!" & Col1Nm & "1:" & _
Col1Nm & "44)*([2]Coef!" & Col2NM & "1:" & Col2NM & "44))")

Replace

  1. [1] by [Actual File Name]
  2. [2] by [Actual File Name]
  3. Inputs by [Actual Sheet Name] and
  4. Coef by [Actual Sheet Name]



回答2:


Thanks for re-posting this question with more detail. I've written a loop bellow with comments that i believe achieves what you're looking to do.

   'link to workbooks
   oWB5 = oXL5.Workbooks.Open("D:\1.xlsx")
   oWB6 = oXL6.Workbooks.Open("D:\2.xlsx")
   oWB7 = oXL7.Workbooks.Open("D:\outputs.xlsx")

   'link to worksheets
   oSheet5 = oWB5.Worksheets("Inputs")
   oSheet6 = oWB6.Worksheets("Coef")
   osheet7 = oWB7.Worksheets("Sheet1")

   ' ranges to be considerd,
   ' oWB5 range C22 to C66
   ' oWB6 range C3 to C47
   ' oWB7 range C2 to C104 (because there are 104 columns in oWB6)

   'multiplication of ranges

   Dim lngOutputCounter As Long

   For i = 1 To 104 'for each column WB6
       For j = 1 To 3 'for each column WB5
           lngOutputCounter = 0 'reset to 0
           For k = 1 To 44 'loop through 44 rows
               'multiply the two cells and keep a running total
               '- 21+k to start at row 22, 2+k to start at row 3, 2+j and 2+i because        columns start at C
               lngOutputCounter = lngOutputCounter + oSheet5.Cells(21 + k, 2 + i).Value * oSheet6.Cells(2 + k, 2 + j).Value
           Next k

           'whatever column sheet2 was on becomes row here, j is still the same column        (1-3)
           '- i+i to output to row 2 first, 2+j to output to column C first
           osheet7.Cells(1 + i, 2 + j).Value = lngOutputCounter

       Next j
   Next i


来源:https://stackoverflow.com/questions/11385455/range-multiplication-vb-net-what-is-wrong-with-this-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!