How to copy data dynamically via VBA

Howdy1

Board Regular
Joined
Aug 13, 2008
Messages
50
Hi,

I am stuck in the VBA coding to figure out how I can copy two rows of data dynamically within the same spreadsheet. I got some suggestion earlier but still can't make it to work. There are two columns of data (ColA and ColB) that I need to copy dynamically depending on the next empty cell. For example, ColA will have the TotalRegion and then the next row will be empty for the same column until it hit another Region. I need to VBA to copy the data all the way down if there is data. In another word, if ColA and ColB have data, the rest of the rows should be filled up as well.

A generous gentleman offered the following code earlier but I try to understand where I can insert the copy and paste data funtion. In another words, how can I determine where I copy the data and paste it with the loops? Sorry, but I am quite new to VBA and just try to learn VBA via recording the macro.

Thanks a million for all of the help!

Here is the code I tried to use

Sub CopyData()
Dim cl As Range
Range("A2: B2").Select
Selection.Copy
For Each cl In Range("$A$2:$A" & Range("$B$24").End(xlUp).Row)
If cl = "" Then
Selection.Paste
cl = Cells(cl.Row - 1, 1)
Else
Range(cl, Cells(cl, 1)).Copy ' this prompts error, how can I determine
the next cell to copy as I need to copy
two columns
End If
Next cl
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Sorry, I didn't have the Admin right in my computer to install the software. But let me try to explain succintly. For column A and Column B, these act as like the subtotal line. For each subtotal, the name for Column A and Column B exists. However, all the rest of the column A and column B are empty before it has the subtotal. When I need to do is to fill in all the name for the column A and column B so that I can import the data into my Access table to run query.

Example


ColA ColB Amount
TotReg
 
Upvote 0
Select all of your data in columns A and B.

Press F5.

Click Special.

Tick Blanks and click OK

Press =

Press Up arrow

Hold down CTRL and press Enter.
 
Upvote 0
Wow.... That is really powerful!!!!

I have been trying to copy and paste 42000 records manually for the paste few hours and it drove my crazy. Can I translate this into the VBA code via recording the Macro?

You save me tons of hours and headache. Many thanks to you!!!!
 
Upvote 0
Yes it can be recorded using a macro. Do that and paste the code and we can tidy it up for you.
 
Upvote 0

Forum statistics

Threads
1,207,402
Messages
6,078,265
Members
446,324
Latest member
JKamlet

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