-
Dec 17th, 2020, 02:52 PM
#81
Re: Copying data from different sheet to sheet 1 on Excel
i presume you mean not appear on sheet1
try changing to
Code:
For Each i In c1
Set fnd = Rng.Find(i)
If Not fnd Is Nothing Then
if not fnd = "subs" then
Set fndval = fnd.End(xlDown).Offset(-1, 7)
If fndval.Row = dt.Rows.Count - 1 Then Set fndval = fnd.Offset(dt.Rows.Count - fnd.Row, 7).End(xlUp)
vrow.Offset(nxtrw).Value = fnd & " " & fnd.Offset(, 1)
vrow.Offset(nxtrw).Offset(, 1).Resize(, 3).Value = fndval.Resize(, 3).Value
nxtrw = nxtrw + 1
end if
End If
Next
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 17th, 2020, 04:04 PM
#82
Thread Starter
Hyperactive Member
Re: Copying data from different sheet to sheet 1 on Excel
Thanks Pete yes I mean not copy across to sheet 1.
I'll try this out. Thanks again.
-
Dec 18th, 2020, 06:11 AM
#83
Thread Starter
Hyperactive Member
Re: Copying data from different sheet to sheet 1 on Excel
Thanks Pete. It is excluding the "subs" which is great but what is happening now that after the first part of the lookups between sheet 1 and 2 the code is also adding all the codes with the figures from sheet 2 again at the bottom of the other data in the variation section so getting duplicate lines.
-
Dec 18th, 2020, 02:56 PM
#84
Re: Copying data from different sheet to sheet 1 on Excel
while i did not test excluding subs, there is no way that the minor change above could cause all the items to be added again
any items found from sheet1 are added to the first collection, only items on sheet2 not already in the first collection are added to the second collection, then the items in the second collection are added to sheet1 after the items found in sheet1
this part was all tested
post the entire code as you have it now
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 23rd, 2020, 04:23 PM
#85
Thread Starter
Hyperactive Member
Re: Copying data from different sheet to sheet 1 on Excel
Hi Pete this is the bit of the code which copies the data from sheet 2 to sheet 1. It looks like all the rows are being copied over even after the initial lookup.
Code:
Edit / DeleteEdit Post Quick reply to this messageReply Reply With QuoteReply With Quote Multi-Quote This Message Nov 24th, 2020, 09:32 AM#59
westconn1 westconn1 is offline
PowerPoster
Join Date
Dec 2004
Posts
25,132
Re: Copying data from different sheet to sheet 1 on Excel
you can try this, i am not sure it has enough checks, but seems to do the basics requested for the sample data supplied, plenty of tests required
Code:
Set dept = .Sheets(1)
Set dt = .Sheets(2)
Set vrow = dept.Range("a:a").Find("GRAND TOTAL").Offset(4)
vrow.Value = "Variation"
dept.Range("b12:d13").Copy vrow.Offset(1, 1)
vrow.Offset(2, 2).Value = "Contracted"
Set vrow = vrow.Offset(4)
Set rw = dept.Cells(17, 1)
oset = 0
nxtrw = 0
Do
If IsEmpty(rw.Offset(oset)) Then Exit Do
If InStr(rw.Offset(oset), "Temp") = 0 And InStr(rw.Offset(oset), "Agency") = 0 And Not rw.Offset(oset, 1) = 0 Then
Set fnd = dt.Range("c:c").Find(Left(rw.Offset(oset), 4))
If Not fnd Is Nothing Then
Set fndval = fnd.End(xlDown).Offset(-1, 7)
If fndval.Row = dt.Rows.Count - 1 Then Set fndval = fnd.Offset(Rows.Count - 21, 7).End(xlUp)
vrow.Offset(nxtrw).Value = rw.Offset(oset).Value
vrow.Offset(nxtrw, 1).Value = Round(rw.Offset(oset, 1).Value, 2)
vrow.Offset(nxtrw, 2).Value = fndval.Value
rwcnt = vrow.Row + nxtrw
vrow.Offset(nxtrw, 3).Formula = "=sum(b" & rwcnt & "-c" & rwcnt & ")*-1"
nxtrw = nxtrw + 1
Else
If rw.Offset(oset, 2) = 0 Then vrow.Offset(nxtrw, 1).Resize(, 3).Value = rw.Offset(oset, 1).Resize(, 3): nxtrw = nxtrw + 1
End If
End If
oset = oset + 1
Loop
vrow.Offset(nxtrw + 1, 1).Formula = "=sum(" & vrow.Offset(, 1).Resize(nxtrw).Address(False, False) & ")"
With vrow.Offset(nxtrw + 1, 1).Resize(, 3)
.Formula = vrow.Offset(nxtrw + 1, 1).Formula
.Borders(3).LineStyle = 1
.Borders(3).ColorIndex = -4105
.Borders(4).LineStyle = -4119
.Borders(4).Weight = 4
.Borders(4).ColorIndex = -4105
End With
vrow.Offset(-4).Resize(nxtrw + 6, 4).Interior.Color = 12566463
.Sheets(1).Select
.Close True
End With
-
Dec 23rd, 2020, 11:00 PM
#86
Re: Copying data from different sheet to sheet 1 on Excel
this looks like from many posts back, so i have no idea what relationship this has to the current code i have just been working on
if there is an issue with the most recent code post a sample of the data that demonstrates the problem
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|