I'm trying to write a code to open mentioned workbooks one by one and move & copy a particular worksheet into a new workbook
my code for the above mentioned task runs well till it opens the first file, then it gives me the following error
method or data member not found
Sub OpenFilesMoveCopyWorksheet()
Const PTH As String = "C:\Users\xxx\yyy\" 'use const for fixed values
Dim SFile As Workbook, SFname As Worksheet, SFname2 As Worksheet
Dim SFlname As String, I As Long, DFile As Workbook
Dim Acellrng As Range, ws As Worksheet, rngDest As Range, rngCopy As Range
Set SFile = ThisWorkbook
Set SFname = SFile.Worksheets("Sheet1")
Application.ScreenUpdating = False
For I = 1 To SFname.Cells(Rows.Count, "A").End(xlUp).Row
SFlname = SFname.Range("A" & I).Value
If Len(SFlname) > 0 Then
Set DFile = Workbooks.Open(PTH & SFlname)
If DFile.Worksheets.Name Like "*.cours" Then
DFile.Worksheet.copyafter: SFile.SFname
End If
DFile.Close savechanges:=False
End If
Next I
MsgBox "job done"
Application.ScreenUpdating = True
End Sub
Modified the code still getting "Run-time error'-2147221080 (800401a8)': Automation error"
Sub OpenFilesMoveCopyPaste()
Const PTH As String = "C:\xxx\yy\" 'use const for fixed values
Dim SFile As Workbook, SFname As Worksheet, SFname2 As Worksheet
Dim SFlname As String, I As Long, DFile As Workbook, I1 As Long, SFlname2 As String
Dim Acellrng As Range, ws As Worksheet, rngDest As Range, rngCopy As Range
Set SFile = ThisWorkbook
Set SFname = SFile.Worksheets("Sheet1")
Application.ScreenUpdating = False
For I = 1 To SFname.Cells(Rows.Count, "A").End(xlUp).Row
SFlname = SFname.Range("A" & I).Value
If Len(SFlname) > 0 Then
Set DFile = Workbooks.Open(PTH & SFlname)
For I1 = 1 To SFname.Cells(Rows.Count, "B").End(xlUp).Row
SFlname2 = SFname.Range("B" & I1).Value
If Len(SFlname2) > 0 Then
Set ws = DFile.Worksheets(SFlname2)
ws.copy Before:=SFile.Sheets("sheet1")
DFile.Close savechanges:=False
End If
Next I1
End If
Next I
MsgBox "job done"
Application.ScreenUpdating = True
End Sub
Final Version
Sub OpenFilesMoveCopyPasteSpecial()
Const PTH As String = "C:\XXX\YY\" 'use const for fixed values
Dim SFile As Workbook, SFname As Worksheet, SFname2 As Worksheet
Dim SFlname As String, I As Long, DFile As Workbook, I1 As Long, SFlname2 As String
Dim Acellrng As Range, ws As Worksheet, rngDest As Range, rngCopy As Range
Application.DisplayAlerts = False
Set SFile = ThisWorkbook
Set SFname = SFile.Worksheets("Sheet1")
Application.ScreenUpdating = False
For I = 1 To SFname.Cells(Rows.Count, "A").End(xlUp).Row
SFlname = SFname.Range("A" & I).Value
If Len(SFlname) > 0 Then
Set DFile = Workbooks.Open(PTH & SFlname)
Debug.Print DFile.Name
SFlname2 = SFname.Range("B" & I).Value
Set ws = DFile.Worksheets(SFlname2)
ws.copy After:=SFile.Sheets("sheet1")
Cells.Select
Range("AO1").Activate
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
DFile.Close savechanges:=False
End If
Next I
MsgBox "job done"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I am new to macro.
I have written macro code to add the rows based on filter from the macro enabled excel file and copy the results in new excel file.
I have VBS to run the macro.
My problem is
when I run the macro from the xlsm file ,it is running only once and the values are stored correctly by creating the xlsx file
But when I run the same macro from VBS, macro is running multiple times with error msg which is posted below
My Macro is :
Sub SuppOSCalculation()
Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet
Dim filterRange As Range
Dim lastRow As Long
Dim lastCol As Long
Dim Total As Double
Dim AddRange As Range
Dim c As Variant
Dim list As Object, item As Variant
Dim i As Integer
spath = "Mypath\"
sFile = spath & "supp.xlsm"
Set wb = Workbooks.Open(sFile)
SendKeys "{Enter}"
Set src = wb.Sheets("supp")
Set tgt = wb.Sheets("Sheet3")
Set list = CreateObject("System.Collections.ArrayList")
i = 2
' turn off any autofilters that are already set
src.AutoFilterMode = False
' Copy all fileds to second sheet and remove duplicates
src.Range("C3:C" & Cells(Rows.Count, "C").End(xlUp).Row).Copy tgt.Range("A2")
tgt.Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlNo
' Add all values in Second sheet to a list
With tgt
For Each item In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
If Not list.Contains(item.Value) Then list.Add item.Value
Next
End With
tgt.Range("A1").Value = "Supplier GL Code"
tgt.Range("B1").Value = "Supplier OS Report-Invoice Amount"
' find the last row and Column with data in column A
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
lastCol = src.Cells(1, Columns.Count).End(xlToLeft).Column
'MsgBox lastCol
' the range that we are auto-filtering (all columns)
Set filterRange = src.Range("A2:AF2" & lastRow)
For Each item In list
'From List set the value for the filter
' MsgBox (item)
filterRange.Range("C2").AutoFilter field:=3, Criteria1:=item
'Add the column value after applying filter
Set AddRange = src.Range("P3:P" & src.Range("P" & Rows.Count).End(xlUp).Row)
Total = WorksheetFunction.Sum(AddRange.SpecialCells(xlCellTypeVisible))
'MsgBox (Total)
tgt.Range("B" & i).Value = Total
i = i + 1
Next
'src.AutoFilterMode = False
'wb.Close SaveChanges:=True
Dim lRow, lCol As Integer
tgt.Select
lRow = Range("A" & Rows.Count).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
For Each cell In Range(Cells(1, "B"), Cells(1, lCol))
Union(Range("A2:A" & lRow), Range(Cells(2, cell.Column), Cells(lRow, cell.Column))).Copy
Workbooks.Add
Range("A1").PasteSpecial
ActiveWorkbook.SaveAs Filename:= _
"SupOSTBCalc\" & cell.Value & ".xlsx" 'You might want to change the extension (.xls) according to your excel version
Next cell
ActiveWorkbook.Close
Application.CutCopyMode = False
'wb.Close
' Application.DisplayAlerts = False
' Application.AlertBeforeOverwriting = False
' Application.ScreenUpdating = False
' SendKeys "{Enter}"
wb.Close savechanges:=False
End Sub
VBS is:
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("Mypath\SupOSTBCalc.xlsm")
xlApp.Run "Module1.SuppOSCalculation"
'xlBook.Save
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Error msg is
Pls help me to solve this.
I've only been at VBA for about 2 weeks so I need some assistance. I have a loop setup to copy 7 cells across on workbook (A), then paste them vertically in a column on workbook (B). For some reason the code is working but will not paste any data... I've been trying to troubleshoot for a while now with no luck.
Here is a screenshot of the sheet I'm copying data from.
Here is a screenshot of the workbook I'm pasting too.
Sub pullSecEquipment()
Dim path As String
Dim ThisWB As String
Dim wbDest As Workbook
Dim shtDest As Worksheet
Dim Filename As String
Dim Wkb As Workbook
Dim CopyRng As Range, DestRng As Range
Dim lRow As Integer
Dim destLRow As Integer
Dim UpdateDate As String
ThisWB = ActiveWorkbook.Name
Dim selectedFolder
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
selectedFolder = .SelectedItems(1) & "\"
End With
path = selectedFolder
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shtDest = Workbooks("GPnewchapterTEST2.xlsm").Worksheets("START")
'clear content of destination table
shtDest.Rows("8:" & Rows.Count).ClearContents
Filename = Dir(path & "\*.xls*", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
'MsgBox Filename
'''''
'SEC
'''''
If InStr(Filename, "Equipment") <> 0 Then
'''
'' Equipment Hours
'''
Dim range1 As Range
Set range1 = Range("E:K")
If shtDest.Name Like "*-*" Then
'last row
destLRow = Wkb.Sheets(1).Cells.Find(what:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
'1st row
lRow = Wkb.Sheets(1).Cells.Find(what:="EQUIPMENT DESCRIPTION", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Dim i As Integer
For i = lRow To destLRow
Set CopyRng = Wkb.Sheets(1).Range(Cells(i, 5).Address, Cells(i, 11).Address)
Set DestRng = shtDest.Range("O" & shtDest.Cells(Rows.Count, 1).End(xlUp).Row + 1)
CopyRng.Copy
DestRng.PasteSpecial Transpose:=True
Application.CutCopyMode = False 'Clear Clipboard
i = i + 2
Next i
End If
End If
Filename = Dir()
Loop
MsgBox "Done!"
End Sub
I am struggling with my VBA code. Instead of fixed values in a table, which contains the names how the workbooks should be saved as. My range needs to be variable (below example for starting with range "A3").
Sheets("CC").Select 'sheet with the names
Range("A3").Select ' starting from this range are the names in a column
Selection.Copy
Sheets("CZK").Select 'going to different sheet to paste some value
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False 'pasting values to different sheet
Application.CutCopyMode = False
Sheets("CC").Select 'returning back to the sheet with names
Nazev = Range("A3")
ActiveWorkbook.SaveAs Filename:=cesta & Nazev 'saving it with predefined path and name
I have to start like this:
Set MyRange = Sheets("CC").Range("A3") ' predefining varible range
Set MyRange = Range(MyRange, MyRange.End(xlDown))
But then I am stuck.
Something like this should work for you:
Sub tst()
Dim wb As Workbook
Dim wsNames As Worksheet
Dim wsDest As Worksheet
Dim NameCells As Range
Dim NameCell As Range
Dim cesta As String
Dim Nazev As String
cesta = "C:\Test\"
Set wb = ActiveWorkbook
Set wsNames = wb.Sheets("CC")
Set wsDest = wb.Sheets("CZK")
Set NameCells = wsNames.Range("A3", wsNames.Cells(wsNames.Rows.Count, "A").End(xlUp))
Application.DisplayAlerts = False
For Each NameCell In NameCells
Nazev = NameCell.Value
wsDest.Range("B2").Value = Nazev
wb.SaveAs cesta & Nazev & ".xlsm", xlOpenXMLWorkbookMacroEnabled
Next NameCell
Application.DisplayAlerts = True
End Sub
I will keep this quick. The attached code for the most part works i have used slight variations of it on other projects. the commented out range3.copy is from my last project.
I am currently having issues getting selection.copy to copy the selected range in the correct workbook. I have tried many things some are noted in the script. but I can not get the selection.copy to work
.range.copy will work and populate the clipboard. But I have not figured out how to pastespecial using .copy.
I tried outputting to variable .. didn't work as i thought it might. I feel I have to be missing something on the workbook selection/activation but I don't know what. Thanks in advance for any advice or assistance .. I will continue plugging away and see if I can figure it out.
Here is the first segment with the issue. SRCrange1.select then selection.copy does not actually copy the designated selection. The full code is below.
Dim MyColumn As String
Dim Here As String
Dim AC As Variant
'SRCrange1.copy ': This will copy to clipboard
'objworkbook.Worksheets("plan").Range("b6:h7").Select no change from SRCrange1.select
'SRCrange1.Select 'the range does select
'Selection.copy ' this will cause a activecell in DSTwb _
to be copied neither direct reference to SRCrange1.select or .avtivate will change that.
DSTwb.Select
DSTwb.Range("b2").Select
Here = ActiveCell.Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
lastrow.Select
Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
FULL CODE
Sub parse()
Dim strPath As String
Dim strPathused As String
'On Error Resume Next
Set objexcel = CreateObject("Excel.Application")
objexcel.Visible = True
objexcel.DisplayAlerts = False
strPath = "C:\prodplan"
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
'Loop through objWorkBooks
For Each objfile In objFolder.Files
If objfso.GetExtensionName(objfile.Path) = "xlsx" Then
Set objworkbook = objexcel.Workbooks.Open(objfile.Path)
' Set path for move to at end of script
strPathused = "C:\prodplan\used\" & objworkbook.Name
'open WB to consolidate too
Workbooks.Open "C:\prodplan\compiled\plancon.xlsx"
'Range management sourcebook
Set SRCwb = objworkbook.Worksheets("plan")
Set SRCrange1 = objworkbook.Worksheets("plan").Range("b6:i7")
Set SRCrange2 = objworkbook.Worksheets("plan").Range("k6:p7")
'Set SRCrange3 = objworkbook.Worksheets("").Range("")
'Range management sourcebook
Set DSTwb = Workbooks("plancon.xlsx").Worksheets("data")
'Set DSTrange1 = Workbooks("plancon.xlsx").Worksheets("data").Range("")
'Set DSTrange2 = Workbooks("plancon.xlsx").Worksheets("data").Range("")
'Set DSTrange3 = Workbooks("plancon.xlsx").Worksheets("data").Range("")
'start header dates and shifts copy from objworkbook to consolidated WB
SRCwb.Select
'On Error Resume Next
'SRCwb.Cells.UnMerge
Dim MyColumn As String
Dim Here As String
Dim AC As Variant
'SRCrange1.copy ': This will copy to clipboard
'objworkbook.Worksheets("plan").Range("b6:h7").Select no change from SRCrange1.select
'SRCrange1.Select 'the range does select
'Selection.copy ' this will cause a activecell in DSTwb _
to be copied neither direct reference to SRCrange1.select or .avtivate will change that.
DSTwb.Select
DSTwb.Range("b2").Select
Here = ActiveCell.Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
lastrow.Select
Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
SRCrange2.Select
Selection.copy
Workbooks("plancon.xlsx").Worksheets("sheet1").Select
ActiveSheet.Range("b2").Select
ActiveSheet.Range("b2").Activate
Here = ActiveCell.Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
lastrow.Select
Selection.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
' range3.copy
' Workbooks("data.xlsx").Worksheets("sheet1").Activate
' ActiveSheet.Range("c2").Select
' ActiveSheet.Range("c2").Activate
' Here = ActiveCell.Address
' MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
' Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
' ActiveSheet.Paste Destination:=lastrow
'start loop for objworkbook name copy to field in plancon corisponding with date/shift and copy/paste select row data.
objworkbook.Close False
'Move proccesed file to new Dir
OldFilePath = objfile 'original file location
NewFilePath = strPathused ' new file location
Name OldFilePath As NewFilePath ' move the file
End If
Next
objexcel.Quit
End Sub
First, a relative welcome to SO!
Second, some tips for you that will make life easier in VBA programming:
Use Option Explicit and always Dimension and Declare your variable types.
When naming variables, make them easy to understand and follow. So, if you are going to create a worksheet variable, call it something like wksCopy. Or, if you are going to name a workbook, call it wkbCopyTo
You don't need to use .Select and .Activate, but rather you can work directly with your objects. Also, by declaring the appropriate variables types, this make it much easier to work with these objects in your code each time you need them.
I don't know if you are running this code inside Excel, or another application (like Access), but if you are in Excel, there is no need to create an Excel object, as you can work with the Excel App directly. Ignore this if you are using Access / Word / PPT etc to fire the code.
All these tips make your code much easier to read and understand and follow when trying to debug, and write.
All that said, I have refactored your code above to incorporate most of these principles (I kept all your variable names intact so you wouldn't get lost in any re-namings.) If this re-write doesn't directly solve your problem = which it may not, because the code is kind of confusing to me as written, I think it will be much easier for you to follow and understand and find out where it's not doing what you expect when you debug. Also, I think it will help us help you if you can't figure it out.
Sub parse()
Dim strPath As String, strPathused As String
Dim objexcel As Excel.Application
Set objexcel = CreateObject("Excel.Application")
With objexcel
.Visible = True
.DisplayAlerts = False
End With
strPath = "C:\prodplan"
Dim objfso As FileSystemObject, objFolder As Folder
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
'Loop through objWorkBooks
For Each objfile In objFolder.Files
If objfso.GetExtensionName(objfile.Path) = "xlsx" Then
Dim objWorkbook As Excel.Workbook
Set objWorkbook = objexcel.Workbooks.Open(objfile.Path)
' Set path for move to at end of script
strPathused = "C:\prodplan\used\" & objWorkbook.Name
'open WB to consolidate too
objexcel.Workbooks.Open "C:\prodplan\compiled\plancon.xlsx"
'Range management sourcebook
Dim SRCwb As Excel.Worksheet, SRCrange1 As Excel.Range, SRCrange2 As Excel.Range
Set SRCwb = objWorkbook.Worksheets("plan") 'sjh -> to me wb implies wb, but you set it to a worksheet (could be a style thing, but worth pointing out
Set SRCrange1 = objWorkbook.Worksheets("plan").Range("b6:i7")
Set SRCrange2 = objWorkbook.Worksheets("plan").Range("k6:p7")
'Range management sourcebook
Set DSTwb = Excel.Worksheet
Set DSTwb = Workbooks("plancon.xlsx").Worksheets("data")
'start header dates and shifts copy from objworkbook to consolidated WB
Dim MyColumn As String
Dim Here As String
Dim AC As Variant
Here = DSTwb.Range("B2").Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
'sjh comment -> best to change "ActiveWorkbook" to the workbook referece you mean, like objWorkbook
Dim lastrow As Range
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
SRCrange1.Copy
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
Here = Workbooks("plancon.xlsx").Worksheets("sheet1").Range("B2").Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
'sjh comment -> best to change "ActiveWorkbook" to the workbook referece you mean, like objWorkbook
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
SRCrange2.Copy
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
objWorkbook.Close False
'Move proccesed file to new Dir
OldFilePath = objfile 'original file location
NewFilePath = strPathused ' new file location
Name OldFilePath As NewFilePath ' move the file
End If
Next
objexcel.Quit
End Sub
UPDATE If you are running this all in Excel. Just use this code below. I left both codes in my answer, in case you are not running this from Excel.
Option Explicit
Sub parse()
Application.DisplayAlerts = False
Dim strPath As String, strPathused As String
strPath = "C:\prodplan"
Dim objfso As FileSystemObject, objFolder As Folder
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
'Loop through objWorkBooks
For Each objfile In objFolder.Files
If objfso.GetExtensionName(objfile.Path) = "xlsx" Then
Dim objWorkbook As Workbook
Set objWorkbook = Workbooks.Open(objfile.Path)
' Set path for move to at end of script
strPathused = "C:\prodplan\used\" & objWorkbook.Name
'open WB to consolidate too
Workbooks.Open "C:\prodplan\compiled\plancon.xlsx"
'Range management sourcebook
Dim SRCwb As Worksheet, SRCrange1 As Range, SRCrange2 As Range
Set SRCwb = objWorkbook.Worksheets("plan")
Set SRCrange1 = SRCwb.Range("b6:i7")
Set SRCrange2 = SRCwb.Range("k6:p7")
'Range management sourcebook
Dim DSTwb As Worksheet
Set DSTwb = Workbooks("plancon.xlsx").Worksheets("data")
'start header dates and shifts copy from objworkbook to consolidated WB
Dim MyColumn As String
Dim Here As String
Dim AC As Variant
Here = DSTwb.Range("B2").Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
'sjh comment -> best to change "ActiveWorkbook" to the workbook referece you mean, like objWorkbook or the other workbook you have open
Dim lastrow As Range
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
SRCrange1.Copy
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
Here = Workbooks("plancon.xlsx").Worksheets("sheet1").Range("B2").Address
MyColumn = Mid(Here, InStr(Here, "$") + 1, InStr(2, Here, "$") - 2)
'sjh comment -> best to change "ActiveWorkbook" to the workbook referece you mean, like objWorkbook or the other workbook you have open
Set lastrow = ActiveWorkbook.ActiveSheet.Range(MyColumn & "65536").End(xlUp).Offset(1, 0)
SRCrange2.Copy
lastrow.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks:=False, Transpose:=True
objWorkbook.Close False
'Move proccesed file to new Dir
OldFilePath = objfile 'original file location
NewFilePath = strPathused ' new file location
Name OldFilePath As NewFilePath ' move the file
End If
Next
End Sub
Just to add to the other answers: for contiguous ranges you don't need to use copy for this operation (pastespecial >> values + transpose)
Sub CopyValuesTranspose()
Dim rngCopy As Range, rngPaste As Range
Set rngCopy = Range("A1:B10")
Set rngPaste = Range("D1")
rngPaste.Resize(rngCopy.Columns.Count, rngCopy.Rows.Count).Value = _
Application.Transpose(rngCopy.Value)
End Sub
no need to select a range and then copy the selection, when you can copy a range directly:
objworkbook.Worksheets("plan").Range("b6:h7").Copy
same_or_different_Range.PasteSpecial Paste:=xlPasteValues, _
operation:=xlNone, skipblanks:=False, Transpose:=True