Copy dedicated range, paste to next empty row

R2ah1ze1l

Board Regular
Joined
Nov 10, 2017
Messages
93
data_layout.JPG
Trans_To.JPG


I want to take the columns of data and paste them to a dedicated location in another workbook.
From the data I need info from "B" and "F:EndColumn" but I want to copy F2:F5 to go into M2:M5 and B2:B5 to H2:H5
when shifting to next column in first workbook now I want:
copy G2:G5 to M6:M9 and B2:B5 to H6:H9
continue to end of file.
Close data file

I'm getting hung up trying to determine the appropriate strategy to handle the cell activations.
(avoiding select to reduce time delay)

Thank you for your help.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Private Sub CommandButton1_Click()
Dim wbTrans As Workbook, wbMeas As Workbook, wsTrans As Worksheet, wsMeas As Worksheet
Dim wbPath As String, iss_rng As Range, iss_num As Variant, flPath As String, flName As String, wpTrans As String
Dim dat_v As Variant, dat_rng As Range, wsData As Worksheet
Dim LRow As Long, found As Range, dp As Variant

Application.ScreenUpdating = True
    wpTrans = "N:\E_Neale\CSV_Cleaned_Char"
    wnTrans = "Trans_to_Meas.xlsm"
Set wbTrans = ActiveWorkbook
Set wsTrans = wbTrans.Worksheets("Files")
Set wsData = wbTrans.Worksheets("Measurements")
 
flPath = wsTrans.Range("B1").Value
flName = wsTrans.Range("B2").Value


 
Set iss_rng = wsTrans.Range(Cells(3, 2), Cells(14, 2))
For Each iss_num In iss_rng
    If iss_num = "" Then
        GoTo Leave
    End If
    lastr = 0
    lastc = 0
    i = 0
    wbName = flName & "-" & iss_num & ".xlsx"
    wbPath = flPath & "\" & wbName
    Set wbMeas = Workbooks.Open(wbPath)
    Set wsMeas = wbMeas.Worksheets("Update_CSV")
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastr = lastr + 1
        ActiveCell.Offset(1, 0).Activate
        Wend
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastc = lastc + 1
        ActiveCell.Offset(0, 1).Activate
        Wend
'Need to determine best way to transfer data into wbTrans(Measurements)
wsData.Activate
While cntr_r < (lastr * lastc)
cntr_r = i * lastr
Windows(wbName).Activate
Range(Cells(2, 6), Cells(lastr, 6)).Select <--Errors out Runtime 1004 (Select method of Range class failed) 
Selection.Copy
Windows(wnTrans).Activate
Range(Cells(2 + cntr_r, 6)).Select
ActiveSheet.Paste
Windows(wbName).Activate
Range(Cells(2, 2), Cells(lastr, 2)).Select
Selection.Copy
Windows(wnTrans).Activate
Range(Cells(2 + cntr_r, 8)).Select
ActiveSheet.Paste
Range(Cells(2 + cntr_r, 4), Cells(2 + cntr_r + lastr, 4)).Select
ActiveCell.Value = "1"
Windows(wbName).Activate
Range(1, 6 + i).Select
Selection.Copy
Windows(wnTrans).Activate
Range(Cells(2 + cnrt_r, 2), Cells(2 + cnrt_r + lastr, 2)).Select
ActiveSheet.Paste
i = i + 1
Wend
cntr_r = 0

wbMeas.Activate
ActiveWorkbook.Close

Next iss_num
Leave:

Application.ScreenUpdating = True

End Sub

So I've been tinkering away and used the record macro. I then tried to adjust the code to make it more automated, what am I missing to select this range?
 
Upvote 0
Code:
Set iss_rng = wsTrans.Range(Cells(3, 2), Cells(14, 2))
For Each iss_num In iss_rng
    If iss_num = "" Then
        GoTo Leave
    End If
    lastr = 0
    lastc = 0
    i = 0
    wbName = flName & "-" & iss_num & ".xlsx"
    wbPath = flPath & "\" & wbName
    Set wbMeas = Workbooks.Open(wbPath)
    Set wsMeas = wbMeas.Worksheets("Update_CSV")
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastr = lastr + 1
        ActiveCell.Offset(1, 0).Activate
        Wend
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastc = lastc + 1
        ActiveCell.Offset(0, 1).Activate
        Wend
'Need to determine best way to transfer data into wbTrans(Measurements)
wsData.Activate
While cntr_r < (lastr * lastc)
    cntr_r = i * lastr
    Windows(wbName).Activate
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 6), wsData.Cells(2 + cntr_r + lastr, 6)).Copy Destination:=wsMeas.Range(wsMeas.Cells(2, 6), wsMeas.Cells(lastr, 6)).Value
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)).Copy Destination:=wsMeas.Range(wsMeas.Cells(2, 2), wsMeas.Cells(lastr, 2)).Value
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)).Copy Destination:=wsMeas.Cells(1, 6 + i).Value
    
        ' = meas_data
        ' = sn_data
        ' = char_data
    i = i + 1
