Global variable in userforms does not work - excel

I am creating userforms. Userforms are pretty connected to each other - use informations one from another. So... I thought about creating few global variables to make my life easier:
Public nazwa_arkusza As String
Public skoroszyt As Workbooks
Public arkusz As Worksheet
They are wrote in Useform Klient_kraj. Edytuj is Combobox within Klient_kraj Userform. I want to execute arkusz variable in different userform, but I get run-time error: "Object does not support this method"
Private Sub but_next_Click()
Dim Faktura As Range, faktury_range As Range
Dim LastRow As Integer
LastRow = Klient_kraj.skoroszyt.arkusz.Cells(Rows.Count, 1).End(xlUp).Row 'error line
Set faktury_range = skoroszyt.Range("A1:A" & LastRow)
(...)
end sub
.
Private Sub edytuj_Click()
Dim nazwa As String
nazwa_arkusza = kraj.List(kraj.ListIndex, 1) & " " & Mid(okres1.Value, 4, 2) & Mid(okres2.Value, 3, 3)
nazwa = "C:\1\" & klient.Text & ".xlsx"
'Jeżeli kraj nie wybrany = msgbox
If kraj.Value = "" Then
MsgBox ("Nie wybrałeś kraju")
Exit Sub:
Else
If okres1.Value = "" Or okres2.Value = "" Then
MsgBox ("Nie wybrałeś okresu rozliczeniowego")
Exit Sub:
End If
End If
'Jeżeli nie ma pliku - utwórz nowy
If Dir(nazwa) = "" Then
Workbooks.Add(1).SaveAs Filename:="C:\1\" & klient.Text, FileFormat:=51
Worksheets(1).Name = nazwa_arkusza
Else
'Jeżeli nie jest otwarty - otwórz
On Error GoTo niema_pliku:
If GetObject(, "Excel.Application").Workbooks(Klient_kraj.klient.Text & ".xlsx") Is Nothing Then
Workbooks.Open Filename:="C:\1\" & klient.Text & ".xlsx"
Else
Workbooks(Klient_kraj.klient.Text).Activate
End If
End If
Set skoroszyt = Workbooks(Klient_kraj.klient.Text & ".xlsx")
'Jeżeli arkusz nie istnieje - utwórz; istnieje - aktywuj
On Error GoTo niema_arkusza:
If skoroszyt.Worksheets(nazwa_arkusza).Name = "" Then
Else
skoroszyt.Worksheets(nazwa_arkusza).Activate
End If
On Error GoTo 0
Set arkusz = Sheets(nazwa_arkusza)
Application.Windows(klient.Text & ".xlsx").Visible = False
Faktura.Show
niema_pliku:
If Err.Number = 9 Then
Resume Next
End If
niema_arkusza:
If Err.Number = 9 Then
skoroszyt.Worksheets.Add.Name = nazwa_arkusza
skoroszyt.Worksheets(nazwa_arkusza).Activate
Resume Next
End If
End Sub
This variable will also be used in different userform.
What am I doing wrong?

Related

Excel MaxVal + 1 not ticking up when document is finished

