+ Reply to Thread
Results 1 to 2 of 2

Need a macro to subtract cell values of 2 separate workbooks.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-24-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    1

    Need a macro to subtract cell values of 2 separate workbooks.

    Hi,

    Not sure if it's possible to create a macro like this, but i wanted to ask anyway.

    I have 2 workbooks.
    Workbook 1: Has a table of cell values (with 8 rows and 4 columns in cell range B2:E10)
    Workbook 2: Has a similar table of cell values (also with 8 rows and 4 columns in cell range C15:F22)

    Now, i want to create a macro which will subtract all the cell values in workbook 1 with the corresponding cell value in workbook 2. For example, I want to do the calculations B2-C15, C2-D15, B3-C16 etc.

    I would really appreciate any help, thanks!

  2. #2
    Registered User
    Join Date
    01-21-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Need a macro to subtract cell values of 2 separate workbooks.

    Sub test()
    
    Dim LastRow1 As Long
    Dim LastRow2 As Long
    
    
    LastRow1 = Worksheets(1).Range("B" & Rows.Count).End(xlUp).Row
    LastRow2 = Worksheets(2).Range("C" & Rows.Count).End(xlUp).Row
    
    
    For i = 2 To LastRow1
    For r = 15 To LastRow2
    
    
        If IsEmpty(Worksheets(1).Cells(i, 2)) = False Then
            
            Worksheets(1).Cells(r, 2) = Worksheets(1).Range("B" & i) - Worksheets(2).Range("C" & r)
            
        End If
        
        If IsEmpty(Worksheets(1).Cells(i, 3)) = False Then
            
            Worksheets(1).Cells(r, 3) = Worksheets(1).Range("C" & i) - Worksheets(2).Range("D" & r)
            
        End If
        
        If IsEmpty(Worksheets(1).Cells(i, 4)) = False Then
            
            Worksheets(1).Cells(r, 4) = Worksheets(1).Range("D" & i) - Worksheets(2).Range("E" & r)
            
        End If
        
        If IsEmpty(Worksheets(1).Cells(i, 5)) = False Then
            
            Worksheets(1).Cells(r, 5) = Worksheets(1).Range("E" & i) - Worksheets(2).Range("F" & r)
            
        End If
        
    Next
    Next
    
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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