I want to refer to another Workbook in my macro, so the function in the macro will also run over the other workbook
There is a button in one of the workbooks on one of the sheets that needs to cut and insert the whole row at the top of the list (row 2).it also needs to do this in one sheet of the a different workbook.
My main problem is with referring to the file. The main error I get are "Run-time error '9': subscript out of rang" or a "Object or method invalid"
The current code I have works until the comment.
Private Sub CommandButton2_Click()
Dim wbA As Workbook
Set wbA = Workbooks("MFDT Dashboard 3.0 PRE-alpha.xlsm")
'Loop through workbook
For Each ws In ThisWorkbook.Sheets
'Color cells
If ws.Rows(ActiveCell.Row).Interior.Color = RGB(255, 0, 0) Then
ws.Rows(ActiveCell.Row).Interior.Color = RGB(255, 255, 255)
Else
ws.Rows(ActiveCell.Row).Interior.Color = RGB(255, 0, 0)
End If
'Cut and insert
If Not ActiveCell.Row = 2 Then
ws.Rows(ActiveCell.Row).Cut
ws.Rows(2).Insert Shift:=xlDown
End If
Next ws
'after this it stops working.
wbA.Sheets("Enginlist").Rows(ActiveCell.Row).Cut
wbA.Sheets("Enginlist").Rows(2).Insert Shift:=xlDown
End Sub
You need to open the file to make changes in it.
replace
Set wbA = Workbooks("MFDT Dashboard 3.0 PRE-alpha.xlsm")
with
Set wbA = Workbooks.Open("PATH\MFDT Dashboard 3.0 PRE-alpha.xlsm") ' add path to file also
then at the end of the macro you need to close the file
wbA.close
But I always do it twice just to be sure, it has happened several times that a file is not closed.
on error resume next
wbA.close
wbA.close
on error goto 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.