retrieve data from database

RawanF

New Member
Joined
May 2, 2017
Messages
2
hello everyone ,


I have an assigment for work and Its basically a code to check every row in a database that match three cratiria which will be defined by the user
name, month and year ,, it has to work through a huge database and then retrive the entire row , I got error in the IF line


Code:
    Dim db As Worksheet
    Dim inp As Worksheet
    Dim i As Long
    Dim lr As Long
    Dim Lrow As Long
    Set db = Sheets("DB")
    Set inp = Sheets("Input")
    Set r = db.Range("B:N")
    lr = Sheets("DB").Cells(Rows.Count, "c").End(xlUp).Row + 1
    i = 2
    Sheets("DB").Activate
    For i = 2 To lr
        If inp.Cells("K11").Value = db.Cells("d" & i).Value And inp.Cells("L11").Value = db.Cells("I" & i).Value And inp.Cells("M11").Value = db.Cells("J" & i).Value Then
            db.Range("B" & i & " :N" & i).Copy Sheets("rtv").Range("a" & i).pastevalues
        End If
        i = i + 1
    Next
    Lrow = Sheets("rtv").Cells(Row.Count, "a").End(xlUp).Row + 1
    Sheets("rtv").Range("a2:" & Lrow).Copy inp.Cells("c15").pastevalues

please help
thank you in advance
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Re: retrive data from database

Welcome to MrExcel,

This syntax is incorrect to reference a cell:
Code:
  .Cells("K11")

Correct options include:
Code:
  .Cells(11, "K")
Code:
  .Range("K11")
 
Upvote 0
Re: retrive data from database

Welcome to MrExcel,

This syntax is incorrect to reference a cell:
Code:
  .Cells("K11")

Correct options include:
Code:
  .Cells(11, "K")
Code:
  .Range("K11")

Thank you , but it still shows me an error when I want to paste it in destination :(
 
Upvote 0
Re: retrive data from database

Hi,
as already pointed out by Jerry, you have some syntax errors in your coding.

not tested but see if update to your code helps:


Code:
Dim db As Worksheet
    Dim inp As Worksheet
    Dim i As Long
    Dim lr As Long
    Dim Lrow As Long
    
    Set db = Sheets("DB")
    Set inp = Sheets("Input")
    Set r = db.Range("B:N")
    
    lr = Sheets("DB").Cells(Sheets("DB").Rows.Count, "c").End(xlUp).Row + 1
    i = 2
    Sheets("DB").Activate
    For i = 2 To lr
        If inp.Range("K11").Value = db.Cells(i, "d").Value And _
            inp.Range("L11").Value = db.Cells(i, "I").Value And _
            inp.Range("M11").Value = db.Cells(i, "J").Value Then
            db.Range("B" & i & ":N" & i).Copy
            Sheets("rtv").Range("a" & i).PasteSpecial xlPasteValues
        End If
    i = i + 1
'clear clipboard
    Application.CutCopyMode = False
    Next


    Lrow = Sheets("rtv").Cells(Sheets("rtv").Rows.Count, "A").End(xlUp).Row + 1
    Sheets("rtv").Range("a2:a" & Lrow).Copy
    inp.Range("c15").PasteSpecial xlPasteValues
    Application.CutCopyMode = False


Dave
 
Upvote 0

Forum statistics

Threads
1,223,995
Messages
6,175,850
Members
452,675
Latest member
duongtruc1610

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