Run-time error '91 - vba

I keep getting an error:
Run-time error '91; Object variable or with block variable not set.
My script runs fine and does what it needs to do but I can't figure out how to get rid of this error.
Thank you for the help.
Public Sub CommandButton1_Click()
Dim rng As Range
Set rng = Range("F24:I24")
rng.Select
If TextBox1.Text = "" Then
MsgBox ("Must insert Temperature you dingus!")
Else
rng = TextBox1.Text
Call GetCabinet1
End If
Unload Me
End Sub
Public Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Executive Summary")
wb.Activate
ws.Select
UserForm1.Show
Unload Me
End Sub

remove all those Unload.Me from both your subs and place it in the sub calling that userform
place a Me.Hide by the end of CommandButton1_Click(), instead
finally remove UserForm1.Show from UserForm_Initialize since it'd make it repeat twice
so your "Main" sub would look like:
Sub main()
Dim UF As UserForm1
Set UF = New UserForm1
UF.Show
Unload UF ' unload the userform from here
End Sub
and your userform1 code like:
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = Range("F24:I24")
rng.Select
If TextBox1.Text = "" Then
MsgBox ("Must insert Temperature you dingus!")
Else
rng = TextBox1.Text
Call GetCabinet1
End If
Me.Hide
End Sub
Public Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Executive Summary")
wb.Activate
ws.Select
End Sub

Simply replacing the unload me with me.hide fixed my problem... Thank you to all of those who gave their input....

Related

Displaying Workbook and Worksheet names in a Listview instead of Listbox

in WB1, I use the code above to display the opened workbook names in the Lisbox1 and their respective worksheets in listbox2 using a User form. But I would like to use Listview1 and Listview2 instead because I would like for every workbook and Worksheet name to show beside each one of them a checkbox, What changes should I do so it works in Listview1 and Listview2.
Option Explicit
Private Sub UserForm_Initialize()
Dim wb As Workbook
Me.Caption = "Workbooks and Sheets Detail"
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
End Sub
Private Sub ListBox1_Click()
Dim sWorkbookname As String
sWorkbookname = ListBox1.List(ListBox1.ListIndex)
ListWbWorksheets sWorkbookname
End Sub
Private Sub ListWbWorksheets(ByVal psWorkbookName As String)
Dim targetWb As Excel.Workbook
Dim n As Long
Set targetWb = Application.Workbooks(psWorkbookName)
ListBox2.Clear
For n = 1 To targetWb.Sheets.Count
ListBox2.AddItem targetWb.Sheets(n).Name
Next n
Set targetWb = Nothing
End Sub
It'll take some trial and error to learn how to use a Listview. This should give you a good start.
Private Sub ListView1_Click()
Dim ws As Worksheet
Dim item As ComctlLib.ListItem
ListView2.ListItems.Clear
For Each ws In Workbooks(ListView1.SelectedItem.Text).Worksheets
Set item = ListView2.ListItems.Add(Text:=ws.Name)
Next
End Sub
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim item As ComctlLib.ListItem
With ListView1
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Workbooks"
.ColumnHeaders.Add Text:="Paths"
End With
With ListView2
.View = lvwReport
.MultiSelect = False
.ColumnHeaders.Add Text:="Worksheets"
End With
For Each wb In Workbooks
Set item = ListView1.ListItems.Add(Text:=wb.Name)
item.SubItems(1) = wb.Path
Next
End Sub

In a form Click a Workbook name in listbox1 and see the Sheets of the WB in the Listbox2

