+ Reply to Thread
Results 1 to 2 of 2

Using macro generated data in general excel formulas

Hybrid View

  1. #1
    Registered User
    Join Date
    05-02-2012
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    1

    Using macro generated data in general excel formulas

    Ok, a bit hard to explain this, hope someone understand...

    I'm already using a macro to merge data from different Workbooks in a specific folder (same folder as the excel file using the macro is saved). Look further down for the macro I'm currently using.
    The data that is imported to the excel file "holding" the macro is gonna be used as a database. I've tried using different general excel formulas (e.g. Vlookup, SUMIF, SUMIFS), which searching the database built up by using the macro. So far, so good..

    The problem occure when I run the macro again, with a new set of data. The formulas result doesn't change. I guess they don't act dynamically.

    To be a little more specific about what I'm trying to aim (please look at the attachment file):

    In Sheet 2 column 'A' there are different project numbers (e.g. 101323). In Column 'C' (Sheet 2) I would get the total sum from Column 'L' in Sheet 1, based on the following criterium: project number, value in column 'A' in Sheet 2 are > 3000 and < 4000. In the attched example the result would have been 533653.

    I know that I can use the SUMIFS function to solv this, but it doesn't seem to work when the data is generated by a macro. And as metioned already - a new set of data imported doesn't effect the formulas outcome. E.g. say the new set of data doesn't contain the project number "101323", the SUMIFS formlua won't return 0, but still 533653.

    I should also mentioned that the project numers are dynamic, since they are linked to numbers in Sheet 3, column B (but I guess this would not be a problem).

    Do I need a macro to solv this or do I just have to modify the macro I'm already using? Or something else... please help!


    Here's the macro I'm using:

    Private Declare Function SetCurrentDirectoryA Lib _
        "kernel32" (ByVal lpPathName As String) As Long
    
    Sub ChDirNet(szPath As String)
        SetCurrentDirectoryA szPath
    End Sub
    
    Sub Combine_Workbooks_Select_Files()
        Dim MyPath As String
        Dim SourceRcount As Long, Fnum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
        Dim SaveDriveDir As String
        Dim FName As Variant
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        SaveDriveDir = CurDir
        ChDirNet ThisWorkbook.Path & Application.PathSeparator
    
        FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
                                            MultiSelect:=True)
        If IsArray(FName) Then
            Set BaseWks = ActiveWorkbook.Worksheets(1)
            rnum = 1
            For Fnum = LBound(FName) To UBound(FName)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(FName(Fnum))
                On Error GoTo 0
                If Not mybook Is Nothing Then
                    On Error Resume Next
                    With mybook.Worksheets(12)
                        Set sourceRange = .Range("B21:M142")
                    End With
                    If Err.Number > 0 Then
                        Err.Clear
                        Set sourceRange = Nothing
                    Else
            If sourceRange.Columns.count >= BaseWks.Columns.count Then
                            Set sourceRange = Nothing
                        End If
                    End If
                    On Error GoTo 0
    
                    If Not sourceRange Is Nothing Then
    
                        SourceRcount = sourceRange.Rows.count
    
                        If rnum + SourceRcount >= BaseWks.Rows.count Then
                            MsgBox "Not enough rows in the sheet. "
                            BaseWks.Columns.AutoFit
                            mybook.Close savechanges:=False
                            GoTo ExitTheSub
                        Else
                            Set destrange = BaseWks.Range("A" & rnum)
                            With sourceRange
                                Set destrange = destrange. _
                                                Resize(.Rows.count, .Columns.count)
                            End With
                            destrange.Value = sourceRange.Value
    
                            rnum = rnum + SourceRcount
                        End If
                    End If
                    mybook.Close savechanges:=False
                End If
            Next Fnum
            BaseWks.Columns.AutoFit
        End If
    ExitTheSub:
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
        ChDirNet SaveDriveDir
    End Sub
    Attached Files Attached Files
    Last edited by joesta; 05-05-2012 at 06:36 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937

    Re: Using macro generated data in general excel formulas

    Please read and adhere to these simple rules!

    #3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing.

    Select your code and click the [#] button at the top of the post window

    * (if you are editing an existing post, press Go Advanced to see the [#] button).

    The result will appear like this in the editing window:

    [code]
    your code here ...
    and here ...
    and here
    [/code]

    It will appear like this when posted:

    your code here ...
    and here ...
    and here
    You can also type the code tags in manually if you prefer.
    Please wrap each Sub..End Sub with a separate [code]-[/code] pair.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1