Vba with offset copy and past

bboysen

New Member
Joined
Aug 6, 2010
Messages
44
Hello all.

I am having a problem with my code.
Here is what i am trying to do.

Lets say i have data in rows R1C1, R3C1 AND R5C1. I have hidden rows 2,4. Now I want to copy the info from R1C1 to R3C2. My problem happens with the hidden rows. It will copy with the offset of (0, -1) from the selected cell of R3C2, but will paste it in R2C2 when I use the offset of (1, 0).

I have looked all over the internet and know I am missing one little thing but can't seem to pinpoint the problem.

I have add my simple code to this post please let me know where I have gone wrong!

Thanks

Code:
Sub OFFSETCOPY()'
'
'SELECT AND COPY ONE C UP AND ONE R LEFT OF CURRENT SELECTION
ActiveCell.Offset(0, -1).Select
    Selection.Copy
    Do
ActiveCell.Offset(1, 0).Select
     Loop While ActiveCell.EntireRow.Hidden = True
'SELECT AND PAST ONE C DOWN AND ONE R RIGHT OF CURRENT SELECTION
    ActiveSheet.PASTE
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Your example would probably give you an error message since you are trying to offset from column A to the left. But I think I understand what you are saying. Excel still sees the rows and columns although the user may have hidden them.

With VBA you need to tell Excel to ignore the hidden rows or columns. Try this modified version. It assumes that your worksheet is sheet1. If not, make the appropriate change to the With statement.

Code:
Sub OFFSETCOPY2()'
'SELECT AND COPY ONE C UP AND ONE R LEFT OF CURRENT SELECTION
With Sheets(1).SpecialCells(xlCellTypeVisible)
ActiveCell.Offset(0, -1).Select
Selection.Copy
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True
'SELECT AND PAST ONE C DOWN AND ONE R RIGHT OF CURRENT SELECTION
ActiveSheet.Paste
End With
End Sub
Code:
 
Upvote 0
Thanks that worked had to make a few tweaks to it but got it to go!

Do you think you could help me with an if statement for it to work only on rows that contain the word weld?

What I have is this:

Excel 2010
ABCDEFGHIJKLM
Point IDNorthingEastingElevationFeature CodeLatitude (Local)Longitude (Local)Attributes 1Attributes 2Attributes 3Attributes 4Attributes 5Attributes 6
LE1168-1F40520
WELDF2ML-7681016-91168-1R42324
WELDF2ML-7691036-91016-9F40320
WELDF2ML-7701018-91036-9F40319
WELDF2ML-7711018-9B1018-9F40319
WELDF2ML-7721013-81018-9BR42247
WELDF2ML-7731013-8B1013-8R42247
WELDF2ML-7741013-8D1013-8BR42247
WELDF2ML-7751015-51013-8DR42324
WELDF2ML-7761082-41015-5F40521
WELDF2ML-7771095-81082-4F40522
WELDA2C2F2ML-778NW1095-8B1095-8F40522
WELDF2ML-7791000-41095-8BR42245
WELDF2ML-780993-61000-4R42243
WELDF2ML-7811021-2993-6R42248
WELDF2ML-782369-51021-2R43792
WELDF2ML-783375-3369-5R43792
WELDA2C2F2ML-784NW333-6375-3F41756
WELDF2ML-785372-5333-6R43792
WELDF2ML-786372-2372-5R43792
WELDF2ML-787363-3372-2R43792
WELDF2ML-788362-4363-3R43792
WELDF2ML-789328-8362-4F41756
WELDF2ML-790346-6328-8F41757
WELDC2F2ML-791R374-4346-6R43792
WELDF2ML-792361-5374-4R43792
LE361-5R43792

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]206146[/TD]
[TD="align: right"]16716330.99[/TD]
[TD="align: right"]1830753.758[/TD]
[TD="align: right"]3313.547[/TD]

[TD="align: right"]46.00745[/TD]
[TD="align: right"]-104.25067[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]206149[/TD]
[TD="align: right"]16716260.91[/TD]
[TD="align: right"]1830751.853[/TD]
[TD="align: right"]3316.922[/TD]

[TD="align: right"]46.00726[/TD]
[TD="align: right"]-104.25068[/TD]

[TD="align: right"]28.3[/TD]

[TD="align: right"]1016[/TD]

[TD="align: center"]8[/TD]
[TD="align: right"]206152[/TD]
[TD="align: right"]16716232.59[/TD]
[TD="align: right"]1830751.318[/TD]
[TD="align: right"]3318.787[/TD]

[TD="align: right"]46.00718[/TD]
[TD="align: right"]-104.25068[/TD]

[TD="align: right"]45.9[/TD]

[TD="align: right"]1036[/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]206158[/TD]
[TD="align: right"]16716187.53[/TD]
[TD="align: right"]1830750.718[/TD]
[TD="align: right"]3323.865[/TD]

[TD="align: right"]46.00705[/TD]
[TD="align: right"]-104.25068[/TD]

[TD="align: right"]21.2[/TD]

[TD="align: right"]1018[/TD]

