Error while trying to copy Worksheet - vba

I am having the error "Copy Method of Worksheeet class failed" on this line:
.Sheets("Blank Forecast Sheet").Copy After:=.Sheets("Button Sheet")
I've looked around and couldn't find any solution. This code is ,as can be seen, supposed to add new sheets that I will rename, once, I get this problem solved.
Sub addnewsheet()
Dim wbook As Workbook
Set wbook = Application.ActiveWorkbook
Dim newsheet As Worksheet
Dim datasheet As Worksheet
Dim m As String
Dim y As Integer
m = Format(Date, "mmmm")
y = Format(Date, "yyyy")
With wbook
.Sheets("Blank Forecast Sheet").Copy After:=.Sheets("Button Sheet")
End With
End Sub

Use ThisWorkbook instead of wbook and .Worksheets instead of .Sheets:
Sub addnewsheet()
Dim newsheet As Worksheet
Dim datasheet As Worksheet
Dim m As String
Dim y As Integer
m = Format(Date, "mmmm")
y = Format(Date, "yyyy")
With ThisWorkbook
.Worksheets("Blank Forecast Sheet").Copy After:=.Worksheets("Button Sheet")
End With
End Sub

Related

Copy content from closed work book column

As said in the title I'm trying to copy data from a column into a new workbook as its a weekly report where the data I add in this column remains valid.
Sub copyColData00()
Dim lastRow As Long
Dim myApp As Excel.Application
Dim wkBk As Workbook
Dim wkSht As Object
Dim mnt As String
Set myApp = CreateObject("Excel.Application")
mnt = InputBox("Enter Filename")
Set wkBk = Workbooks.Open("\\n\Documents\" & mnt & ".xlsx")
lastRow = wkBk.Sheets(1).Range("R" & Rows.Count).End(xlUp).Row
wkBk.Sheets(1).Range("R1:R" & lastRow).Copy
myApp.DisplayAlerts = False
wkBk.Close
myApp.Quit
Set wkBk = Nothing
Set myApp = Nothing
Set wkBk = ActiveWorkbook
Set wkSht = wkBk.Sheets("Sheet1")
wkSht.Activate
Range("R1").Select
wkSht.Paste
Exit Sub
End Sub
My problem is that I want it to past it directly while there I get a prompt that ask if I want to copy all the data in the clip board or not and my second problem is that at
Set wkSht = wkBk.Sheets("Sheet1")
It gives me the error subscript out of range I've trouble understanding what happens there if someone could help it would be nice!
This is a lot easier way to do that:
Sub copyColData00()
Dim lastRow As Long
Dim wkBk1 As Workbook, wkBk2 As Workbook
Dim wkSht As Object
Dim mnt As String
mnt = InputBox("Enter Filename")
Set wkBk1 = ActiveWorkbook
Set wkBk2 = Workbooks.Open("\\n\Documents\" & mnt & ".xlsx")
lastRow = wkBk2.Sheets(1).Range("R" & Rows.Count).End(xlUp).Row
wkBk1.Sheets(1).Range("R1:R" & lastRow).Value = wkBk2.Sheets(1).Range("R1:R" & lastRow).Value 'change which sheet you want for wkBk1
wkBk2.Close
End Sub

I get a Run-time Error #1004 for my VBA code with a Named range?

why does this code run fine:
Sub SelectRange()
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim sourceSheetSum As Worksheet
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("Tabelle1")
ActiveWorkbook.Names.Add _
Name:="ggg", _
RefersTo:="=Sheet1!A4:L37"
sourceSheet.Select
sourceSheet.Range("A4:L37").Select
End Sub
However if I change
sourceSheet.Range("A4:L37").Select
to:
sourceSheet.Range("ggg").Select
I receive a run-time error 1004
Try the code below, it will create "ggg" named range from cells "A4:L37" in "Tabelle1" sheet.
Afterwards, it sets another Range MyNamedRange to the named Range("ggg") - this step is not necessary, I just like to work with variables for Range.
At the end, it selects MyNamedRange.
Code
Sub SelectRange()
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim sourceSheetSum As Worksheet
Dim MyNamedRange As Range
Set sourceBook = ActiveWorkbook
Set sourceSheet = sourceBook.Sheets("Tabelle1")
' create the named range "ggg"
sourceBook.Names.Add _
Name:="ggg", _
RefersTo:="=" & sourceSheet.Name & "!A4:L37"
Set MyNamedRange = Range("ggg") ' <-- set the Range to your NamedRange "ggg"
sourceSheet.Activate '<-- activate the sheet first
MyNamedRange.Select '<-- select the Named Range
End Sub
Try this:
SourceSheet.Range(Names.Item("ggg")).Select

What update will open my workbook to today's date?

I would like my workbook to open on the cell of today's date. The dates of 2017 are listed in row 10. My macro is listed below but contains a syntax error on the line
'x = Format(Date, "Short Date")'
Private Sub Workbook_OpenDate()
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = Workbooks("2017 Capacity Planner.xlsm")
Worksheets("Dashboard").Select
x = Format(Date, "Short Date")
On Error Resume Next
Worksheets(“Dashboard”).Row(10).Find(What:=x, LookIn:=xlValues).Activate
Application.Goto Selection, True
End Sub
You are still using magic quotes. Even if you remove that you will Error 438 on the line .Row(10). Then on the find another error is waiting when nothing is found.Change your code like this :
Sub test()
Dim rng As Range
Dim x As String
Dim wb As Workbook, wb1 As Workbook
Dim LastRow As Long
Set wb = Workbooks("2017 Capacity Planner.xlsm")
Worksheets("Dashboard").Select
x = (Format(Date, "Short Date"))
Set rng = Worksheets("Dashboard").Rows(10).Find(What:=x, LookIn:=xlValues)
If Not rng Is Nothing Then
rng.Parent.Activate
rng.Select
End If
End Sub

Copy Worksheet from one workbook to another. Type Mismatch 13

I'm looking to copy just one worksheet from a workbook to another and then change the name. Nothing hugely fancy.
I keep getting a type-mismatch error and I don't know why. I've used code from others which they say works but just doesn't for me.
Any ideas.
Private Sub cmdStockLog_Click()
week = ThisWorkbook.Sheets("Stock Sheet").Range("F1")
Dim wb As Workbook
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Data\FOH Stock.xlsx")
ThisWorkbook.Sheets("Stock Sheet").Copy After:=Workbooks(wb).Sheets(Worksheets.Count)
ActiveSheet.Name = "WK" & week
End Sub
Try this code:
Private Sub cmdStockLog_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim week
week = ThisWorkbook.Sheets("Stock Sheet").Range("F1")
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Data\FOH Stock.xlsx")
ThisWorkbook.Sheets("Stock Sheet").Copy After:=wb.Sheets(wb.Sheets.Count)
Set ws = wb.Sheets(wb.Sheets.Count) ' new sheet
ws.Name = "WK" & week
End Sub

getting error "Object Variable or With block variable not set"

I have created an Add-In for Excel which determines the name of ActiveSheet and ActiveWorkbook. The code I used is below. When I run the Add-In it is showing the above mentioned error after the message box "variables set". But when I run it in macros it is working fine. I don't understand what is happening with the Add-In. Could anyone help me with this?
Sub sheetvalues()
Dim bk As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
Dim book As String, sht As String, i As Integer, j As Integer
Dim att(1 To 4) As String, att_col(1 To 4) As Integer
MsgBox ("variables set")
book = ActiveWorkbook.Name
sht = ActiveSheet.Name
MsgBox ("names set")
Set bk = Workbooks.Add
With bk
.Title = "MissingValues"
.SaveAs Filename:="MissingValues.xls"
End With
Set sht1 = bk.Sheets.Add
sht1.Name = "EndOne"
Set sht2 = bk.Sheets.Add
sht2.Name = "EndTwo"
Set sht3 = bk.Sheets.Add
sht3.Name = "EndThree"
MsgBox (book & " " & sht)
MsgBox ("completed")
End Sub
A common issue that causes this issue is forgetting to use 'Set' with assigning a value to a variable.
Check if the workbook in Excel is asking you if you want to open a write-protected version or not. I think while this question is present the workbook is not considered Active, nor is any other
Like #TimWilliams said, you will get this error if your add-in is the only workbook loaded. In that case there is no active workbook and your code is failing on the line
book = ActiveWorkbook.Name
You can check for the existence of a workbook by adding the following lines:
Set bk = Application.ActiveWorkbook
If bk Is Nothing Then
MsgBox ("No workbook open. Creating a new one.")
Set bk = Workbooks.Add(xlWBATWorksheet)
Set sht1 = bk.ActiveSheet
End If
So you end up with:
Sub sheetvalues()
Dim bk As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
Dim book As String, sht As String, i As Integer, j As Integer
Dim att(1 To 4) As String, att_col(1 To 4) As Integer
MsgBox ("variables set")
Set bk = Application.ActiveWorkbook
If bk Is Nothing Then
MsgBox ("No workbook open. Creating a new one.")
Set bk = Workbooks.Add(xlWBATWorksheet)
Set sht1 = bk.ActiveSheet
End If
book = ActiveWorkbook.Name
sht = ActiveSheet.Name
MsgBox ("names set")
Set bk = Workbooks.Add
With bk
.Title = "MissingValues"
.SaveAs Filename:="MissingValues.xls"
End With
Set sht1 = bk.Sheets.Add
sht1.Name = "EndOne"
Set sht2 = bk.Sheets.Add
sht2.Name = "EndTwo"
Set sht3 = bk.Sheets.Add
sht3.Name = "EndThree"
MsgBox (book & " " & sht)
MsgBox ("completed")
End Sub