In a User form I would like to display two listboxs . In the listbox1 I would display the Opened Workbooks with the Exception of the Personal Workbook; and whenever I click one of the Workbooks in the Listbox1 I would like to display the Worksheets available in that workbook in the Listbox2.
Doing some research I found the following code which represent how far I have been able to go about this:
Private Sub UserForm_Initialize()
UserForm1.Caption = "Workbooks and Sheets"
Dim wb As Workbook
Dim n As Long
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
For n = 1 To ActiveWorkbook.Sheets.Count
ListBox2.AddItem ActiveWorkbook.Sheets(n).Name
Next n
End Sub
Also I found this Post in Here but I should be doing something wrong because when I run the code both list box appear without any content whatsoever. Do you know How could I get this code to Work?
Thank you
You should create a subroutine that will refresh both list. In this way, you can keep the open Workbook list up to date.
Private Sub ListBox1_Click()
RefreshListBoxes
End Sub
Private Sub UserForm_Initialize()
RefreshListBoxes
End Sub
Sub RefreshListBoxes()
Dim wb As Workbook, ws As Worksheet
ListBox2.Clear
If ListBox1.ListIndex > -1 Then
On Error Resume Next
Set wb = Workbooks(ListBox1.Value)
On Error GoTo 0
If wb Is Nothing Then
MsgBox "Workbook not found: " & ListBox1.Value, vbCritical, "Try Again"
Else
For Each ws In wb.Worksheets
ListBox2.AddItem ws.Name
Next
End If
End If
ListBox1.Clear
For Each wb In Workbooks
If Not wb.FullName Like "*Excel\XLSTART\PERSONAL.XL*" Then ListBox1.AddItem wb.Name
Next
End Sub
Here's some code to get you started:
Option Explicit
Private Sub UserForm_Initialize()
Dim wb As Workbook
Me.Caption = "Workbooks and Sheets"
For Each wb In Application.Workbooks
ListBox1.AddItem wb.Name
Next wb
'Selecting item 0 (zero), i.e. the list's top element, will trigger its Click event,
'which in turn will call ListWbWorksheets, populating ListBox2.
ListBox1.Selected(0) = True
End Sub
Private Sub ListBox1_Click()
Dim sWorkbookName As String
sWorkbookName = ListBox1.List(ListBox1.ListIndex)
ListWbWorksheets sWorkbookName
End Sub
Private Sub ListWbWorksheets(ByVal psWorkbookName As String)
Dim targetWb As Excel.Workbook
Dim n As Long
Set targetWb = Application.Workbooks(psWorkbookName)
ListBox2.Clear
For n = 1 To targetWb.Sheets.Count
ListBox2.AddItem targetWb.Sheets(n).Name
Next n
Set targetWb = Nothing
End Sub
Note that the code above doesn't check for any errors; #Thomas Inzina's answer at least checks whether a workbook's name leads to an actual workbook.

If statement to delete tab if there but move on if page is not there

I have a code that deletes a tab in the worksheet then runs another code. I am currently running into an issue that if the sheet is not there the code gives me an error... I'm wondering if I could make an if statement that looks if the tab is there and if not it moves on and if it is there it will delete it. I have the code that I have written already posted below but I have no idea how to do the if in the delete section.
Thanks!
Sub delete()
Dim ws As Worksheet
Set ws = Worksheets("Workbench Report")
Application.DisplayAlerts = False
ws.delete
Call Sorting
End Sub
Check if the sheet exists first:
Sub delete()
Dim ws As Worksheet
If WorksheetExists("Workbench Report") Then
Set ws = Worksheets("Workbench Report")
Application.DisplayAlerts = False
ws.delete
Call Sorting
End If
End Sub
Public Function WorkSheetExists(SheetName As String, Optional WrkBk As Workbook) As Boolean
Dim wrkSht As Worksheet
If WrkBk Is Nothing Then
Set WrkBk = ThisWorkbook
End If
On Error Resume Next
Set wrkSht = WrkBk.Worksheets(SheetName)
WorkSheetExists = (Err.Number = 0)
Set wrkSht = Nothing
On Error GoTo 0
End Function
Try this
Sub delete()
Dim i As Integer
i = 1
Application.DisplayAlerts = False
While i <= ActiveWorkbook.Worksheets.Count
Sheets(i).Select
If ActiveSheet.Name = "Workbench Report" Then
ActiveSheet.delete
End If
i = i + 1
Wend
Call Sorting
Application.DisplayAlerts = True
End Sub