Wend
cntr_r = 0

wbMeas.Activate
ActiveWorkbook.Close

Next iss_num
Leave:

I just tried .Copy Destination:= and received a 438 error (object doesn't support this property or method)
 
Upvote 0
Runtime error 13:
Code:
While cntr_r < (lastr * lastc)
    cntr_r = i * lastr
    Windows(wbName).Activate
        meas_data = wsMeas.Range(wsMeas.Cells(2, 6), wsMeas.Cells(lastr, 6))
        sn_data = wsMeas.Range(wsMeas.Cells(2, 2), wsMeas.Cells(lastr, 2))
        char_data = wsMeas.Cells(1, 6 + i)
    Windows(wbTrans).Activate
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 6), wsData.Cells(2 + cntr_r + lastr, 6)) = meas_data
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)) = sn_data
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)) = char_data
    i = i + 1
Wend

wbTrans has 2 worksheets but I already activated the data worksheet so when I flip back to my primary workbook, it would be using the correct sheet. If I try changing to use the wsData reference instead I still receive the same error message.
 
Upvote 0
Code:
Set iss_rng = wsTrans.Range(Cells(3, 2), Cells(14, 2))
For Each iss_num In iss_rng
    If iss_num = "" Then
        GoTo Leave
    End If
    lastr = 0
    lastc = 0
    i = 0
    wbName = flName & "-" & iss_num & ".xlsx"
    wbPath = flPath & "\" & wbName
    Set wbMeas = Workbooks.Open(wbPath)
    Set wsMeas = wbMeas.Worksheets("Update_CSV")
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastr = lastr + 1
        ActiveCell.Offset(1, 0).Activate
        Wend
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastc = lastc + 1
        ActiveCell.Offset(0, 1).Activate
        Wend
'Need to determine best way to transfer data into wbTrans(Measurements)
wsData.Activate
While cntr_r < (lastr * lastc)
    cntr_r = i * lastr
    Windows(wbName).Activate
        meas_data = wsMeas.Range(wsMeas.Cells(2, 6 + i), wsMeas.Cells(lastr, 6 + i)).Value
        sn_data = wsMeas.Range(wsMeas.Cells(2, 2), wsMeas.Cells(lastr, 2)).Value
        char_data = wsMeas.Cells(1, 6 + i).Value
    wsData.Activate
        wsData.Range(wsData.Cells(2 + cntr_r, 6), wsData.Cells(2 + cntr_r + lastr, 6)).Value = meas_data
        wsData.Range(wsData.Cells(2 + cntr_r, 8), wsData.Cells(2 + cntr_r + lastr, 8)).Value = sn_data
        wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)).Value = char_data
    i = i + 1
Wend
cntr_r = 0

wbMeas.Activate
ActiveWorkbook.Close

Next iss_num
Leave:

This is doing what I'd like for the transfer until the very list line in the range, that is populating with '#N/A" for 2 of my 3 data sets. (meas_data and sn_data)

Even with screenupdating=False there is a lot of flickering.
 
Upvote 0
Runtime error 13:
Code:
While cntr_r < (lastr * lastc)
    cntr_r = i * lastr
    Windows(wbName).Activate
        meas_data = wsMeas.Range(wsMeas.Cells(2, 6), wsMeas.Cells(lastr, 6))
        sn_data = wsMeas.Range(wsMeas.Cells(2, 2), wsMeas.Cells(lastr, 2))
        char_data = wsMeas.Cells(1, 6 + i)
    Windows(wbTrans).Activate
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 6), wsData.Cells(2 + cntr_r + lastr, 6)) = meas_data
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)) = sn_data
        wbTrans.wsData.Range(wsData.Cells(2 + cntr_r, 4), wsData.Cells(2 + cntr_r + lastr, 4)) = char_data
    i = i + 1
Wend

