Reputation: 9
I am working with a column of data in which the range (e.g. how many rows of data there'll be) will eventually vary for each new sheet that a user creates.
On one sheet, the range in B may be from B3:B7, on another, it may be from B3:B22 for example.
I want a Macro that copies the data from the range B3:Bn, with n being the last row before there is a blank cell in the next row. For example, Copy B4:B7, and in B8 there is a blank cell. I want to paste this range into another workbook, in column C.
I'm only a beginner in VBA and have not gotten far with the code. I have tried several iterations of a loop but it doesn't really work.
This is the weak code i have so far. Any help is appreciated.
Sub main()
Dim r As Range
Set r = Range("B3")
Do While r.Value <> ""
Range("C").Value = r.Value
Set r = r.Offset(1)
Loop
End Sub
Upvotes: 1
Views: 6989
Reputation: 6654
See this is a Simple code to get you started. Change the Names accordingly
Sub main()
Dim wba As Workbook
Dim wbb As Workbook
Set wba = Workbooks("Workbook A")
Set wbb = Workbooks("Workbook B")
with wba.Worksheets("Worksheet in A")
.Range("B3", .Range("B3").End(xlDown)).Copy
End With
wbb.Worksheets("Worksheet in B").Range("C3").PasteSpecial xlPasteValues
End Sub
A is the workbook with Data, B where you have to copy.
Change the sheet names on both Workbooks, and Range("B3", Range("B1").End(xlDown))
will select the range till a blank cell.
Upvotes: 5
Reputation: 194
I just want to add one thing to Mikku's code. Instead of xlDown, in this case xlUp might be better. Because there might be one cell blank and next cell is not. When looking for last row, xlUp is good practice.
wba.Worksheets("Worksheet in A").Range("B3", Range("B" & Rows.Count).End(xlUp)).Copy
Upvotes: 1