im currently having an error code 1004 with VBA for excel, I'm not too familiar with the program but I have been able to determine the issue with the code although i definitely do not know how to fix it.
TLDR on what the code is supposed to do;
once the form is filled copy relevant data to a separate workbook
send an email to the relevant party of new entry
save the entry as a PDF
reset the workbook with the ticket number + 1 to mark up
the issue lies within the last step, once the first PDF file was created the workbook will no longer save as the ticker is stuck on Ticket# 1
Application.ScreenUpdating = False
If Range("H3").Value = "" Then MsgBox "Please Enter Device Serial Number"
Range("H3").Select
If Range("H3").Value = "" Then Exit Sub
If Range("M3").Value = "" Then MsgBox "Please Enter Reference Standard ID"
Range("M3").Select
If Range("M3").Value = "" Then Exit Sub
If Range("K9").Value = "" Then MsgBox "Please Enter Atleast One Dimensional Check"
Range("K9").Select
If Range("K9").Value = "" Then Exit Sub
If Range("Q9").Value = "" Then MsgBox "Please Enter Visual Check for Damage"
Range("Q9").Select
If Range("Q9").Value = "" Then Exit Sub
If Range("U9").Value = "" Then MsgBox "Please Enter Inital for Damage Check"
Range("U9").Select
If Range("U9").Value = "" Then Exit Sub
If Range("Q10").Value = "" Then MsgBox "Please Enter Visual Check for Wear"
Range("Q10").Select
If Range("Q10").Value = "" Then Exit Sub
If Range("U10").Value = "" Then MsgBox "Please Enter Inital for Wear Check"
Range("U10").Select
If Range("U10").Value = "" Then Exit Sub
If Range("Q11").Value = "" Then MsgBox "Please Enter Visual Check for Travel"
Range("Q11").Select
If Range("Q11").Value = "" Then Exit Sub
If Range("U11").Value = "" Then MsgBox "Please Enter Inital for Travel Check"
Range("U11").Select
If Range("U11").Value = "" Then Exit Sub
If Range("Q12").Value = "" Then MsgBox "Please Enter Visual Check for Zero"
Range("Q12").Select
If Range("Q12").Value = "" Then Exit Sub
If Range("U12").Value = "" Then MsgBox "Please Enter Inital for Zero Check"
Range("U12").Select
If Range("U12").Value = "" Then Exit Sub
If Range("Q13").Value = "" Then MsgBox "Please Enter Visual Check for Repeatability"
Range("Q13").Select
If Range("Q13").Value = "" Then Exit Sub
If Range("U13").Value = "" Then MsgBox "Please Enter Inital for Repeatability Check 3x"
Range("U13").Select
If Range("U13").Value = "" Then Exit Sub
If Range("C23").Value = "True" Then MsgBox "Please Check Final Verification Pass or Fail"
If Range("C23").Value = "True" Then Exit Sub
Workbooks.Open "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\VerificationData(DONOTDELETE).xlsx"
Application.Run (["GetMax"])
Application.Run (["SavePrintEmail"])
Application.Run (["CopyClear"])
Application.ScreenUpdating = True
End Sub
Private Sub GetMax()
Dim WorkRange As Range
Dim MaxVal As Double
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set WorkRange = ActiveWorkbook.Worksheets("Data").Range("AK:AK")
MaxVal = WorksheetFunction.Max(WorkRange)
Workbooks("PIV-001.xlsm").Activate
ActiveWorkbook.Worksheets("PIV-001").Unprotect ("Moldamatic")
ActiveWorkbook.Worksheets("PIV-001").Range("U21").Value = MaxVal + 1
End Sub
Private Sub SavePrintEmail()
ThisWorkbook.Save
If Len(Dir("\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date), vbDirectory)) = 0 Then
MkDir "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date)
End If
Sheets("PIV-001").Select
Sheets("PIV-001").ExportAsFixedFormat xlTypePDF, "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date) & "\" & Range("U21").Value & "-" & Year(Date), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)
On Error Resume Next
With OutMail
.To = "Spage#moldamatic.com"
.CC = ""
.BCC = ""
.Subject = "NEW INSTRUMENT VERFICATION (TICKET# " & Range("U21").Value & " INSTRUMENT ID# " & Range("H3").Value & " RESULT: " & Range("H22").Value & ")"
.HTMLBody = "An instrument has just been verfied, please see attached verification report. Verficiation results: " & Range("H22").Value & " "
.Attachments.Add "\\192.168.150.31\Quality Control\Calibration\Periodic Verification\" & Year(Date) & "\" & Range("U21").Value & "-" & Year(Date) & ".pdf"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
ThisWorkbook.Save
End Sub
Private Sub CopyClear()
'Change path to database in line below
Dim historyWks As Worksheet
Dim inputWks As Worksheet
Dim nextRow As Long
Dim oCol As Long
Dim myRng As Range
Dim myCopy As String
Dim myCell As Range
'cells to copy from Input sheet - some contain formulas
myCopy = "U21,C22,E22,H3,M3,B9,F9,I9,K9,B10,F10,I10,K10,B11,F11,I11,K11,B12,F12,I12,K12,B13,F13,I13,K13,Q9,U9,Q10,U10,Q11,U11,Q12,U12,Q13,U13,G17"
Set inputWks = ThisWorkbook.Worksheets("PIV-001")
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set historyWks = ActiveWorkbook.Worksheets("Data")
With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With
With inputWks
Set myRng = .Range(myCopy)
End With
With historyWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
.Cells(nextRow, "B").Value = Application.UserName
oCol = 3
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With
Workbooks("PIV-001.xlsm").Activate
Range("H3,M3,B9,F9,K9,B10,F10,K10,B11,F11,K11,B12,F12,K12,B13,F13,K13,Q9,U9,Q10,U10,Q11,U11,Q12,U12,Q13,U13,G17").Select
Selection.ClearContents
ActiveSheet.CheckBoxes.Value = False
Range("H3:L3").Select
ThisWorkbook.Worksheets("PIV-001").Protect ("Moldamatic")
Workbooks("PIV-001.xlsm").Save
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Workbooks("VerificationData(DONOTDELETE).xlsx").Save
Workbooks("VerificationData(DONOTDELETE).xlsx").Close
End Sub
the issue laid within the MaxVal string, changed the code to get rid of it
new code
Private Sub GetMax()
Dim WorkRange As Range
Workbooks("VerificationData(DONOTDELETE).xlsx").Activate
Set WorkRange = ActiveWorkbook.Worksheets("Data").Range("AK:AK")
Workbooks("PIV-001.xlsm").Activate
ActiveWorkbook.Worksheets("PIV-001").Unprotect ("Moldamatic")
ActiveWorkbook.Worksheets("PIV-001").Range("U21").Value =
Range("U21").Value + 1
End Sub

Error code:-2147024809 (80070057) "Cound not find specific object"

I keep getting this error for some code I am adapting. I have used this in other workbooks without issue as shown. The line "Me.Controls("Reg" & X).Value = findvalue" is where I am getting stuck. I use this throughout my project again without issue in other projects. Any ideas?
Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Search new and existing training and return data to user controls at the bottom of the form
'declare the variables
Dim ID As String
Dim I As Integer
Dim findvalue
'error block
On Error GoTo errHandler:
'get the select value from the listbox
For I = 0 To lstLookup.ListCount - 1
If lstLookup.Selected(I) = True Then
'set the listbox column
ID = lstLookup.List(I, 8)
End If
Next I
'find the value in the range
Set findvalue = Sheet2.Range("J:J").Find(What:=ID, LookIn:=xlValues).Offset(0, -8)
'add the values to the userform controls
cNum = 9
For X = 1 To cNum
**Me.Controls("Reg" & X).Value = findvalue**
Set findvalue = findvalue.Offset(0, 1)
Next
'disable controls force user to select option
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub

VBA: Range.Formula Returns Application Defined or Object Defined Error

For my code, I am trying to find the difference between an objects value from two different days.
Sub GoingBack()
numberCube = InputBox("Which file are we going back to?")
numberYest = numberCube - 1
Workbooks.Open ("C:\Users\user\Downloads\file (" & numberCube & ").xlsx")
Workbooks.Open ("C:\Users\user\Downloads\file (" & numberYest & ").xlsx")
Set Work1 = Workbooks("file (" & numberCube & ").xlsx")
Set Work2 = Workbooks("file (" & numberCube - 1 & ").xlsx")
'Add the Time Difference Column (AA--27)
LastRow67 = Work1.Sheets("67").Cells(Rows.Count, 2).End(xlUp).Row
Work1.Sheets("67").Cells(1, 27).Value = "Time Clock Difference"
Work1.Sheets("67").Cells(1, 27).FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21], '[file (" & numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"
Work1.Sheets("67").Range("AA2").Select
Selection.AutoFill Destination:=Range("AA2:AA" & LastRow67)
Work1.Close savechanges:=True
Work2.Close savechanges:=True
End Sub
The line that is throwing the "Application Defined or Object Defined" error is:
Work1.Sheets("67").Cells(1, 27).FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21], '[file (" & numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"
I have tried using Range.Formula, and that threw the error as well.
Work1.Sheets("67").Range("AA2").Formula = "=L2-VLOOKUP(F2, '[file (" & numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"
Any help would be appreciated. Thank you so much.
EDIT: I typed in the formula in Excel, and it works. I recorded the inputting of the formula, and the below is the result. I clicked/referenced columns F through L, so I'm not sure why it is only displaying C6:C12 below.
ActiveCell.FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21],'[file.xlsx]67'!C6:C12,7,FALSE)"
Just because you've always done something a certain way, doesn't make it good practice. There are a lot of opportunities for improvement here.
Consider this refactor:
Option Explicit ' always!
Sub GoingBack()
' Dim all variables
Dim numberCube As Variant
Dim numberYest As Long
Dim Work1 As Workbook
Dim Work2 As Workbook
Dim LastRow67 As Long
Dim WorkSh1 As Worksheet
Dim WorkSh2 As Worksheet
Dim Pth As String
' avoid repeats of the same data
Pth = "C:\Users\user\Downloads\"
' Might be better to use FileDialog, but anyway...
' Handle user cancel and invalid entry
Do
numberCube = InputBox("Which file are we going back to?")
If numberCube = vbNullString Then
' User canceled, exit
Exit Sub
End If
If IsNumeric(numberCube) Then Exit Do
MsgBox "Enter a Number", vbCritical + vbOKOnly, "Error"
Loop
numberYest = numberCube - 1
' Handle files missing or won't open
On Error Resume Next
Set Work1 = Workbooks.Open(Pth & "file (" & numberCube & ").xlsx")
On Error GoTo 0
If Work1 Is Nothing Then
'Work1 failed to open, what now?
GoTo CleanUp
End If
On Error Resume Next
Set Work2 = Workbooks.Open(Pth & "file (" & numberYest & ").xlsx")
On Error GoTo 0
If Work2 Is Nothing Then
'Work2 failed to open, what now?
GoTo CleanUp
End If
' Set refences to worksheets and handle if missing
On Error Resume Next
Set WorkSh1 = Work1.Sheets("67")
On Error GoTo 0
If WorkSh1 Is Nothing Then
' WorkSh1 doesn't exist, what now?
GoTo CleanUp
End If
On Error Resume Next
Set WorkSh2 = Work2.Sheets("67")
On Error GoTo 0
If WorkSh2 Is Nothing Then
' WorkSh2 doesn't exist, what now?
GoTo CleanUp
End If
'Add the Time Difference Column (AA--27)
' use your references
With WorkSh1
LastRow67 = .Cells(.Rows.Count, 2).End(xlUp).Row
.Cells(1, 27).value = "Time Clock Difference"
' no need for select or Autofill
' Can't use A1 style in FormulaR1C1
.Range(.Cells(1, 27), .Cells(LastRow67, 27)).FormulaR1C1 = _
"=RC[-15]-VLOOKUP(RC[-21], " & WorkSh2.Range("F:L").Address(, , xlR1C1, True) & ", 7, FALSE)"
End With
CleanUp:
Work1.Close SaveChanges:=True
Work2.Close SaveChanges:=False ' you didn't change file 2
End Sub