[TD="align: center"]16[/TD]
[TD="align: right"]206159[/TD]
[TD="align: right"]16716166.38[/TD]
[TD="align: right"]1830750.383[/TD]
[TD="align: right"]3325.947[/TD]

[TD="align: right"]46.007[/TD]
[TD="align: right"]-104.25068[/TD]

[TD="align: right"]20.9[/TD]

[TD="align: right"]1018[/TD]

[TD="align: center"]19[/TD]
[TD="align: right"]206163[/TD]
[TD="align: right"]16716145.48[/TD]
[TD="align: right"]1830749.979[/TD]
[TD="align: right"]3327.325[/TD]

[TD="align: right"]46.00694[/TD]
[TD="align: right"]-104.25069[/TD]

[TD="align: right"]18.3[/TD]

[TD="align: right"]1013[/TD]

[TD="align: center"]20[/TD]
[TD="align: right"]206164[/TD]
[TD="align: right"]16716127.34[/TD]
[TD="align: right"]1830749.669[/TD]
[TD="align: right"]3328.03[/TD]

[TD="align: right"]46.00689[/TD]
[TD="align: right"]-104.25069[/TD]

[TD="align: right"]18[/TD]

[TD="align: right"]1013[/TD]

[TD="align: center"]22[/TD]
[TD="align: right"]206165[/TD]
[TD="align: right"]16716109.41[/TD]
[TD="align: right"]1830749.132[/TD]
[TD="align: right"]3328.651[/TD]

[TD="align: right"]46.00684[/TD]
[TD="align: right"]-104.25069[/TD]

[TD="align: right"]9[/TD]

[TD="align: right"]1013[/TD]

[TD="align: center"]23[/TD]
[TD="align: right"]206167[/TD]
[TD="align: right"]16716100.49[/TD]
[TD="align: right"]1830748.986[/TD]
[TD="align: right"]3328.894[/TD]

[TD="align: right"]46.00682[/TD]
[TD="align: right"]-104.25069[/TD]

[TD="align: right"]49[/TD]

[TD="align: right"]1015[/TD]

[TD="align: center"]24[/TD]
[TD="align: right"]206168[/TD]
[TD="align: right"]16716051.53[/TD]
[TD="align: right"]1830747.844[/TD]
[TD="align: right"]3329.948[/TD]

[TD="align: right"]46.00668[/TD]
[TD="align: right"]-104.2507[/TD]

[TD="align: right"]52.8[/TD]

[TD="align: right"]1082[/TD]

[TD="align: center"]26[/TD]
[TD="align: right"]206169[/TD]
[TD="align: right"]16715998.72[/TD]
[TD="align: right"]1830746.932[/TD]
[TD="align: right"]3331.266[/TD]

[TD="align: right"]46.00654[/TD]
[TD="align: right"]-104.2507[/TD]

[TD="align: right"]21.2[/TD]

[TD="align: right"]1095[/TD]

[TD="align: center"]27[/TD]
[TD="align: right"]206171[/TD]
[TD="align: right"]16715977.65[/TD]
[TD="align: right"]1830746.69[/TD]
[TD="align: right"]3331.822[/TD]

[TD="align: right"]46.00648[/TD]
[TD="align: right"]-104.25071[/TD]

[TD="align: right"]37[/TD]

[TD="align: right"]1095[/TD]

[TD="align: center"]28[/TD]
[TD="align: right"]206172[/TD]
[TD="align: right"]16715940.71[/TD]
[TD="align: right"]1830745.968[/TD]
[TD="align: right"]3332.923[/TD]

[TD="align: right"]46.00638[/TD]
[TD="align: right"]-104.25071[/TD]

[TD="align: right"]75.3[/TD]

[TD="align: right"]1000[/TD]

[TD="align: center"]30[/TD]
[TD="align: right"]206173[/TD]
[TD="align: right"]16715865.49[/TD]
[TD="align: right"]1830744.74[/TD]
[TD="align: right"]3336.127[/TD]

[TD="align: right"]46.00617[/TD]
[TD="align: right"]-104.25072[/TD]

[TD="align: right"]75.7[/TD]

[TD="align: right"]993[/TD]

[TD="align: center"]31[/TD]
[TD="align: right"]206175[/TD]
[TD="align: right"]16715791.09[/TD]
[TD="align: right"]1830743.608[/TD]
[TD="align: right"]3340.857[/TD]

[TD="align: right"]46.00597[/TD]
[TD="align: right"]-104.25073[/TD]

[TD="align: right"]78.2[/TD]

[TD="align: right"]1021[/TD]

[TD="align: center"]36[/TD]
[TD="align: right"]206180[/TD]
[TD="align: right"]16715713.4[/TD]
[TD="align: right"]1830743.015[/TD]
[TD="align: right"]3347.726[/TD]

[TD="align: right"]46.00575[/TD]
[TD="align: right"]-104.25073[/TD]

[TD="align: right"]60.5[/TD]

[TD="align: right"]369[/TD]

[TD="align: center"]37[/TD]
[TD="align: right"]206181[/TD]
[TD="align: right"]16715644.17[/TD]
[TD="align: right"]1830741.363[/TD]
[TD="align: right"]3353.35[/TD]

