Im making a new excel template where the managers can add info so that we can make a quote based on their template. The meaning is that if they are clicking on the submit button that depending on the value segment gos to the correct excel file (follow up list) and that the customer name, customer id and general info put in the follow up list.
This is de code that i have until now, only thing is the submit button that i need to have.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("B7")) Is Nothing Then
If Range("B7") <> "Server & Storage" And Range("B7") <> "Power" And Range("B7") <> "Networking" And Range("B7") <> "Software" And Range("B7") <> "Printing" Then
MsgBox "Selecteer een value segment!"
End If
Else
Exit Sub
End If
End Sub
'E-mail knop
Private Sub CommandButton1_Click()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim srtEmail As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi Team," & vbNewLine & vbNewLine & _
"Dit is een nieuwe request voor " & Range("B8")
If Range("B7") = "Server & Storage" Then
srtEmail = "hardware#bechtle.be"
Else
If Range("B7") = "Power" Then
srtEmail = "hardware#bechtle.be"
Else
If Range("B7") = "Networking" Then
srtEmail = "networking#bechtle.be"
Else
If Range("B7") = "Software" Then
srtEmail = "software#bechtle.be"
Else
If Range("B7") = "Printing" Then
srtEmail = "kristof.neubauer#bechtle.com"
Else
MsgBox "Geen value segment geselecteerd!"
End If
End If
End If
End If
End If
On Error Resume Next
With xOutMail
.To = srtEmail
.CC = "berty.vaneijgen#bechtle.com"
.BCC = ""
.Subject = "Value Request voor " & Range("B9") & Range("B8")
.Body = xMailBody
If Range("B7") <> "" Then
.Display 'or use .Send
End If
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
'Reset knop
Private Sub CommandButton2_Click()
Sheets("VRT").Range("B7:B33") = ""
MsgBox "Velden zijn gewist!"
End Sub
'Save as knop
Private Sub CommandButton3_Click()
Dim nom As String
nom = Day(Date) & "-" & Month(Date) & "-" & Year(Date) & " " & Range("B8")
ActiveWorkbook.SaveCopyAs ActiveWorkbook.Path & "\" & nom & ".xlsm"
rep = MsgBox("Je bestand is opgeslagen! ", vbYes + vbInformation, "Copy of spreadsheet")
'MsgBox(You database has been saved : " & Name, vbYes + vbInformation, "Copy of spreadsheet")
End Sub
'print
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = (ActiveSheet.Name = "VRT")
If Cancel = True Then MsgBox "Gebruik de print knop."
End Sub
'print knop
Private Sub CommandButton4_Click()
On Error Resume Next
Application.EnableEvents = False
With ActiveWorkbook.Sheets("VRT")
.Range("A1:F33").PrintOut
End With
Application.EnableEvents = True
On Error GoTo 0
End Sub
Private Sub CommandButton5_Click()
End Sub
You can open the developer tab and create a button. How to show developer tab on the ribbon MSDN.
Create an ActiveX button, by clicking the Developer Tab > Insert > Command Button:
Then make sure that the name of the button is CommandButton1, to be compatible with your code. This is where to rename the name of the button:
Since you mentioned that you have the code, and only need to add the button, these steps may be what you need to follow.
-Add a button from the 'Developer' tab using the Insert group
-Right-click on the button to 'Assign macro..'. You will see a list of subs
present in the workbook to select from.
-For the code use something like this
Dim src, dst as Workbook
dst= ThisWorkbook 'Destination is your current workbook
'Define the src inside your IF-ELSE tree based on 'B7' cell value
'Use the statement below for each cell value with address in the 'Range' quotes
dst.Range("").Value= src.Range("").Value
Related
I have some vba code, which I have attached to a excel button. When pressing the button the outlook mail box will appear- ready to send the mail. But for some reason the workbook is not attached to the mail. In the code below - I think something is wrong with add.attachments. at the end
My VBA code is:
Dim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D7"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 200 Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Attachments.Add "W\Desktop\Files\Workbook1
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
The following line of code is not complete:
.Attachments.Add "W\Desktop\Files\Workbook1
The Add method of the Attachments class creates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name), for example:
.Attachments.Add "C:\Test.xslx", olByValue, 1, "Test"
I'd also suggest specifying a local file path.
I have code that uses information from a set of text boxes in a userform to find and edit values in two workbooks. The code that I am using to edit the values in the second workbook gives me the following error, "Sorry an Error Occurred Object doesn't support this property or method". Can anyone help me with this? Aside from what is causing the error I think my code is correct but if anyone sees any errors in my code by all means please feel free to correct me or offer suggestions. Thanks in advance!
Private Sub Submit_Click()
Dim WS As Worksheet
Dim lastrow As Long
Dim r As Long
Dim password As String
Application.ScreenUpdating = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
password = TextBox1.Text
Set WS = ActiveWorkbook.Worksheets("Accounts")
lastrow = WS.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If WS.Cells(r, 2) = Label5.Caption Then
WS.Cells(r, 2).Value = TextBox1.Text
WS.Cells(r, 3).Value = TextBox2.Text
WS.Cells(r, 4).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
Call Edit_Login
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
End Sub
Private Sub Edit_Login()
Dim Wkbk As Workbook
Dim txt As String
Dim txt2 As String
Dim txt3 As String
Dim lastrow As Long
Dim r As Long
Dim Account As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
If Not IsNumeric(TextBox1.Text) Then
On Error GoTo ErrorHandler
Account = TextBox1.Text
Set Wkbk = Workbooks.Open("C:\Users\kameron\Desktop\Quality Improvement
Software\Log In.xlsm")
lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastrow
If Wkbk.Sheets("Tables").Cells(r, 1) = Label5.Caption Then
Wkbk.Sheets("Tables").Cells(r, 1).Value = TextBox1.Text
Wkbk.Sheets("Tables").Cells(r, 2).Value = TextBox2.Text
Wkbk.Sheets("Tables").Cells(r, 3).Value = TextBox3.Text
MsgBox "Update Successful", vbInformation
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Unload EditAccount
Application.ScreenUpdating = True
Exit Sub
End If
Next
MsgBox "Data not Found!!", vbCritical
TextBox1.Text = ""
Unload Me
Application.ScreenUpdating = True
Exit Sub
ErrorHandler: MsgBox "Sorry an Error occured. " & vbCrLf & Err.Description
Exit Sub
End If
MsgBox "Please Enter Correct Information", vbCritical
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
The problem is the line
lastrow = Wkbk.Cells(Rows.Count, "A").End(xlUp).Row
Workbook objects don't have a Cells property.
In context, you seem to want
lastrow = Wkbk.Sheets("Tables").Cells(Rows.Count, "A").End(xlUp).Row
In order to track down this error, you could have done one of two things:
1) Stepped through the code using F8 and see what line it fails on.
2) Temporarily commented out the line On Error GoTo ErrorHandler and run the code.
Either approach would have quickly led to that line.
I have a sub on a form which is intended to allow users to report bugs and suggest improvements to the form. I have it pretty much ready to go, but keep running into issues with adding attachments.
Sub Submit()
Dim OutApp As Object
Dim OutMail As Object
Dim Item
Dim STR As String, AdminOnly As String, TruncBox As String, STRAttachments As String
For Each cCont In Me.MultiPage1.SelectedItem.Controls
Select Case TypeName(cCont)
Case "TextBox"
If cCont.value = "Please enter a short description here." Or _
cCont.value = "Please enter a short description here." Then
MsgBox ("Please enter all information.")
Exit Sub
ElseIf cCont.value = "" Then
MsgBox ("Please enter all information.")
Exit Sub
End If
Case "ComboBox"
If cCont.value = "" Then
MsgBox ("Please enter all information.")
Exit Sub
ElseIf InStr(cCont.value, "Report") Then
TruncBox = "BUG"
Else
TruncBox = "SUGGESTION"
End If
End Select
Next
STR = "{email address redacted}"
If RecipientsListBox.ListCount = 0 Then
AdminOnly = MsgBox("Only admin will receive updates!", _
vbOKCancel + vbExclamation, "No Users on Watch List")
If AdminOnly = vbCancel Then
Exit Sub
Else
STR = STR
End If
Else
For Each Item In RecipientsListBox.List
STR = STR & ";" & Item
Next Item
End If
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = STR
Call .Recipients.resolveall
.Subject = TruncBox & ": " & ActiveWorkbook.Name & ": " & ShortDescriptionTextBox
.Body = LongDescriptionTextBox
If AttachmentsListBox.ListCount = 0 Then
Else
For Each Item In AttachmentsListBox.List
STRAttachments = Item
.Attachments.Add STRAttachments
Next Item
End If
'.Send 'Or use Display
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
I have run through various attempts at looping through each item on the AttachmentsListBox control, and ready to ask for help. This latest attempt produced Run-time error '94': Invalid use of Null with the line STRAttachments = Item returning null in the highlighted section. Looking at what I already have, and comparing to other searches on the internet, I don't understand. I returned variant Item to STR in the line STR = STR & ";" & Item above, and I've seen other examples of strings being returned as attachments. What am I missing?
So, this is a problem that I have identified in the past, but I haven't thoroughly researched the root cause. ListBox.List returns a multi-dimensional array of ListObjects. So, even though you have a ListBox with 1 column, the List array has multiple columns. When you loop over with a For Each loop, it tries to access values in these other columns, which just result in a Null value. Try using a For loop with a counter, such as:
Private Sub UserFormButton_Click()
For i = 0 To Me.ListBox1.ListCount - 1
MsgBox Me.ListBox1.List(i)
Next i
End Sub
Im seeking for help as i have a bulk of links to check if the link is broken i have tried the below macro but it works twice and after that it is no longer working i am using ms office 10 64bit i would like to add on the macro if macro
can check the image resolution for example if i paste url on column A it will highlight the broken links and on column b it will show the image resolution
Sub Audit_WorkSheet_For_Broken_Links()
If MsgBox("Is the Active Sheet a Sheet with Hyperlinks You Would Like to Check?", vbOKCancel) = vbCancel Then
Exit Sub
End If
On Error Resume Next
For Each alink In Cells.Hyperlinks
strURL = alink.Address
If Left(strURL, 4) <> "http" Then
strURL = ThisWorkbook.BuiltinDocumentProperties("Hyperlink Base") & strURL
End If
Application.StatusBar = "Testing Link: " & strURL
Set objhttp = CreateObject("MSXML2.XMLHTTP")
objhttp.Open "HEAD", strURL, False
objhttp.Send
If objhttp.statustext <> "OK" Then
alink.Parent.Interior.Color = 255
End If
Next alink
Application.StatusBar = False
On Error GoTo 0
MsgBox ("Checking Complete!" & vbCrLf & vbCrLf & "Cells With Broken or Suspect Links are Highlighted in RED.")
End Sub
Edit: I changed your macro to declare variables properly and release objects upon macro completion; this should address any potential memory issues. Please try this code and let me know if it works.
Sub Audit_WorkSheet_For_Broken_Links()
If MsgBox("Is the Active Sheet a Sheet with Hyperlinks You Would Like to Check?", vbOKCancel) = vbCancel Then
Exit Sub
End If
Dim alink As Hyperlink
Dim strURL As String
Dim objhttp As Object
On Error Resume Next
For Each alink In Cells.Hyperlinks
strURL = alink.Address
If Left(strURL, 4) <> "http" Then
strURL = ThisWorkbook.BuiltinDocumentProperties("Hyperlink Base") & strURL
End If
Application.StatusBar = "Testing Link: " & strURL
Set objhttp = CreateObject("MSXML2.XMLHTTP")
objhttp.Open "HEAD", strURL, False
objhttp.Send
If objhttp.statustext <> "OK" Then
alink.Parent.Interior.Color = 255
End If
Next alink
Application.StatusBar = False
'Release objects to prevent memory issues
Set alink = Nothing
Set objhttp = Nothing
On Error GoTo 0
MsgBox ("Checking Complete!" & vbCrLf & vbCrLf & "Cells With Broken or Suspect Links are Highlighted in RED.")
End Sub
Old Answer Below
Combining your macro (which seems to be from here) with an alternative found on excelforum yields the below code. Give it a try and let me know if it works for you.
Sub TestHLinkValidity()
Dim rRng As Range
Dim fsoFSO As Object
Dim strPath As String
Dim cCell As Range
If MsgBox("Is the Active Sheet a Sheet with Hyperlinks You Would Like to Check?", vbOKCancel) = vbCancel Then
Exit Sub
End If
Set fsoFSO = CreateObject("Scripting.FileSystemObject")
Set rRng = ActiveSheet.UsedRange.Cells
For Each cCell In rRng.Cells
If cCell.Hyperlinks.Count > 0 Then
strPath = GetHlinkAddr(cCell)
If fsoFSO.FileExists(strPath) = False Then cCell.Interior.Color = 65535
End If
Next cCell
End Sub
Function GetHlinkAddr(rngHlinkCell As Range)
GetHlinkAddr = rngHlinkCell.Hyperlinks(1).Address
End Function
I have a workbook that has 7 worksheets. I have the below vba to send an email once a value is met on a particular sheet.
Each sheet has a different value and a different attachment to be sent. How do I add a code for each sheet so the email is sent?
Thanks in advance
Set as General (Declarations)
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 3500 Then
Call Fuel_LevelW03
End If
End If
End Sub
followed by a module
General Fuel_LevelW03
Sub Fuel_LevelW03()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi" & vbNewLine & vbNewLine & _
"Please order fuel as attached." & vbNewLine & _
"" & vbNewLine & _
"Kind Regards" & vbNewLine & _
""
On Error Resume Next
With OutMail
.To = "email address"
.CC = "email address"
.BCC = ""
.Subject = "Fuel Order W03"
.Body = strbody
.Attachments.Add ("H:\Fuel Order Sheets\Glen Eden W03 Pump Station.xlsx")
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
From what I understand, you try to "tell the method" a bit about what the Target.Value was. Just pass the parameter to the function like this :
If IsNumeric(Target.Value) Then
If Target.Value < 3500 Then
Call Fuel_LevelW03( Sh.Name, Target.Value )
End If
End If
and change the function's name with this one :
Fuel_LevelW03( sheetName as String, targetValue as String )
'Change String to appropriate type
EDIT2 : I changed the code around a bit, if you need any help let me know.
EDIT : Ok, here's how you solve this. Inside the "ThisWorkbook" code object (underneath the sheet code objects, on the left side of the code editor), paste this :
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 3500 Then
Call Fuel_LevelW03( Sh.Name )
End If
End If
End Sub
Sub Fuel_LevelW03( sheetName as String )
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
If sheetName = "Sheet1" Then 'Replace Sheet1 with the name of your worksheet
strbody = "Hi" & vbNewLine & vbNewLine & _
"Please order fuel as attached." & vbNewLine & _
"" & vbNewLine & _
"Kind Regards" & vbNewLine & _
"STRING BODY1"
With OutMail
.To = "email address"
.CC = "email address"
.BCC = ""
.Subject = "Fuel Order W03"
.Body = strbody
.Attachments.Add ("H:\Fuel Order Sheets\Glen Eden W03 Pump Station.xlsx")
.Send
End With
On Error GoTo 0
ElseIf sheetName = "Sheet2" Then 'Replace Sheet2 with the name of the next sheet and
'Put the same content as the first IF statement, but adapted to "Sheet2"
ElseIf sheetName = "Sheet3" Then 'Replace Sheet3 with the name of the next sheet and
'Put the same content as the first IF statement, but adapted to "Sheet3"
ElseIf sheetName = "Sheet4" Then 'Replace Sheet4 with the name of the next sheet and
'Put the same content as the first IF statement, but adapted to "Sheet4"
'ElseIf ............. (So on, so forth)
End If
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You can add as many ElseIf's as you want (one for each sheet)
Am pretty sure this is what you need, although am not sure.
If ActiveSheet.Name = "Sheet1" Then
'Do something specific to "Sheet1"
ElseIf ActiveSheet.Name = "Sheet2" Then
'Do something specific to "Sheet2"
'And so on so forth...
End If
You have a button to that macro in each sheet, and depending on the sheet calling the macro, you want a different e-mail to be sent, right? Then this will do it. You can add as many ElseIf's as you want.