We help IT Professionals succeed at work.

Moving Data from one WS to another

Bright01
Bright01 asked
on
EE Pros,

I have a Workbook with 3 Worksheets.  I'm trying to select an Industry on the Splash Screen, have it match up to a range, and then place the new data from a DB (where the range is named), and place it in the third Worksheet.

Please take a look at the sample Attached.

Thank you in advance.

B.
Matching-up-Data-from-a-DB.xlsm
Comment
Watch Question

Top Expert 2015

Commented:
You can use the following code to do what you are looking for...

Sub copydata()

    Dim rng As Range
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
    Dim str As String

    Set ws = Sheets("Splash")
    Set ws1 = Sheets("Database")
    Set ws2 = Sheets("Cases")

    str = Application.WorksheetFunction.VLookup(ws.Range("n16").Value, ws.Range("X8:Y10"), 2, 0)

    Set rng = ws1.Range(str)

    rng.Copy ws2.Range("G5")

End Sub

Open in new window


Saurabh...

Author

Commented:
Saurabh,

Thanks for jumping in....however, I don't believe that will work as a automatic macro.  If you look at the code I provided, you will see it looks for a change in the cell N16 for a particular Industry that then triggers the move.

Please take a relook.

Thank you,

B.
Top Expert 2015
Commented:
Replace your current splash code which you have in that module with this... This will do what you are looking for...

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
  
        Dim r As Long
        Dim rng As Range
        Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
        Dim str As String

        If Target.Address = "$N$16" And Trim(Target.Value) <> "" Then
            On Error Resume Next

            Set ws = Sheets("Splash")
            Set ws1 = Sheets("Database")
            Set ws2 = Sheets("Cases")

            str = Application.WorksheetFunction.VLookup(ws.Range("n16").Value, ws.Range("X8:Y10"), 2, 0)

            Set rng = ws1.Range(str)

            rng.Copy ws2.Range("G5")

        End If
  
End Sub

Open in new window


Saurabh...

Author

Commented:
You nailed it!  Thanks very much..... excellent work...and fast.

B.