wbTrans has 2 worksheets but I already activated the data worksheet so when I flip back to my primary workbook, it would be using the correct sheet. If I try changing to use the wsData reference instead I still receive the same error message.
To any future user:
The Runtime '13' error was Windows(wbTrans).Activate only needed to be wsData.Activate (my assumption is that the wbTrans is my main workbook and the wsData is a worksheet in that workbook. I needed to GoTo my 2nd workbook and merely jump back to my worksheet.
 
Upvote 0
I can't see people looking forward to wading through 5 Posts with code that does not work.
Explain in a concise manner what you want to do.
The explanation in your first post confuses me.
 
Upvote 0
Code:
Private Sub CommandButton1_Click()
Dim wbTrans As Workbook, wbMeas As Workbook, wsTrans As Worksheet, wsMeas As Worksheet, wsData As Worksheet
Dim wbPath As String, flPath As String, flName As String, wpTrans As String
Dim dat_v As Variant, dat_rng As Range, iss_rng As Range, iss_num As Variant
Dim char_clean As Variant, char_rng As Range, r As Variant, rows As Range

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    iWindowState = .WindowState
    .WindowState = xlMinimized
End With

    wpTrans = "N:\E_Neale\CSV_Cleaned_Char"
    wnTrans = "Trans_to_Meas.xlsm"
Set wbTrans = ActiveWorkbook
Set wsTrans = wbTrans.Worksheets("Files")
Set wsData = wbTrans.Worksheets("Measurements")
 
flPath = wsTrans.Range("B1").Value
flName = wsTrans.Range("B2").Value
cntr_r = 0
cntr_k = 0
pn_name = Split(flName, "_")(0)


 
Set iss_rng = wsTrans.Range(Cells(3, 2), Cells(14, 2))
For Each iss_num In iss_rng
    If iss_num = "" Then
        GoTo Leave
    End If
    lastr = 0
    lastc = 0
    i = 0
    col_s = 0
    cntr_c = 0
    r_end = lastr
    r_s = 2
    r = 0
    wbName = flName & "-" & iss_num & ".xlsx"
    wbPath = flPath & "\" & wbName
    Set wbMeas = Workbooks.Open(wbPath)
    Set wsMeas = wbMeas.Worksheets("Update_CSV")
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastr = lastr + 1
        ActiveCell.Offset(1, 0).Activate
        Wend
        wsMeas.Range("F2").Activate
        While Not ActiveCell = ""
        lastc = lastc + 1
        ActiveCell.Offset(0, 1).Activate
        Wend
'Need to determine best way to transfer data into wbTrans(Measurements)
wsData.Activate
While cntr_c < (lastc * lastr)
    cntr_r = ((col_s - i) * lastr) + cntr_k
    Windows(wbName).Activate
            chk_char = Left(wsMeas.Cells(1, 6 + col_s).Value, 1)
            If IsNumeric(chk_char) = False Then
                i = i + 1
               GoTo skip_letter
            End If
        meas_data = wsMeas.Range(wsMeas.Cells(2, 6 + col_s), wsMeas.Cells(lastr + 1, 6 + col_s)).Value
        sn_data = wsMeas.Range(wsMeas.Cells(2, 2), wsMeas.Cells(lastr + 1, 2)).Value
        char_data = wsMeas.Cells(1, 6 + col_s).Value
    wsData.Activate
        wsData.Range(wsData.Cells(2 + cntr_r, 6), wsData.Cells(1 + cntr_r + (lastr), 6)).Value = meas_data
        wsData.Range(wsData.Cells(2 + cntr_r, 8), wsData.Cells(1 + cntr_r + (lastr), 8)).Value = sn_data
        wsData.Range(wsData.Cells(2 + cntr_r, 2), wsData.Cells(1 + cntr_r + (lastr), 2)).Value = char_data
        wsData.Range(wsData.Cells(2 + cntr_r, 5), wsData.Cells(1 + cntr_r + (lastr), 5)).Value = 1
        wsData.Range(wsData.Cells(2 + cntr_r, 1), wsData.Cells(1 + cntr_r + (lastr), 1)).Value = pn_name
        For r = 1 To lastr
            wsData.Range("D" & 1 + cntr_r + r).Value = r
        Next r
skip_letter:
    col_s = col_s + 1
    cntr_c = (col_s + i) * lastr
Wend

cntr_k = cntr_r
cntr_c = 0

wbMeas.Activate
ActiveWorkbook.Close

Next iss_num
Leave:

Set char_rng = wsData.Range(wsData.Cells(2, 2), wsData.Cells(cntr_k + 1, 2))
For Each char_clean In char_rng
    If InStr(char_clean.Value, "_") > 0 Then
        char_clean.Value = Replace(char_clean, "_", ".")
    End If
Next char_clean



With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    iWindowState = .WindowState
    .WindowState = xlMaximized
End With

End Sub

Transfers all the data and arranges it how I needed. (multiple columns of data now neatly assembled in a singular column with corresponding data from adjacent rows)
I ran into a lot of errors along the way but completed it digging through other codes.

If you ever feel stuck and alone, here's a post to help show you can still work it out! (along with some of the history of hardships)
Thank you to every message board and instruction I read through.

If there were better ways to complete my task, happy to read variations of the code!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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