[TD="align: right"]46.00556[/TD]
[TD="align: right"]-104.25074[/TD]

[TD="align: right"]76.2[/TD]

[TD="align: right"]375[/TD]

[TD="align: center"]39[/TD]
[TD="align: right"]206182[/TD]
[TD="align: right"]16715568.2[/TD]
[TD="align: right"]1830739.822[/TD]
[TD="align: right"]3357.615[/TD]

[TD="align: right"]46.00536[/TD]
[TD="align: right"]-104.25075[/TD]

[TD="align: right"]71.6[/TD]

[TD="align: right"]333[/TD]

[TD="align: center"]40[/TD]
[TD="align: right"]206184[/TD]
[TD="align: right"]16715496.7[/TD]
[TD="align: right"]1830738.641[/TD]
[TD="align: right"]3360.608[/TD]

[TD="align: right"]46.00516[/TD]
[TD="align: right"]-104.25076[/TD]

[TD="align: right"]70.6[/TD]

[TD="align: right"]372[/TD]

[TD="align: center"]41[/TD]
[TD="align: right"]206185[/TD]
[TD="align: right"]16715426.12[/TD]
[TD="align: right"]1830737.457[/TD]
[TD="align: right"]3362.825[/TD]

[TD="align: right"]46.00497[/TD]
[TD="align: right"]-104.25076[/TD]

[TD="align: right"]66.5[/TD]

[TD="align: right"]372[/TD]

[TD="align: center"]44[/TD]
[TD="align: right"]206188[/TD]
[TD="align: right"]16715359.7[/TD]
[TD="align: right"]1830736.156[/TD]
[TD="align: right"]3365.936[/TD]

[TD="align: right"]46.00478[/TD]
[TD="align: right"]-104.25077[/TD]

[TD="align: right"]75[/TD]

[TD="align: right"]363[/TD]

[TD="align: center"]45[/TD]
[TD="align: right"]206189[/TD]
[TD="align: right"]16715284.99[/TD]
[TD="align: right"]1830734.486[/TD]
[TD="align: right"]3369.276[/TD]

[TD="align: right"]46.00458[/TD]
[TD="align: right"]-104.25078[/TD]

[TD="align: right"]62.8[/TD]

[TD="align: right"]362[/TD]

[TD="align: center"]48[/TD]
[TD="align: right"]206192[/TD]
[TD="align: right"]16715223.99[/TD]
[TD="align: right"]1830733.556[/TD]
[TD="align: right"]3370.262[/TD]

[TD="align: right"]46.00441[/TD]
[TD="align: right"]-104.25079[/TD]

[TD="align: right"]75.4[/TD]

[TD="align: right"]328[/TD]

[TD="align: center"]49[/TD]
[TD="align: right"]206193[/TD]
[TD="align: right"]16715148.53[/TD]
[TD="align: right"]1830732.895[/TD]
[TD="align: right"]3370.012[/TD]

[TD="align: right"]46.0042[/TD]
[TD="align: right"]-104.25079[/TD]

[TD="align: right"]77.1[/TD]

[TD="align: right"]346[/TD]

[TD="align: center"]51[/TD]
[TD="align: right"]206194[/TD]
[TD="align: right"]16715071.62[/TD]
[TD="align: right"]1830733.635[/TD]
[TD="align: right"]3369.029[/TD]

[TD="align: right"]46.00399[/TD]
[TD="align: right"]-104.25079[/TD]

[TD="align: right"]76.5[/TD]

[TD="align: right"]374[/TD]

[TD="align: center"]54[/TD]
[TD="align: right"]206198[/TD]
[TD="align: right"]16714995.34[/TD]
[TD="align: right"]1830734.716[/TD]
[TD="align: right"]3369.695[/TD]

[TD="align: right"]46.00378[/TD]
[TD="align: right"]-104.25079[/TD]

[TD="align: right"]76.9[/TD]

[TD="align: right"]361[/TD]

[TD="align: center"]56[/TD]
[TD="align: right"]206199[/TD]
[TD="align: right"]16714918.68[/TD]
[TD="align: right"]1830735.778[/TD]
[TD="align: right"]3372.646[/TD]

[TD="align: right"]46.00357[/TD]
[TD="align: right"]-104.25079[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Now remember I have hidden Info between the welds.

Also here is my updated Code
Code:
Sub WELD()

finalrow = ActiveSheet.UsedRange.Rows.Count


Count = 1
For r = 2 To finalrow
    With ActiveCell.SpecialCells(xlCellTypeVisible)
    ActiveCell.Offset(0, 3).Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-4],SEARCH(""-"",RC[-4])-1)"
    ActiveCell.Offset(0, -4).Select
    Selection.Copy
    Do
    ActiveCell.Offset(1, 0).Select
    Loop While ActiveCell.EntireRow.Hidden = True
    ActiveCell.Offset(0, 1).Select
    ActiveSheet.PASTE
    End With
    
Next r


End Sub

And how would you get it to stop at row 56 and not go any further?

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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