What do these code mean?

Phoenix_Turn

New Member
Joined
May 11, 2011
Messages
37
Hi all just wondering what do these syntax in vba mean?

1)
Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
intRow = ActiveCell.Row

Set rngSheet = shtSheet.Range("A7:D" & intRow)
rngSheet.Select
rngSheet.Copy

Then

2)
Do
ActiveCell.Offset(3, 2).Select
Loop While Not IsEmpty(ActiveCell)
ActiveSheet.Paste

Can someone define what each function and each line does? Thanks :)
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
1)
This macro starts with the active cell (the cell that you have selected in your Excel file the moment you start this macro).

Do Until ActiveCell = "" 'Do the following (ending at "Loop") till you find that the cell is empty ("")
ActiveCell.Offset(1, 0).Select 'Select the next cell (the cell below: offset(1,0) means 1 cell down, 0 to the right)
Loop 'Hop back to the "Do" line
intRow = ActiveCell.Row 'intRow is the variable name, is is filled here with the activecell row, that is the cell your macro ended at.

Set rngSheet = shtSheet.Range("A7:D" & intRow) 'define the range of cells, starting at A7 and ending at column D, the row found with the loop of the bit above. (so e.g. D20).

rngSheet.Select 'Select the found range
rngSheet.Copy 'Copy the found range

Then

2)
Do 'Same like before: this is where a loop starts, only in this case the condition is at the end of the loop, not at the start.
ActiveCell.Offset(3, 2).Select 'Select the cell 3 cells below and 2 cells to the right of the active cell
Loop While Not IsEmpty(ActiveCell) 'do that again if that cell is not empty (and again and again etc.), otherwise contintue to the next line
ActiveSheet.Paste 'Paste the range that was copied to the last selected cell

G'luck with that :)
 
Upvote 0
Basically the code looks for empty cells, copies a range from A7 to the empty cell row on column D and pastes it to the first empty cell found when going 3 steps down and 2 steps right:
Code:
'1)
Do Until ActiveCell = ""    'Repeat until ActiveCell(.value) is empty
ActiveCell.Offset(1, 0).Select  'Select the cell below ActiveCell
Loop                        'Go back to Do Until -line
intRow = ActiveCell.Row     'The loop ends when activecell is empty. intRow -variable = the row where the empty cell was found
Set rngSheet = shtSheet.Range("A7:D" & intRow)  'Sets the range variable rngSheet = range "A7:D" to the empty cell row from above on the shtSheet (= worksheet variable set somewhere before). The next line reveals the shtSheet variable = ActiveSheet because you can only select ranges from current sheet.
rngSheet.Select 'Selects the rngSheet -range (delete this line: the code doesn't need it and if you don't select ranges the rngSheet variable can be on any sheet - even from another workbook)
rngSheet.Copy   'Copies the range rngSheet
'2)
Do  'Do the following
ActiveCell.Offset(3, 2).Select  'Select the cell 3 rows down and 2 columns to the right from ActiveSheet
Loop While Not IsEmpty(ActiveCell)  'If ActiveCell is not empty go back to Do -line
ActiveSheet.Paste   'ActiveCell should be empty when the code gets here: Paste the copied range here.
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

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