VBA reference sheetname in excel

We have a excel file with a bunch of sheets. The first sheet is a "Search page" thing... where we want to type the name of the spreadsheet (for example in cell A1) we are looking for and then that would automatically pop up the right spreadsheet (within the same file).
I tried that, it didn't work at all :
Function ActivateWB(wbname As String)
'Open wbname.
Workbooks(wbname).Activate
End Function
Two code sets below
Add a full hyperlinked Table of Contents page
For your specific question re finding a sheet that is referred to by A1 on the first sheet see 'JumpSheet' code (at bottom)
Create TOC
Option Explicit
Sub CreateTOC()
Dim ws As Worksheet
Dim nmToc As Name
Dim rng1 As Range
Dim lngProceed As Boolean
Dim bNonWkSht As Boolean
Dim lngSht As Long
Dim lngShtNum As Long
Dim strWScode As String
Dim vbCodeMod
'Test for an ActiveWorkbook to summarise
If ActiveWorkbook Is Nothing Then
MsgBox "You must have a workbook open first!", vbInformation, "No Open Book"
Exit Sub
End If
'Turn off updates, alerts and events
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
'If the Table of Contents exists (using a marker range name "TOC_Index") prompt the user whether to proceed
On Error Resume Next
Set nmToc = ActiveWorkbook.Names("TOC_Index")
If Not nmToc Is Nothing Then
lngProceed = MsgBox("Index exists!" & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbCritical, "Warning")
If lngProceed = vbYes Then
Exit Sub
Else
ActiveWorkbook.Sheets(Range("TOC_Index").Parent.Name).Delete
End If
End If
Set ws = ActiveWorkbook.Sheets.Add
ws.Move before:=Sheets(1)
'Add the marker range name
ActiveWorkbook.Names.Add "TOC_INDEX", ws.[a1]
ws.Name = "TOC_Index"
On Error GoTo 0
On Error GoTo ErrHandler
For lngSht = 2 To ActiveWorkbook.Sheets.Count
'set to start at A6 of TOC sheet
'Test sheets to determine whether they are normal worksheets
ws.Cells(lngSht + 4, 2).Value = TypeName(ActiveWorkbook.Sheets(lngSht))
If TypeName(ActiveWorkbook.Sheets(lngSht)) = "Worksheet" Then
'Add hyperlinks to normal worksheets
ws.Hyperlinks.Add Anchor:=ws.Cells(lngSht + 4, 1), Address:="", SubAddress:="'" & ActiveWorkbook.Sheets(lngSht).Name & "'!A1", TextToDisplay:=ActiveWorkbook.Sheets(lngSht).Name
Else
'Add name of any non-worksheets
ws.Cells(lngSht + 4, 1).Value = ActiveWorkbook.Sheets(lngSht).Name
'Colour these sheets yellow
ws.Cells(lngSht + 4, 1).Interior.Color = vbYellow
ws.Cells(lngSht + 4, 2).Font.Italic = True
bNonWkSht = True
End If
Next lngSht
'Add headers and formatting
With ws
With .[a1:a4]
.Value = Application.Transpose(Array(ActiveWorkbook.Name, "", Format(Now(), "dd-mmm-yy hh:mm"), ActiveWorkbook.Sheets.Count - 1 & " sheets"))
.Font.Size = 14
.Cells(1).Font.Bold = True
End With
With .[a6].Resize(lngSht - 1, 1)
.Font.Bold = True
.Font.ColorIndex = 41
.Resize(1, 2).EntireColumn.HorizontalAlignment = xlLeft
.Columns("A:B").EntireColumn.AutoFit
End With
End With
'Add warnings and macro code if there are non WorkSheet types present
If bNonWkSht Then
With ws.[A5]
.Value = "This workbook contains at least one Chart or Dialog Sheet. These sheets will only be activated if macros are enabled (NB: Please doubleclick yellow sheet names to select them)"
.Font.ColorIndex = 3
.Font.Italic = True
End With
strWScode = "Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)" & vbCrLf _
& " Dim rng1 As Range" & vbCrLf _
& " Set rng1 = Intersect(Target, Range([a6], Cells(Rows.Count, 1).End(xlUp)))" & vbCrLf _
& " If rng1 Is Nothing Then Exit Sub" & vbCrLf _
& " On Error Resume Next" & vbCrLf _
& " If Target.Cells(1).Offset(0, 1) <> ""Worksheet"" Then Sheets(Target.Value).Activate" & vbCrLf _
& " If Err.Number <> 0 Then MsgBox ""Could not select sheet"" & Target.Value" & vbCrLf _
& "End Sub" & vbCrLf
Set vbCodeMod = ActiveWorkbook.VBProject.VBComponents(ws.CodeName)
vbCodeMod.CodeModule.AddFromString strWScode
End If
'tidy up Application settins
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
ErrHandler:
If Err.Number <> 0 Then MsgBox Err.Description & vbCrLf & "Please note that your Application settings have been reset", vbCritical, "Code Error!"
End Sub
Jump Sheet
Sub JumpSheet()
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(Sheets(1).[a1].Value)
On Error GoTo 0
If Not ws Is Nothing Then
Application.Goto ws.[a1]
Else
MsgBox "Sheet not found", vbCritical
End If
End Sub
Iterate over all sheets of the current workbook and activate the one with the right name. Here is some code which should give you the idea, You can put this in the code section of your search sheet and associate it with the "Clicked" event of a button.
Option Explicit
Sub Search_Click()
Dim sheetName As String, i As Long
sheetName = Range("A1")
For i = 1 To ThisWorkbook.Sheets.Count
If ThisWorkbook.Sheets(i).Name = sheetName Then
ThisWorkbook.Sheets(i).Activate
Exit For
End If
Next
End Sub
I am just confused about the question. Are you trying to open Workbook or Worksheet?.
If you trying to navigate to worksheet with in workbook,
E.g.
Worksheets("Sheet2").Activate