Capture cell value with TextBox in UserForm

I have a UserForm which should be able to copy paste cells ideally. So firstly I would click the range I would want to copy, then activate the UserForm. The UserForm would have a combo box to choose which sheet I want to paste the data in, thereafter it would go to that sheet and user will click on the range or cell where he wants the data to be pasted.
I originally did an input box code to do this and it works perfectly, however when I do it in the UserForm it does not work as I am not able to incorporate the Type:=8 code in the textbox. Hence I would need some help on how can I enable my UserForm to paste cell data on the sheet, similarly to what I have done in application.inputbox.
This is the perfectly working code in the form of an input box:
Sub CopyPasteCumUpdateWithinSameSheet()
Dim rng As Range
Dim inp As Range
Selection.Interior.ColorIndex = 37
Set inp = Selection
On Error Resume Next
Set rng = Application.InputBox("Copy to", Type:=8)
On Error GoTo 0
If TypeName(rng) <> "Range" Then
Exit Sub
Else
inp.Copy
rng.Select
ActiveSheet.Paste Link:=True
'Cells(1,2).Font.ThemeColor =
End If
End Sub
This is the UserForm I have tried:
Dim Sh As Worksheet
Private Sub CommandButton1_Click()
On Error GoTo 0
If TypeName(rng) <> "Range" Then
Exit Sub
Else
inp.Copy
rng.Select
ActiveSheet.Paste Link:=True
End If
End Sub
Private Sub UserForm_Initialize()
CopyPasteUserform.Show vbModeless
For Each Sh In ThisWorkbook.Sheets
If Sh.Name <> "Inputs" Then
ComboBox1.AddItem Sh.Name
End If
Next
ComboBox1.Style = fmStyleDropDownList
End Sub
Private Sub ComboBox1_Change()
With ThisWorkbook.Sheets(ComboBox1.Text)
.Visible = xlSheetVisible
.Activate
End With
End Sub
Private Sub TextBox1_Change()
Dim rng As Range
Dim inp As Range
Selection.Interior.ColorIndex = 37
Set inp = Selection
On Error Resume Next
Set rng = TextBox.Value
End Sub
I tried incorporating the UserForm but all other functions stop responding apart from the RefEdit.
Dim Sh As Worksheet
Private Sub UserForm_Initialize()
CopyPasteUserform.Show vbModeless
For Each Sh In ThisWorkbook.Sheets
If Sh.Name <> "Inputs" Then
ComboBox1.AddItem Sh.Name
End If
Next
ComboBox1.Style = fmStyleDropDownList
Dim rng As Range
Dim inp As Range
Selection.Interior.ColorIndex = 37
Set inp = Selection
End Sub
Private Sub Combobox1_Change()
With ThisWorkbook.Sheets(ComboBox1.Text)
.Visible = xlSheetVisible
.Activate
End With
End Sub
Private Sub RefEdit1_Change()
Label1.Caption = ""
If RefEdit1.Value <> "" Then _
Label1.Caption = "[" & ComboBox1 & "]" & RefEdit1
Dim rng As Range
Dim inp As Range
On Error Resume Next
Set rng = RefEdit1.Value
On Error GoTo 0
If TypeName(rng) <> "Range" Then
Exit Sub
Else
inp.Copy
rng.Select
ActiveSheet.Paste Link:=True
End If
End Sub
You do not need the combobox to navigate to the sheets. That is the beauty of the Refedit
Is this what you are trying? I have not done any error handling. I am sure you can take care of that.
Create a userform and place 2 labels, 2 refedits and 1 commandbutton as shown below
Next paste this code in the userform code area
Code
Private Sub CommandButton1_Click()
Dim rngCopy As Range, rngPaste As Range
Dim wsCopy As Worksheet, wsPaste As Worksheet
If RefEdit1.Value <> "" And RefEdit2.Value <> "" Then
Set wsCopy = ThisWorkbook.Sheets(Replace(Split(RefEdit1.Value, "!")(0), "'", ""))
Set rngCopy = wsCopy.Range(Split(RefEdit1.Value, "!")(1))
Set wsPaste = ThisWorkbook.Sheets(Replace(Split(RefEdit2.Value, "!")(0), "'", ""))
Set rngPaste = wsPaste.Range(Split(RefEdit2.Value, "!")(1))
rngCopy.Copy rngPaste
Else
MsgBox "Please select Input and Output range"
End If
End Sub
In Action
The data will be copied from Sheet1!$A$1:$A$3 to Sheet2!$A$1:$A$3
Followup From Comments
However the pastelink feature has been missed out in the userform. Is it possible to incorporate it?:) – Niva 7 mins ago
Add a checkbox to the form as shown below
Use this code
Private Sub CommandButton1_Click()
Dim rngCopy As Range, rngPaste As Range
Dim wsCopy As Worksheet, wsPaste As Worksheet
If RefEdit1.Value <> "" And RefEdit2.Value <> "" Then
Set wsCopy = ThisWorkbook.Sheets(Replace(Split(RefEdit1.Value, "!")(0), "'", ""))
Set rngCopy = wsCopy.Range(Split(RefEdit1.Value, "!")(1))
Set wsPaste = ThisWorkbook.Sheets(Replace(Split(RefEdit2.Value, "!")(0), "'", ""))
Set rngPaste = wsPaste.Range(Split(RefEdit2.Value, "!")(1))
If CheckBox1.Value = True Then
wsPaste.Activate
rngPaste.Select
rngCopy.Copy
ActiveSheet.Paste Link:=True
Else
rngCopy.Copy rngPaste
End If
Else
MsgBox "Please select Input and Output range"
End If
End Sub
Description: Type:=8 will check that user input correct range name or not? In UserForm the TextBox not have this function. But we can detect this error when user click button. see my code.
No need to check when textbox is change, I delete code of textbox_change.
Replace below in your user form code area.
Option Explicit
Dim Sh As Worksheet
Dim inp As Range
Dim rng As Range
Private Sub CommandButton1_Click()
ActiveCell.Value = Me.TextBox1.Text
'On Error Resume Next
'If TypeName(Range(Me.TextBox1.Text)) <> "Range" Then
' MsgBox "Invalid range name!", vbCritical
' Exit Sub
'Else
' inp.Copy
' rng.Select
'
' ActiveSheet.Paste Link:=True
' MsgBox "Copy and paste finish.", vbInformation
'End If
'On Error GoTo 0
End Sub
Private Sub UserForm_Initialize()
For Each Sh In ThisWorkbook.Sheets
If Sh.Name <> "Inputs" Then
ComboBox1.AddItem Sh.Name
End If
Next
ComboBox1.Style = fmStyleDropDownList
End Sub
Private Sub ComboBox1_Change()
With ThisWorkbook.Sheets(ComboBox1.Text)
.Visible = xlSheetVisible
.Activate
End With
End Sub

"Method or Data Member Not Found" when Dim'ing Worksheet as Worksheet but not Variant

Consider this simple example. In a new sheet create a ActiveX Checkbox called Checkbox1
Try the following two subroutines. The first does not compile with a "Method or Data Member Not Found" error, the second one works fine.
Why doesn't the first example work?
Option Explicit
Sub DoesntWork()
Dim ws As Worksheet
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " + CStr(ws.CheckBox1.Value)
End Sub
Sub Works()
Dim ws As Variant
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " + CStr(ws.CheckBox1.Value)
End Sub
The problem is with the line ws.CheckBox1.Value. You can't use it like this and hence you are getting that error. Try this
Sub Sample()
Dim ws As Worksheet
Dim objole As OLEObject
Set ws = Worksheets(1)
Set objole = ws.OLEObjects("CheckBox1")
MsgBox "Checkbox state is: " & objole.Object.Value
End Sub
If you want to use the Object directly then you can also use this
Sub Sample()
Dim ws As Worksheet
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " & Worksheets(ws.Name).CheckBox1.Value
End Sub