Hi everyone
I am having problems getting some code to work in one of my work sheets and would appreciate if anyone could point me in the right direction i have tried several things to alter the code but can still only manage to get it to work on 1 sheet only at a time. What i am trying (very poorly!) to do is:
When I click a “GetData” button on worksheet “DataSheet” I want is it to lookup and copy data from 5 work sheets (sheet2,sheet3,sheet4,sheet5,sheet6) and copy, from row 3 the range A:K until it comes across an empty cell then I want it to move on to the next worksheet and lookup that sheet copy the data and add it to the bottom of my “DataSheet” sheet I want this to loop until it has got the data from all five worksheets and added then to the “DataSheet” . I have managed to get it to work for one sheet but as soon as I try to alter the code to add another sheet it goes wrong! what I have so far is:
Any advice would be great
Cheers
Jackie
I am having problems getting some code to work in one of my work sheets and would appreciate if anyone could point me in the right direction i have tried several things to alter the code but can still only manage to get it to work on 1 sheet only at a time. What i am trying (very poorly!) to do is:
When I click a “GetData” button on worksheet “DataSheet” I want is it to lookup and copy data from 5 work sheets (sheet2,sheet3,sheet4,sheet5,sheet6) and copy, from row 3 the range A:K until it comes across an empty cell then I want it to move on to the next worksheet and lookup that sheet copy the data and add it to the bottom of my “DataSheet” sheet I want this to loop until it has got the data from all five worksheets and added then to the “DataSheet” . I have managed to get it to work for one sheet but as soon as I try to alter the code to add another sheet it goes wrong! what I have so far is:
Code:
Sub GetData()
Dim x As Integer
Dim y As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim z As Long
Set ws1 = Worksheets("DataSheet") 'whatever you worksheet is
Set ws2 = Worksheets("sheet2") 'or whatever your worksheet is called
Set ws3 = Worksheets("sheet3") 'or whatever your worksheet is called
Set ws4 = Worksheets("sheet4") 'or whatever your worksheet is called
Set ws5 = Worksheets("sheet5") 'or whatever your worksheet is called
Set ws6 = Worksheets("sheet6") 'or whatever your worksheet is called
r = 3 'this is the first row where your data will output
x = 3 'this is the first row where you want to check for data
z = ws1.Range("A65536").End(xlUp).Offset(1, 0).Select
Do Until ws2.Range("H" & x) = ""
If Not ws2.Range("H" & x).Value = "" Then
ws1.Range("A" & r).Value = ws2.Range("A" & x).Value 'Type
ws1.Range("B" & r).Value = ws2.Range("B" & x).Value 'Account
ws1.Range("C" & r).Value = ws2.Range("C" & x).Value 'Nominal
ws1.Range("D" & r).Value = ws2.Range("D" & x).Value 'Details
ws1.Range("E" & r).Value = ws2.Range("E" & x).Value 'Date
ws1.Range("F" & r).Value = ws2.Range("F" & x).Value 'Ref
ws1.Range("G" & r).Value = ws2.Range("G" & x).Value 'Ex. Ref
ws1.Range("H" & r).Value = ws2.Range("H" & x).Value 'Gross
ws1.Range("I" & r).Value = ws2.Range("I" & x).Value 'Net
ws1.Range("J" & r).Value = ws2.Range("J" & x).Value 'Vat
ws1.Range("K" & r).Value = ws2.Range("K" & x).Value 'Tax Code
ws1.Range("A" & r).Value = ws3.Range("A" & z).Value 'Type
ws1.Range("B" & r).Value = ws3.Range("B" & z).Value 'Account
ws1.Range("C" & r).Value = ws3.Range("C" & z).Value 'Nominal
ws1.Range("D" & r).Value = ws3.Range("D" & z).Value 'Details
ws1.Range("E" & r).Value = ws3.Range("E" & z).Value 'Date
ws1.Range("F" & r).Value = ws3.Range("F" & z).Value 'Ref
ws1.Range("G" & r).Value = ws3.Range("G" & z).Value 'Ex. Ref
ws1.Range("H" & r).Value = ws3.Range("H" & z).Value 'Gross
ws1.Range("I" & r).Value = ws3.Range("I" & z).Value 'Net
ws1.Range("J" & r).Value = ws3.Range("J" & z).Value 'Vat
ws1.Range("K" & r).Value = ws3.Range("K" & z).Value 'Tax Code
r = r + 1
x = x + 1
Else
x = x + 1
End If
Loop
End Sub
Cheers
Jackie