HHelp With Modifying VBA

menor59

Well-known Member
Joined
Oct 3, 2008
Messages
574
Office Version
  1. 2021
Platform
  1. Windows
Hello Team..

Found this on the Web...I have the following but i would Like to Modify It...

VBA Code:
Sub Copy_To_Another_Sheet_1()
    Dim FirstAddress As String
    Dim MyArr As Variant
    Dim Rng As Range
    Dim Rcount As Long
    Dim I As Long
    Dim NewSh As Worksheet

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the search Value
    MyArr = Range("D3").Text
    'You can also use more values in the Array
    'myArr = Array("@", "www")

    'Add new worksheet to your workbook to copy to
    'You can also use a existing sheet like this
    'Set NewSh = Sheets("Sheet2")
    Set NewSh = Worksheets.Add

    With ActiveSheet.Range("F3:K100")

        Rcount = 0

        For I = LBound(MyArr) To UBound(MyArr)

            'If you use LookIn:=xlValues it will also work with a
            'formula cell that evaluates to "@"
            'Note : I use xlPart in this example and not xlWhole
            Set Rng = .Find(What:=MyArr(I), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rcount = Rcount + 1

                    Rng.Copy NewSh.Range("A" & Rcount)

                    ' Use this if you only want to copy the value
                    ' NewSh.Range("A" & Rcount).Value = Rng.Value

                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next I
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



What I would Like to do is From the Value Of D3 on the Active Sheet, Find The Value from the Database Sheet which could be anywhere On the Database Sheet In Column B multiple times..

If Data is Found On the DataSheet Column B populate the values from C:H where to the Activesheet Range F:K (no Need to Add a New worksheet...) Just populate to the Active Sheet
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello all...Here is a Better way of what im trying to accomplish...

This is My Database that has lines B2:H2000

2022-04-12 06_30_16-VBS for Active Sheet, bring data from database Sheet _ MrExcel Message Board.jpg

This is the Active Sheet i will tie the Code to a Macro Button as "Update"

2022-04-12 06_31_03-VBS for Active Sheet, bring data from database Sheet _ MrExcel Message Board.jpg


The Last Picture is the ActiveSheet. I need to Grab Data from the Database sheet Based on the Active Sheet D3, then From the Database sheet Find the Value of D3 in Range B2:B2000 and Return the Values where it is found from C2:H2000.

D3 or the Sheet Name from the ActiveSheet can be used to search the Database for the Value. Which ever you perfer.

Thank you so much for your time!
 
Upvote 0
Maybe like this:

VBA Code:
Sub CopyToDatabase()
    Const cols = 6, strCopyTo = "Database"
    Dim city As String, origin As Range, r, db, c, i As Long, j As Long, k As Long
    Dim rw(1 To 1, 1 To cols)
    city = [d3] 'Note - these hard-coded range addresses will cause trouble if you insert rows or columns before them
    r = [f3:k2001]
    With Sheets(strCopyTo)
        db = .[b2:b2000]
        Set origin = .[c2]
    End With
    k = 0
    j = 1
    For Each c In db
        If c = city Then
            For i = 1 To cols
                rw(1, i) = r(j, i)
            Next i
            j = j + 1
            origin.Offset(k).Resize(, cols) = rw
        End If
        k = k + 1
    Next
End Sub
 
Upvote 0
Try this:
My sheet has a sheet named "DataBase"
And a sheet named "Alpha"
I do not like using Active Sheet
Modify sheet name to your liking:
VBA Code:
Sub Check_For_Data()
'Modified  4/12/2022  12:59:59 PM  EDT
Application.ScreenUpdating = False
Dim ans As String
ans = Sheets("Alpha").Range("D3").Value
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("DataBase").Cells(Rows.Count, "B").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = 3

For i = 1 To Lastrow
    If Sheets("DataBase").Cells(i, 2).Value = ans Then
        Sheets("DataBase").Cells(i, 3).Resize(, 6).Copy Sheets("Alpha").Cells(Lastrowa, 6)
        Lastrowa = Lastrowa + 1
    End If
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
GOod Morning Guys.

Found this....CHeck this out..

Code:
=FILTER(Database!$C$2:$H$2000,Database!$B$2:$B$2000=$D$3)

I just pasted this into F3 on every sheet..and voila!!
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top