+ Reply to Thread
Results 1 to 2 of 2

Help with code; write over if cells match

Hybrid View

  1. #1
    Registered User
    Join Date
    06-07-2011
    Location
    Göteborg, Sweden
    MS-Off Ver
    Excel 2003
    Posts
    14

    Help with code; write over if cells match

    I have an excel code that exports a row of data to another workbook. The exported data is added in the end of the table. This is done with the following code:

    Private Sub CommandButton1_Click()
        Range("B46:R46").Select
        Selection.Copy
        Workbooks.Open ("G:\VFast.Temporar\3 Göteborg\01 Gemensamt\!NY_Mappstruktur_Gbg\Vfast.o35.Gbg.FF-PL\04_Personal\10_Sommarjobb\2011\Analys\Jämförelse\Projektsammanställning.xls")
        Windows("Projektsammanställning.xls").Activate
        Workbooks("Projektsammanställning.xls").Sheets("Blad1").Range("B2").Select
        ActiveCell.Offset(1, 0).Select
        
        
        Do While Not IsEmpty(ActiveCell)
            ActiveCell.Offset(1, 0).Select
        Loop
    
        ActiveSheet.Paste
        Windows("Projektsammanställning.xls").Activate
        Workbooks("Projektsammanställning.xls").Sheets("Blad1").Range("A1").Select
    End Sub
    I want add a feature that checks if the data in B42 (from the workbook i want to export from) matches a cell in row B in the workbook I want to export to. If this is done, I want it replace that row with the exported row, if not, I want it to use the original code and place it in the end.

    /danpo

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Help with code; write over if cells match

    OK, how about:

    Private Sub CommandButton1_Click()
    
        Const lLEFT_COL=2
        Const lRIGHT_COL=18
        Const lSOURCE_ROW=46
    
        Dim vCheckValue as Variant
        Dim wbkTargetBook As Workbook
        Dim wshSourceSheet As Worksheet
        Dim wshTargetSheet As Worksheet
        Dim rngMatchValue As Range
        Dim lWriteRow As Long
    
        Set wshSourceSheet=ActiveSheet
        vCheckValue=wshSourceSheet.Cells(42,2).Value
    
        Set wbkTargetBook = Workbooks.Open ("G:\VFast.Temporar\3 Göteborg\01 Gemensamt\!NY_Mappstruktur_Gbg\Vfast.o35.Gbg.FF-PL\04_Personal\10_Sommarjobb\2011\Analys\Jämförelse\Projektsammanställning.xls")
        Set wshTargetSheet= wbkTargetBook.Sheets("Blad1")
    
        With wshTargetSheet
            lWriteRow=.Cells(.Rows.Count, lLEFT_COL).End(xlUp).Row
    
           Set rngMatchValue=.Columns(2).Find(vCheckValue, Lookin:=xlValues, Lookat:=xlWhole)
           If Not rngMatchValue Is Nothing Then
              lWriteRow=rngMatchValue.Row
           End If
    
           .Range(.Cells(lWriteRow,lLEFT_COL), .Cells(lWriteRow, lRIGHT_COL)).Value=wshSourceSheet.Range(wshSourceSheet.Cells(lSOURCE_ROW,lLEFT_COL),wshSourceSheet.Cells(lSOURCE_ROW,lRIGHT_COL)).Value
    
        End With
        
        wshTargetSheet.Cells(1,1).Select
    
    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