Results 1 to 15 of 15

Thread: join models

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    I'd open the 'database' workbook, copy the From/To worksheet into the Input workbook, do the replaces, and then delete the 'database' sheet

    I think it's cleaner and less confusing that way


    This code is in the 'Input' workbook, and the 'database' workbook has no macros so it's a XLSX


    Option Explicit
    
    Sub Replaces()
        Dim wbData As Workbook
        
        Application.ScreenUpdating = False
        
        'delete Data is if still exists
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("Data").Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        'open Replaces workbook and copy data sheet in
        Workbooks.Open Filename:=Environ("USERPROFILE") & "\Desktop\ReplaceData.xlsx"   '   <<<<<<<<<<<<<<<<<
        Set wbData = ActiveWorkbook
        wbData.Worksheets("Data").Copy Before:=ThisWorkbook.Worksheets(1)
        wbData.Close False
        
        ThisWorkbook.Activate
        
        'do the replaces
        Call ReplaceAllSheets(Worksheets("Data").Range("A1"))
        Call ReplaceAllSheets(Worksheets("Data").Range("D1"))
        Call ReplaceAllSheets(Worksheets("Data").Range("G1"))
        'get rid of Data
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("Data").Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        Application.ScreenUpdating = True
    End Sub
    
    
    'this sub is Private so that it's only usable in this module
    Private Sub ReplaceAllSheets(R As Range)
        Dim i As Long
        Dim ws As Worksheet
        Dim r1 As Range
        
        Set r1 = R.CurrentRegion
        
        If r1.Rows.Count < 2 Then Exit Sub
        
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name = "Data" Then GoTo GetNextSheet
            If ws.UsedRange.Cells.Count < 2 Then GoTo GetNextSheet
            
            For i = 2 To r1.Rows.Count
                ws.UsedRange.Cells.Replace What:=r1.Cells(i, 1).Value, Replacement:=r1.Cells(i, 2).Value, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                    SearchFormat:=False, ReplaceFormat:=False
            Next i
    GetNextSheet:
        Next
    End Sub
    
    
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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