使用Excel VBA移动不同工作簿的列

时间:2018-02-23 22:56:53

标签: excel excel-vba excel-2016 vba

我将脚本从一个工作表移动到另一个工作表,但是我必须将工作簿的名称放入我希望它工作的VBA脚本中。我可能不会很好地解释它,但我会附上剧本。如何将此代码用于不同的工作簿而不仅仅是一个名称在这里的>设置objWorkbook = objExcel.Workbooks.Open(“Referrals”)我必须输入我正在使用的excel文件的名称进入宏观工作的括号

Sub Column_Test()
'
' Column_Test Macro
'
' Keyboard Shortcut: Ctrl+c
Set objExcel = CreateObject("Excel.Application") 'Moves cell A1 to A1'    
objExcel.Visible = True    
Set objWorkbook = objExcel.Workbooks.Open("Referrals") 
' "Refferals" is the name of the excel workbook '

Set objWorksheet = objWorkbook.Worksheets(1)    
objWorksheet.Activate   

Set objRange = objWorksheet.Range("A1").EntireColumn

objRange.Copy      
Set objWorksheet = objWorkbook.Worksheets(2)    
objWorksheet.Activate        
Set objRange = objWorksheet.Range("A1")    
objWorksheet.Paste (objRange)    ''



Set objWorksheet = objWorkbook.Worksheets(1) 'Moves cell E1 to B1'

objWorksheet.Activate


Set objRange = objWorksheet.Range("E1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("B1")

objWorksheet.Paste (objRange)

''


'Moves Cell F1 to C1'

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Activate

Set objRange = objWorksheet.Range("F1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("C1")
objWorksheet.Paste (objRange)

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Activate
''



Set objRange = objWorksheet.Range("G1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("D1")

objWorksheet.Paste (objRange)

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Activate
''



Set objRange = objWorksheet.Range("H1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("E1")

objWorksheet.Paste (objRange)


Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Activate
''



Set objRange = objWorksheet.Range("K1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("F1")

objWorksheet.Paste (objRange)

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Activate
''



Set objRange = objWorksheet.Range("M1").EntireColumn

objRange.Copy


Set objWorksheet = objWorkbook.Worksheets(2)

objWorksheet.Activate


Set objRange = objWorksheet.Range("G1")

objWorksheet.Paste (objRange)



'
End Sub

<

1 个答案:

答案 0 :(得分:0)

我不知道您想要使用多少工作簿,因为您没有提供完整的信息,所以我们猜测您希望每次使用它时都要自定义。

只需替换

Set objWorkbook = objExcel.Workbooks.Open("Referrals")

使用此代码:

' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Filters.Add "Excel Files", "*.xlsx", 1

    If .Show = 0 Then Exit Sub
    ' Display paths of each file selected
    Set objWorkbook = Application.Workbooks.Open(.SelectedItems(1))
End With

每次执行此代码时,代码都会要求您提供工作簿。