On Click Command Button Macro

I'm trying to create a macro for a command button that when clicked, will get the job number from that row and look for a file for that job. If it does not exist I want it to copy from a template and save with a new name, otherwise just open the file.
However, I cannot seem to work out how to get hold of the information for the command button that calls the macro. This is what I have so far:
Public Function ShapeExists(OnSheet As Object, Name As String) As Boolean
On Error GoTo ErrShapeExists
If Not OnSheet.Shapes(Name) Is Nothing Then
ShapeExists = True
End If
ErrShapeExists:
Exit Function
End Function
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim buttonName As String
buttonName = (Target.Row - 1)
If Not ShapeExists(ActiveSheet, buttonName) Then
If Range("O" & Target.Row).Value = "" And Target.Column <= 14 And Target.Row > 1 Then
ActiveSheet.Buttons.Add(910.5, Range("O" & Target.Row).Top, 80, 20).Select
Selection.Name = buttonName
Selection.OnAction = "Sheet1.JobButton"
ActiveSheet.Shapes(buttonName).Select
Selection.Characters.Text = "Open Job"
End If
End If
End Sub
Private Sub JobButton()
Dim newText As String
ActiveSheet.Shapes(Application.Caller).Select
If Range("N" & Selection.TopLeftCell.Row).Value <> "" Then
newText = "Job " & Range("N" & Selection.TopLeftCell.Row).Value
Dim checkFilename As String
Dim check As String
check = "N" & Selection.TopLeftCell.Row
checkFilename = newText & ".xlsm"
If Dir(checkFilename) <> "" Then
Workbooks.Open (newText)
Else
Dim SrcBook As Workbook
Set SrcBook = ThisWorkbook
Dim NewBook As Workbook
NewBook = Workbooks.Open("Job Template.xlsm")
SrcBook.Worksheets(1).Range("D" & Selection.TopLeftCell.Row).Copy
NewBook.Worksheets(2).Range("B15").PasteSpecial
With NewBook
.Title = newText
.Subject = newText
.SaveAs Filename:=newText
End With
End If
Else
ErrMsg:
MsgBox ("Job Should always have a number."), , "NO JOB NUMBER"
End If
End Sub
As you can see I am currently trying ActiveSheet.Shapes(Application.Caller).Select, this is causing a "Run-time error '13': Type mismatch".
Any help would be much appreciated, thank you!
Right-click the button --> View Code --> put your JobButton code here

Resources