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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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
1Point IDNorthingEastingElevationFeature CodeLatitude (Local)Longitude (Local)Attributes 1Attributes 2Attributes 3Attributes 4Attributes 5Attributes 6
220614616716330.991830753.7583313.547LE46.00745-104.250671168-1F40520
520614916716260.911830751.8533316.922WELD46.00726-104.25068F2ML-7681016-91168-128.3R423241016
820615216716232.591830751.3183318.787WELD46.00718-104.25068F2ML-7691036-91016-945.9F403201036
1420615816716187.531830750.7183323.865WELD46.00705-104.25068F2ML-7701018-91036-921.2F403191018
1620615916716166.381830750.3833325.947WELD46.007-104.25068F2ML-7711018-9B1018-920.9F403191018
1920616316716145.481830749.9793327.325WELD46.00694-104.25069F2ML-7721013-81018-9B18.3R422471013
2020616416716127.341830749.6693328.03WELD46.00689-104.25069F2ML-7731013-8B1013-818R422471013
2220616516716109.411830749.1323328.651WELD46.00684-104.25069F2ML-7741013-8D1013-8B9R422471013
2320616716716100.491830748.9863328.894WELD46.00682-104.25069F2ML-7751015-51013-8D49R423241015
2420616816716051.531830747.8443329.948WELD46.00668-104.2507F2ML-7761082-41015-552.8F405211082
2620616916715998.721830746.9323331.266WELD46.00654-104.2507F2ML-7771095-81082-421.2F405221095
2720617116715977.651830746.693331.822WELD46.00648-104.25071A2C2F2ML-778NW1095-8B1095-837F405221095
2820617216715940.711830745.9683332.923WELD46.00638-104.25071F2ML-7791000-41095-8B75.3R422451000
3020617316715865.491830744.743336.127WELD46.00617-104.25072F2ML-780993-61000-475.7R42243993
3120617516715791.091830743.6083340.857WELD46.00597-104.25073F2ML-7811021-2993-678.2R422481021
3620618016715713.41830743.0153347.726WELD46.00575-104.25073F2ML-782369-51021-260.5R43792369
3720618116715644.171830741.3633353.35WELD46.00556-104.25074F2ML-783375-3369-576.2R43792375
3920618216715568.21830739.8223357.615WELD46.00536-104.25075A2C2F2ML-784NW333-6375-371.6F41756333
4020618416715496.71830738.6413360.608WELD46.00516-104.25076F2ML-785372-5333-670.6R43792372
4120618516715426.121830737.4573362.825WELD46.00497-104.25076F2ML-786372-2372-566.5R43792372
4420618816715359.71830736.1563365.936WELD46.00478-104.25077F2ML-787363-3372-275R43792363
4520618916715284.991830734.4863369.276WELD46.00458-104.25078F2ML-788362-4363-362.8R43792362
4820619216715223.991830733.5563370.262WELD46.00441-104.25079F2ML-789328-8362-475.4F41756328
4920619316715148.531830732.8953370.012WELD46.0042-104.25079F2ML-790346-6328-877.1F41757346
5120619416715071.621830733.6353369.029WELD46.00399-104.25079C2F2ML-791R374-4346-676.5R43792374
5420619816714995.341830734.7163369.695WELD46.00378-104.25079F2ML-792361-5374-476.9R43792361
5620619916714918.681830735.7783372.646LE46.00357-104.25079361-5R43792

<tbody>
</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,216,225
Messages
6,129,604
Members
449,520
Latest member
TBFrieds

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