Copy specific columns to another closed file

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Welcome. I want to copy certain columns to another closed file. Use this code. It works fine, but it shows strange symbols in column 8. I don't know why

Code:
sub copy


On Error Resume Next


    Dim WB As Workbook, WS As Worksheet, SH As Worksheet


    Dim arr As Variant, i As Variant, cr As Variant, j As Long


    Dim lr As Long


    Set SH = ThisWorkbook.Worksheets("sh1")


    Set WB = Workbooks.Open(ThisWorkbook.Path & "\" & "sh2.xlsm")


    Set WS = WB.Worksheets(SH.Range("D2").Value)


    arr = SH.Range("A5:F" & SH.Cells(Rows.Count, 1).End(xlUp).Row).Value2

    cr = Array(2, 6, 8, 7)


    lr = WS.Cells(Rows.Count, 4).End(xlUp).Row + 1

    For Each i In Array(2, 4, 5, 8)


        WS.Cells(lr, cr(j)).Resize(UBound(arr, 1)).Value = Application.Index(arr, , i)


        j = j + 1

    Next i


    WB.Close Savechanges:=True

End Sub[/CODE]
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
VBA Code:
Sub Copy_Values()

  Dim ws1 As Worksheet, ws2 As Worksheet

  Dim aRws As Variant, aCols As Variant

  Dim lr As Long

 

  'I want to copy the following columns to a closed file

  'starting row 2

  Const ColsOfInterest As String = "1 6 9 14 17 "

  Set ws1 = ActiveSheet
  Set ws2 = Workbooks.Open(ThisWorkbook.Path & "\" & "sh2.xlsm")

  'With the possibility of specifying the sheet, the name of the target sheet in cell d2

  'copy

Sheet Name = ActiveSheet.Range("d2").Value

specific Columns

'"12 13 14 18 23 "

 ' starting row 6

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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