access dynamic email address selection - excel

I am trying to send email to multiple person. But the email address that it should select is based on the user who has logged in. How do i do this? I have a table where I have 5 columns of email ID. Based on who is logged in, the code should go n select that person and send email to all the person in that row.
How do I achieve this? I know how to do in excel but access I am less aware. I am pasting both the codes I know and hv tried.
Sub Mail_ActiveSheet()
Dim OutApp As Object
Dim Outmail As Object
Dim sTo As String
Dim sCC As String
Dim lastrow, i As Integer
Dim sub1, sub2, sub3, body1 As Variant
Dim emailid, cc1, cc2, cc3, cc4, subj, attch, Sourcewb, Outnail, Soucrwb As Object
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
ThisWorkbook.Activate
'Code for Email sheet creation
Call EMAILSHEET_DATA
'Code for emailing schedular to the associates
Worksheets("Email_List").Select
lastrow = ThisWorkbook.Worksheets("Email_List").Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Set emailid = Worksheets("Email_List").Range("C" & i)
sTo = emailid
Set cc1 = Worksheets("Email_List").Range("D" & i)
'Set cc2 = Worksheets("Email_List").Range("E" & i)
'body1 = Worksheets("Email_List").Range("a" & i)
sCC = cc1 '& ";" & cc2
subj1 = Worksheets("Email_List").Range("F" & i).Value
subj2 = Worksheets("Email_List").Range("G" & i).Value
subj3 = Worksheets("Email_List").Range("H" & i).Value
subj = "Your Schedule for " & subj1 & subj2 & subj3
ActiveWorkbook.Activate
Set Sourcewb = ActiveWorkbook
Set OutApp = CreateObject("Outlook.Application")
Set Outmail = OutApp.CreateItem(0)
On Error Resume Next
With Outmail
.To = sTo
.CC = sCC
'.BCC=Sbcc
.Subject = subj
.Body = "Hello " & Worksheets("Email_List").Range("a" & i).Value & "," & vbCrLf & vbCrLf & subj & "." _
& vbCrLf & vbCrLf & Worksheets("Email_List").Range("i" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("j" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("k" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("l" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("m" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("n" & i).Value _
& vbCrLf & Worksheets("Email_List").Range("o" & i).Value _
& vbCrLf & vbCrLf & "Note: Please report any scheduling conflicts or errors to your Supervisor." _
& vbCrLf & vbCrLf & "Thank You," & vbCrLf & "gmail.com Management"
.Send
End With
On Error GoTo 0
Set Outmail = Nothing
Set OutApp = Nothing
'ActiveWorkbook.Close False
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Next i
ThisWorkbook.Activate
MsgBox ("Thank You! The Schedules have been sent to all the associates.")
End Sub
'DATA FOR EMAIL SHEET
Sub EMAILSHEET_DATA()
Worksheets("Email_list").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Worksheets("Schedules").Select
Range("B15").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Email_List").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Associate_Info!R1C1:R270C7,4,0)"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Associate_Info!R1C1:R270C7,3,0)"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Associate_Info!R1C1:R270C7,4,0)"
Range("E2").Select
'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-4],Associate_Info!C[-4]:C[-1],4,0)"
ActiveCell.FormulaR1C1 = "Information not available"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=Schedules!R12C5"
Range("G2").Select
ActiveCell.FormulaR1C1 = " to "
Range("H2").Select
ActiveCell.FormulaR1C1 = "=Schedules!R12C17"
Range("I2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C5,""MM/DD/YY"") & "" , "" & Schedules!R13C5 & "" , "" & Schedules!R[13]C[-4] & "" - "" & Schedules!R[13]C[-3]"
Range("J2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C7,""MM/DD/YY"") & "" , "" & Schedules!R13C7 & "" , "" & Schedules!R[13]C[-3] & "" - "" & Schedules!R[13]C[-2]"
Range("K2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C9,""MM/DD/YY"") & "" , "" & Schedules!R13C9 & "" , "" & Schedules!R[13]C[-2] & "" - "" & Schedules!R[13]C[-1]"
Range("L2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C11,""MM/DD/YY"") & "" , "" & Schedules!R13C11 & "" , "" & Schedules!R[13]C[-1] & "" - "" & Schedules!R[13]C[0]"
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C13,""MM/DD/YY"") & "" , "" & Schedules!R13C13 & "" , "" & Schedules!R[13]C & "" - "" & Schedules!R[13]C[1]"
Range("N2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C15,""MM/DD/YY"") & "" , "" & Schedules!R13C15 & "" , "" & Schedules!R[13]C[1] & "" - "" & Schedules!R[13]C[2]"
Range("O2").Select
ActiveCell.FormulaR1C1 = _
"=TEXT(Schedules!R12C17,""MM/DD/YY"") & "" , "" & Schedules!R13C17 & "" , "" & Schedules!R[13]C[2] & "" - "" & Schedules!R[13]C[3]"
Range("O2").Select
Selection.End(xlToLeft).Select
Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Range("A2").Select
lastrow = Cells(2, 1).End(xlDown).Row
'Range("B2:O" & lastrow).Select
Range("b3:o" & lastrow).Select
'Range("B3:B571").Select
'Range("B571").Activate
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.End(xlUp).Select
Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlUp).Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveSheet.Range("$A$1:$O$291").AutoFilter Field:=2, Criteria1:="#N/A"
Rows("6:6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
Range("A1").Select
ActiveWorkbook.Save
End Sub
Public Sub EmailList()
'late Binding
Dim olApp As Object
Dim olemail As Object
Dim strbody As String
'creating new outlook instance
Set olApp = CreateObject("Outlook.Application")
Set olemail = olApp.CreateItem(0)
strbody = "<html> <body> Hi " & Me.FullName & " <br/> <br/> Your leaves have been saved. <br/> Start Date: " & Me.Text8 & " <br/> End Date: " & Me.Text10 & " <br/> <br/> Regards <br/> Walmart.com Management "
Set olApp = CreateObject("Outlook.Application")
Set olemail = olApp.CreateItem(0)
With olemail
.BodyFormat = 2
.Display
.htmlBody = strbody
.to = "reetika.choudhary#gmail.com"
.Subject = "Leaves Applied"
.Send
DoCmd.SetWarnings WarningsOff
End With
End Sub
excel code is as below:
Any help will be highly appreciated! Thanks in advance.

You can use Environ("USERNAME") on a windows system to pull the username from the environment variables:
Sub SO()
Dim username As String
username = Environ("USERNAME")
MsgBox username & " is currently logged on."
End Sub

Related

VBA run multiple times before it's correct

I have a code that copy values from a selected sheet to a master sheet. When the first run is completed; I need to select the second sheet. For some strange reason I need to perform it 3 or more times before it is correct. I've checked it over and over but couldn't find it.
It are two codes but linked to each other.
Can somebody help me?
Sub Update_SISdata_STB()
'
' Update_SISdata Macro
'
Set Workbook = ThisWorkbook
Sheets("Meetstaten").Select
WorkbooknameSISdata = ActiveWorkbook.Name
MsgBox "Selecteer de steigerbouwdump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
Workbookname_ASESR = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If Workbookname_ASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
Else
Sheets("Meetstaten").Select
'Clear filter
On Error Resume Next
Sheets("Meetstaten").ShowAllData
Range("A6").Select
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
If LastRow > 5 Then
Range("A6:V" & LastRow).Select
Selection.ClearContents
End If
Range("A6").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'WorkbooknameYMOE = ActiveWorkbook.Name
Workbooks.Open Filename:=Workbookname_ASESR
Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
End With
'copy Meetstaat, Project, Debiteur
Range("A2:C" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("C6").Select
ActiveSheet.Paste
'Workbooks.Open Filename:=Workbookname_ASESR
'Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Copy Prio1, prio2, prio3, prio4, prio5
Range("D2:H" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("F6").Select
ActiveSheet.Paste
'copy datum SES montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("M2:M" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("K6").Select
ActiveSheet.Paste
'copy datum SES huur, SESnr montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("P2:P" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("M6").Select
ActiveSheet.Paste
'copy SESnr Huur
' Windows(Workbookname_ASESR).Activate
' ActiveWindow.WindowState = xlMaximized
' Range("R2:R" & LastRow).Select
' Selection.Copy
'
' Windows(WorkbooknameSISdata).Activate
' Range("N6").Select
' ActiveSheet.Paste
'copy inhuur, uithuur
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:X" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("O6").Select
ActiveSheet.Paste
'copy montage_demontage-bedrag, Huurbedrag
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AG2:AH" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("Q6").Select
ActiveSheet.Paste
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AK2:AN" & LastRow).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("S6").Select
ActiveSheet.Paste
Windows(Workbookname_ASESR).Close savechanges:=False
End If
Call Update_SISdata_ISO
Windows(WorkbooknameSISdata).Activate
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
Range("A5:AM5").Select
Selection.AutoFilter
Range("A5:AM5").Select
Selection.AutoFilter
ActiveSheet.ShowAllData
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
ActiveSheet.Range("$A$5:$AM" & LastRow).AutoFilter Field:=25, Criteria1:="<=0", _
Operator:=xlAnd
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
With ActiveSheet
Set rngFilt = Application.Intersect(.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible), .Range("A:V"))
End With
rngFilt.Delete
ActiveSheet.ShowAllData
Range("W6:AM6").Select
Selection.AutoFill Destination:=Range("W6:AM1200"), Type:=xlFillDefault
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
For Edit_row = 6 To LastRow
Range("A" & Edit_row) = Mid(Range("D" & Edit_row), 34, 10)
If Range("M" & Edit_row) <> "CONFIRMED" Then
Range("B" & Edit_row) = Range("M" & Edit_row)
End If
If Range("K" & Edit_row).Value = " - -" Then
Range("K" & Edit_row) = ""
End If
If Range("L" & Edit_row).Value = " - -" Then
'If IsEmpty(Range("L" & Edit_row).Value) = True Then
Range("L" & Edit_row) = ""
End If
Next Edit_row
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C6").End(xlUp).Row 'laatste regel bepalen
End With
ActiveWorkbook.ActiveSheet.Range("S2") = Application.WorksheetFunction.Subtotal(109, Range("S6:S" & LastRow))
ActiveWorkbook.ActiveSheet.Range("S3") = Application.WorksheetFunction.Subtotal(109, Range("S6:S" & LastRow))
ActiveWorkbook.ActiveSheet.Range("T2") = Application.WorksheetFunction.Subtotal(109, Range("T6:T" & LastRow))
ActiveWorkbook.ActiveSheet.Range("T3") = Application.WorksheetFunction.Subtotal(109, Range("T6:T" & LastRow))
ActiveWorkbook.ActiveSheet.Range("U2") = Application.WorksheetFunction.Subtotal(109, Range("U6:U" & LastRow))
ActiveWorkbook.ActiveSheet.Range("U3") = Application.WorksheetFunction.Subtotal(109, Range("U6:U" & LastRow))
ActiveWorkbook.ActiveSheet.Range("V2") = Application.WorksheetFunction.Subtotal(109, Range("V6:V" & LastRow))
ActiveWorkbook.ActiveSheet.Range("V3") = Application.WorksheetFunction.Subtotal(109, Range("V6:V" & LastRow))
ActiveWorkbook.ActiveSheet.Range("W2") = Application.WorksheetFunction.Subtotal(109, Range("W6:W" & LastRow))
ActiveWorkbook.ActiveSheet.Range("W3") = Application.WorksheetFunction.Subtotal(109, Range("W6:W" & LastRow))
ActiveWorkbook.ActiveSheet.Range("X2") = Application.WorksheetFunction.Subtotal(109, Range("X6:X" & LastRow))
ActiveWorkbook.ActiveSheet.Range("X3") = Application.WorksheetFunction.Subtotal(109, Range("X6:X" & LastRow))
ActiveWorkbook.ActiveSheet.Range("Y2") = Application.WorksheetFunction.Subtotal(109, Range("Y6:Y" & LastRow))
ActiveWorkbook.ActiveSheet.Range("Y3") = Application.WorksheetFunction.Subtotal(109, Range("Y6:Y" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AF2") = Application.WorksheetFunction.Subtotal(109, Range("AF6:AF" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AG2") = Application.WorksheetFunction.Subtotal(109, Range("AG6:AG" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AH2") = Application.WorksheetFunction.Subtotal(109, Range("AH6:AH" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AI2") = Application.WorksheetFunction.Subtotal(109, Range("AI6:AI" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AJ2") = Application.WorksheetFunction.Subtotal(109, Range("AJ6:AJ" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AK2") = Application.WorksheetFunction.Subtotal(109, Range("AK6:AK" & LastRow))
ActiveWorkbook.ActiveSheet.Range("AL2") = Application.WorksheetFunction.Subtotal(109, Range("AL6:AL" & LastRow))
' Verversen draaitabellen
Dim pivC As PivotCache
For Each pivC In ActiveWorkbook.PivotCaches
pivC.Refresh
Next
End Sub
Sub Update_SISdata_ISO()
'
' Update_SISdata Macro
'
Set Workbook = ThisWorkbook
Sheets("Meetstaten").Select
WorkbooknameSISdata = ActiveWorkbook.Name
MsgBox "Selecteer de Isolatiedump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
Workbookname_ASESR = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If Workbookname_ASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
Else
Sheets("Meetstaten").Select
'Clear filter
'Sheets("Meetstaten").ShowAllData
Range("A6").Select
Dim LastRow_STB As Long
With ActiveSheet
LastRow_STB = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
End With
' If LastRow_STB > 5 Then
' Range("A6:V" & LastRow).Select
' Selection.ClearContents
' End If
' Range("A6").Select
LastRow_STB = LastRow_STB + 1
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'WorkbooknameYMOE = ActiveWorkbook.Name
Workbooks.Open Filename:=Workbookname_ASESR
Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Dim LastRow_ISO As Long
With ActiveSheet
LastRow_ISO = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
End With
'copy Meetstaat, Project, Debiteur
Range("A2:C" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("C" & LastRow_STB).Select
ActiveSheet.Paste
'Workbooks.Open Filename:=Workbookname_ASESR
'Workbookname_ASESR = ActiveWorkbook.Name
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
'Copy Prio1, prio2, prio3, prio4, prio5
Range("F2:J" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("F" & LastRow_STB).Select
ActiveSheet.Paste
'copy datum SES montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AK2:AK" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("K" & LastRow_STB).Select
ActiveSheet.Paste
'copy datum SES huur blijft LEEG, SESnr montage
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("AM2:AM" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("M" & LastRow_STB).Select
ActiveSheet.Paste
'copy SESnr Huur blijft LEEG
' Windows(Workbookname_ASESR).Activate
' ActiveWindow.WindowState = xlMaximized
' Range("R2:R" & LastRow).Select
' Selection.Copy
'
' Windows(WorkbooknameSISdata).Activate
' Range("N" & LastRow_STB).Select
' ActiveSheet.Paste
'copy inhuur, uithuur
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("P2:P" & LastRow_ISO).Select
Selection.Copy
' Inhuur
Windows(WorkbooknameSISdata).Activate
Range("O" & LastRow_STB).Select
ActiveSheet.Paste
' Uithuur
Windows(WorkbooknameSISdata).Activate
Range("P" & LastRow_STB).Select
ActiveSheet.Paste
'copy montage_demontage_bedrag, Huurbedrag
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:W" & LastRow_ISO).Select
Selection.Copy
' Montage_demontage_bedrag
Windows(WorkbooknameSISdata).Activate
Range("Q" & LastRow_STB).Select
ActiveSheet.Paste
' Huurbedrag
' Windows(WorkbooknameSISdata).Activate
' Range("R" & LastRow_STB).Select
' ActiveSheet.Paste
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
Windows(Workbookname_ASESR).Activate
ActiveWindow.WindowState = xlMaximized
Range("W2:Z" & LastRow_ISO).Select
Selection.Copy
Windows(WorkbooknameSISdata).Activate
Range("S" & LastRow_STB).Select
ActiveSheet.Paste
Windows(Workbookname_ASESR).Close savechanges:=False
End If
End Sub
Try it without using select.
Option Explicit
Sub Update_SISdata_STB()
Dim wb As Workbook, ws As Worksheet
Dim LastRow As Long, Edit_row As Long
Dim rngFilt As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
With ws
' clear filter
.AutoFilterMode = False
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
If LastRow > 5 Then
.Range("A6:V" & LastRow).ClearContents
End If
Range("A6").Select
End With
Call Import_SISdata_STB
Call Update_SISdata_ISO
With ws
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
' apply filter
.Range("$A$5:$AM" & LastRow).AutoFilter Field:=25, Criteria1:="<=0", Operator:=xlAnd
Set rngFilt = Application.Intersect(.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible), .Range("A:V"))
rngFilt.Delete
.AutoFilterMode = False
.Range("W6:AM6").AutoFill Destination:=.Range("W6:AM1200"), Type:=xlFillDefault
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row 'laatste regel bepalen
For Edit_row = 6 To LastRow
.Range("A" & Edit_row) = Mid(.Range("D" & Edit_row), 34, 10)
If .Range("M" & Edit_row) <> "CONFIRMED" Then
.Range("B" & Edit_row) = .Range("M" & Edit_row)
End If
If .Range("K" & Edit_row).Value = " - -" Then
.Range("K" & Edit_row) = ""
End If
If .Range("L" & Edit_row).Value = " - -" Then
'If IsEmpty(Range("L" & Edit_row).Value) = True Then
.Range("L" & Edit_row) = ""
End If
Next Edit_row
.Range("S2:Y3,AF2:AL2").Formula = "=Subtotal(109,S$6:S$" & LastRow & ")"
End With
' Verversen draaitabellen
Dim pivC As PivotCache
For Each pivC In ActiveWorkbook.PivotCaches
pivC.Refresh
Next
MsgBox "Done"
End Sub
Sub Import_SISdata_STB()
Dim wb As Workbook, wbIn As Workbook
Dim ws As Worksheet, wsIn As Worksheet
Dim FileASESR As Variant, LastRow As Long
'Select the scaffolding dump to import
MsgBox "Selecteer de steigerbouwdump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
FileASESR = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls), *.xls", _
Title:="Please select a file")
If FileASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
End If
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
Set wbIn = Workbooks.Open(Filename:=FileASESR, ReadOnly:=True)
Set wsIn = wbIn.Sheets(1)
With wsIn
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'copy Meetstaat, Project, Debiteur
'copy Prio1, prio2, prio3, prio4, prio5
.Range("A2:H" & LastRow).Copy ws.Range("C6")
'copy datum SES montage
.Range("M2:M" & LastRow).Copy ws.Range("K6")
'copy datum SES huur, SESnr montage
.Range("P2:P" & LastRow).Copy ws.Range("M6")
'copy inhuur, uithuur
.Range("W2:X" & LastRow).Copy ws.Range("O6")
'copy montage_demontage-bedrag, Huurbedrag
.Range("AG2:AH" & LastRow).Copy ws.Range("Q6")
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
.Range("AK2:AN" & LastRow).Copy ws.Range("S6")
End With
MsgBox "Imported " & LastRow - 1 & " rows from " & wsIn.Name, vbInformation, wbIn.Name
wbIn.Close savechanges:=False
End Sub
Sub Update_SISdata_ISO()
' Update_SISdata Macro
Dim wb As Workbook, wbIn As Workbook
Dim ws As Worksheet, wsIn As Worksheet
Dim FileASESR As Variant
Dim LastRow_STB As Long, LastRow_ISO As Long
' Select the Insulation Dump to import
MsgBox "Selecteer de Isolatiedump om te importeren ", vbMsgBoxSetForeground, "BIS Industrial Services"
FileASESR = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls), *.xls", _
Title:="Please select a file")
If FileASESR = False Then
' They pressed Cancel
MsgBox "Er is geen bestand geselecteerd om te openen!", vbExclamation, "BIS Industrial Services"
Exit Sub 'GoTo exit_openfile
End If
Set wb = ThisWorkbook
Set ws = wb.Sheets("Meetstaten")
LastRow_STB = 1 + ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set wbIn = Workbooks.Open(Filename:=FileASESR, ReadOnly:=True)
Set wsIn = wbIn.Sheets(1)
With wsIn
LastRow_ISO = .Cells(.Rows.Count, "A").End(xlUp).Row 'laatste regel bepalen
'copy Meetstaat, Project, Debiteur
.Range("A2:C" & LastRow_ISO).Copy ws.Range("C" & LastRow_STB)
'Copy Prio1, prio2, prio3, prio4, prio5
.Range("F2:J" & LastRow_ISO).Copy ws.Range("F" & LastRow_STB)
'copy datum SES montage
.Range("AK2:AK" & LastRow_ISO).Copy ws.Range("K" & LastRow_STB)
'copy datum SES huur blijft LEEG, SESnr montage
.Range("AM2:AM" & LastRow_ISO).Copy ws.Range("M" & LastRow_STB)
'copy inhuur
.Range("P2:P" & LastRow_ISO).Copy ws.Range("O" & LastRow_STB)
' Uithuur
.Range("P2:P" & LastRow_ISO).Copy ws.Range("P" & LastRow_STB)
'copy montage_demontage_bedrag, Huurbedrag
.Range("W2:W" & LastRow_ISO).Copy ws.Range("Q" & LastRow_STB)
'copy Totaalbedrag, gefact_mon_demon, gefact_huur, gefact_totaal
.Range("W2:Z" & LastRow_ISO).Copy ws.Range("S" & LastRow_STB)
End With
MsgBox "Imported " & LastRow_ISO - 1 & " rows from " & wsIn.Name, vbInformation, wbIn.Name
wbIn.Close savechanges:=False
End Sub

VBA add image in cell gives different results

I have a question about a problem that I can't seem to solve.
I have some VBA-code that includes a picture in a cell:
fNameAndPath = UserForm1.ComboBox2.Value
Set img = Application.ActiveSheet.Shapes.AddPicture(fNameAndPath, False, True, 1, 1, 1, 1)
With img
.Left = ActiveSheet.Range("G" & Lastrow).Left
.Top = ActiveSheet.Range("G" & Lastrow).Top
.Width = ActiveSheet.Range("G" & Lastrow).Width
.Height = ActiveSheet.Range("G" & Lastrow).Height
.Placement = 1
End With
This code runs perfectly for myself. The pictures are beautifully inserted.
However, 20% of my colleagues who use the macro find their picture in column H instead of column G.
I cannot come up with a reason why this happens.
Someone who encountered a related issue?
Thank you
Sorry for the delay, hereby the whole code that is in this macro.
Sub CommandButton3_Click()
‘ check whether conditions are OK
If UserForm1.TextBox1.Value = "" Or UserForm1.TextBox2.Value = "" Or UserForm1.TextBox3.Value = "" Or UserForm1.ComboBox1.Value = "" Then
MsgBox ("")
Exit Sub
End If
‘Check whether a picture was attached
If UserForm1.ComboBox2.Value = "" Then
MsgBox ("")
Exit Sub
End If
Workbooks("").Sheets("").Unprotect ""
‘Find username of the user
Dim username As String
username = Environ("username")
‘define lastrow and write some data in the cells
Lastrow = Worksheets("Objets Inutiles").Range("A650000").End(xlUp).Row + 1
Worksheets("Objets Inutiles").Range("A" & Lastrow).Value = "=Now()"
Worksheets("Objets Inutiles").Range("A" & Lastrow).Select
Selection.Copy
Worksheets("Objets Inutiles").Range("A" & Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Objets Inutiles").Range("B" & Lastrow).Value = ""
Worksheets("Objets Inutiles").Range("C" & Lastrow).Value = username
Worksheets("Objets Inutiles").Range("C" & Lastrow).Select
Selection.Copy
Worksheets("Objets Inutiles").Range("C" & Lastrow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Objets Inutiles").Range("D" & Lastrow).Value = UserForm1.ComboBox1.Value
Worksheets("Objets Inutiles").Range("E" & Lastrow).Value = UserForm1.TextBox2.Value
Worksheets("Objets Inutiles").Range("F" & Lastrow).Value = UserForm1.TextBox3.Value
Worksheets("Objets Inutiles").Range("H" & Lastrow).Value = UserForm1.TextBox1.Value
Worksheets("Objets Inutiles").Range("I" & Lastrow).Value = ""
Worksheets("Objets Inutiles").Range("J" & Lastrow).Value = ""
fNameAndPath = UserForm1.ComboBox2.Value
Set img = Application.ActiveSheet.Shapes.AddPicture(fNameAndPath, False, True, 1, 1, 1, 1)
With img
'Resize Picture to fit in the range....
.Left = ActiveSheet.Range("G" & Lastrow).Left
.Top = ActiveSheet.Range("G" & Lastrow).Top
.Width = ActiveSheet.Range("G" & Lastrow).Width
.Height = ActiveSheet.Range("G" & Lastrow).Height
.Placement = 1
End With
‘send some spam around
Dim EmailApp As Outlook.Application
Dim Source As String
Set EmailApp = New Outlook.Application
Dim EmailItem As Outlook.MailItem
Set EmailItem = EmailApp.CreateItem(olMailItem)
EmailItem.To = ""
EmailItem.CC = ""
EmailItem.BCC = ""
EmailItem.Subject = ""
EmailItem.HTMLBody = ""
EmailItem.Send
Unload UserForm1
Workbooks("").Sheets("").Protect ""
Workbooks("").Save
MsgBox ("")
Exit Sub
Unload Me
Workbooks("").Sheets("").Protect "Sapore"
Workbooks("").Save
MsgBox ("")
End Sub

Reducing size of a macro

Our company has 36 departments and we use a master budgeting worksheet to develop the budget. The department numbers are not sequential and their budgets are all different. I put together the following macro to send the worksheets to the individual departments. The master is full of VLOOKUPs and other formulae, but the individual departments receive only the final results and a couple of columns for their changes. They can make changes to any number that is not highlighted in yellow. The macro works perfectly for only one department, but when I tried to copy it 35 times below itself so that I could send a worksheet to all departments, I received an error message that said my procedure was too large. I divided it in half and I still received the message!
Sub Macro1()
'
' Macro1 Macro
'' Prepares O&M budget Worksheet for uploading
' Dim sourceSheet as Worksheet
Workbooks.Open Filename:="F:\Rick\2020 Budget\2020 O&M Budget.xlsx"
Set sourcesheet = Worksheets("Dept Detail-O&M Book")
sourcesheet.Activate
' Dim N As Long
' Dim T As Long
' Dim LastRow As Long
lastrow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Dim i As Long, Total As Long
Dim cell As Range
Application.EnableEvents = False
'
Application.Goto Reference:="Dept_01"
Selection.Copy
Workbooks.Open Filename:="Q:\O&M\Departmental Budgets\Dept 1 MOEC.xlsx"
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Activate
Application.CutCopyMode = False
Selection.Copy
Windows("Dept 1 MOEC.xlsx").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("R1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Application.CutCopyMode = False
n = Cells(Rows.Count, "R").End(xlUp).Row
Cells(n, "R").Formula = "=SUM(R1:R" & n - 1 & ")"
activecell.Select
Selection.Copy
activecell.Offset(0, 2).Select
ActiveSheet.Paste
Selection.Copy
activecell.Offset(0, 2).Select
ActiveSheet.Paste
Range("X9").Select
activecell.FormulaR1C1 = "=iferror(+RC[-2]/RC[-10],0)"
Range("X9").Select
T = Cells(Rows.Count, "X").End(xlUp).Row
Selection.AutoFill Destination:=Range("x9:x" & T)
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
For i = lastrow To 1 Step -1
If Range("B" & i).Value = "1010" Or _
Range("B" & i).Value = "1020" Or _
Range("B" & i).Value = "2172" Or _
Range("B" & i).Value = "2190" Or _
Range("B" & i).Value = "2200" Or _
Range("B" & i).Value = "2290" Or _
Range("B" & i).Value = "4020" Or _
Range("B" & i).Value = "4050" Or _
Range("B" & i).Value = "4060" Or _
Range("B" & i).Value = "4070" Or _
Range("B" & i).Value = "4090" Or _
Range("B" & i).Value = "4100" Or _
Range("B" & i).Value = "4110" Or _
Range("B" & i).Value = "4509" Or _
Range("B" & i).Value = "4510" Or _
Range("B" & i).Value = "4600" Or _
Range("B" & i).Value = "4610" Or _
Range("B" & i).Value = "4700" Or _
Range("B" & i).Value = "5710" Or _
Range("B" & i).Value = "5721" Or _
Range("B" & i).Value = "5723" Or _
Range("B" & i).Value = "5725" Or _
Range("B" & i).Value = "5729" Or _
Range("B" & i).Value = "5730" Or _
Range("B" & i).Value = "5731" Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
Application.EnableEvents = True
End With
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
For i = lastrow To 1 Step -1
If Range("B" & i).Value = "5721" Or _
Range("B" & i).Value = "9000" Or _
Range("B" & i).Value = "9005" Or _
Range("B" & i).Value = "9010" Or _
Range("B" & i).Value = "9030" Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
Application.EnableEvents = True
End With
Range("A1").Select
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
Could someone offer suggestions on how to reduce the size of the macro and/or make it more efficient? Thanks!
I took a shot at cleaning this up (at least to make it run, for now) - I don't know enough about what you're doing to clean up that mid section, though. The problem undoubtedly was that long If statement.
Instead of all the Ors, put all your values in an array then test against that array with IsError:
Option Explicit
Sub Macro1()
Dim valuearr As Variant
Dim cell As Range
Dim sourcesheet As Worksheet
Dim lastrow As Long, i As Long, n As Long
Workbooks.Open Filename:="F:\Rick\2020 Budget\2020 O&M Budget.xlsx"
Set sourcesheet = Worksheets("Dept Detail-O&M Book")
sourcesheet.Activate
lastrow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
Application.EnableEvents = False
'This section needs to be cleaned up...
Application.Goto Reference:="Dept_01"
Selection.Copy
Workbooks.Open Filename:="Q:\O&M\Departmental Budgets\Dept 1 MOEC.xlsx"
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Activate
Application.CutCopyMode = False
Selection.Copy
Windows("Dept 1 MOEC.xlsx").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("R1").Select
Selection.End(xlDown).Select
Application.CutCopyMode = False
n = Cells(Rows.Count, "R").End(xlUp).Row
Cells(n, "R").Formula = "=SUM(R1:R" & n - 1 & ")"
ActiveCell.Copy
ActiveCell.Offset(0, 2).Paste
Selection.Offset(0, 2).Select
ActiveSheet.Paste
Range("X9").FormulaR1C1 = "=iferror(+RC[-2]/RC[-10],0)"
Range("X9").AutoFill Destination:=Range("x9:x" & Cells(Rows.Count, "X").End(xlUp).Row)
With ActiveSheet
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
valuearr = Array(1010, 1020, 2172, 2190, 2200, 2290, 4020, 4050, 4060, 4070, 4090, 4100, 4110, 4509, 4510, 4600, 4610, 4700, 5710, 5721, 5723, 5725, 5729, 5730, 5731, 9000, 9005, 9010, 9030)
For i = lastrow To 1 Step -1
If IsError(Application.Match(Range("B" & i).Value, valuearr, 0)) Then
.Range("R" & i).Interior.Color = RGB(255, 255, 0)
.Range("T" & i).Interior.Color = RGB(255, 255, 0)
End If
Next i
End With
Application.EnableEvents = True
ActiveWorkbook.Save
ActiveWindow.Close
End Sub

Repeat Command in in excel macro

I want to repeat this command in subsequent rows every 15th time.So the next one will be J348:M348
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("J318").Select
ActiveCell.FormulaR1C1 = "=R303C10-((R3C2-R4C2)/(R5C2/R6C2))"
Range("K318").Select
ActiveCell.FormulaR1C1 = "=R303C11-((R3C3-R4C3)/(R5C2/R6C2))"
Range("L318").Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("M318").Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("J318:M318").Select
Selection.AutoFill Destination:=Range("J318:M332"), Type:=xlFillDefault
Range("J318:M332").Select
ActiveWindow.SmallScroll Down:=0
Range("J332").Select
ActiveWindow.SmallScroll Down:=15
Range("J333:M333").Select
Selection.Copy
Range("J334").Select
ActiveSheet.Paste
Range("J335").Select
Application.CutCopyMode = False
Selection.Copy
Range("J334:M334").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
For a straight migration of your code to a looped code:
I randomly started at row 5 and used a do while loop. You could just as easily use a for loop. Hopefully this gives you the idea of the looping so you don't have to do it manually.
Public Sub Every15thRow()
Dim i As Integer
Dim iFirst, iSecond, iThird, iFourth, iFifth As Integer
'in Ex: 318, 303, 332, 333, 334
Dim MyStopCriteria As Boolean
MyStopCriteria = False
'... whatever your code does before
'...
i = 5 'start with the 5th row
Do Until MyStopCriteria = True
iFirst = i + 15
iSecond = i
iThird = i + 29
iFourth = i + 30
iFifth = i + 31
Application.CutCopyMode = False
Range("J" & iFirst).Select
ActiveCell.FormulaR1C1 = "=R" & iSecond & "C10-((R3C2-R4C2)/(R5C2/R6C2))"
Range("K" & iFirst).Select
ActiveCell.FormulaR1C1 = "=R" & iSecond & "C11-((R3C3-R4C3)/(R5C2/R6C2))"
Range("L" & iFirst).Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("M" & iFirst).Select
ActiveCell.FormulaR1C1 = "=RC[-5]"
Range("J" & iFirst & ":M" & iFirst).Select
Selection.AutoFill Destination:=Range("J" & iFirst & ":M" & iThird), Type:=xlFillDefault
Range("J" & iFirst & ":M" & iFourth).Select
ActiveWindow.SmallScroll Down:=0
Range("J" & iThird).Select
ActiveWindow.SmallScroll Down:=15
Range("J" & iThird & ":M" & iThird).Select
Selection.Copy
Range("J" & iFourth).Select
ActiveSheet.Paste
Range("J" & iFourth).Select
Application.CutCopyMode = False
Selection.Copy
Range("J" & iFifth & ":M" & iFifth).Select
'...
'... whatever else your repeating code needs to do
'...
i = i + 15 'Add 15 rows
If i > 40 Then MyStopCriteria = True
Loop
'... whatever else your code does after repeating
End Sub

Error 13: Type Mismatch Row Limit Restriction?

When the excel sheet raw data has under 10,000 rows it runs, when it has 10,000 rows and over I get the error. Any idea? The error is pointed to the mu = Cells(joker, 12)
Columns("A:I").Select
Selection.ClearContents
Windows("New Registrations.xls").Activate
ActiveWindow.WindowState = xlNormal
Columns("A:I").Select
Selection.Copy
Windows("Polk Trend Report CYTD.xlsm").Activate
Range("A1").Select
ActiveSheet.Paste
Selection.Interior.ColorIndex = xlNone
Selection.Font.ColorIndex = 0
Sheets("Data").Select
Dim nz As Long
Dim joker As Long
Dim lambda As Long
nz = Cells(4, 12).Value
Dim mu As Long
For joker = 5 To nz + 4
lambda = Cells(joker, 11)
mu = Cells(joker, 12)
If lambda <> 0 And mu - lambda > 1 Then
Range("A" & lambda).Select
Selection.Copy
Range("A" & lambda + 1 & ":A" & mu - 1).Select
ActiveSheet.Paste
Else:
End If
Next joker
Range("N5:O" & nz + 4).Select
Selection.ClearContents
Dim iota As Long
Dim kappa As Long
iota = 7
Do While Cells(iota, 2).Value <> ""
If Cells(iota, 2) = "UNKNOWN" Then
kappa = Application.WorksheetFunction.Match(Cells(iota, 1).Value, Range("J1:J" & nz + 4), 0)
Cells(kappa, 14).Value = Cells(iota, 7).Value
Cells(kappa, 15).Value = Cells(iota, 5).Value
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
ElseIf Cells(iota, 2) = "Zone Total" Then
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
ElseIf Application.WorksheetFunction.And(Cells(iota, 5) = 0, Cells(iota, 7) = 0) Then
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
iota = iota - 1
Else:
End If
iota = iota + 1
Loop
Range("A" & iota & ":I" & iota).Select
Selection.Delete Shift:=xlUp
Range("C5:I5").Select
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Set pvtTable = Worksheets("Total Dealer (Trend)").Range("O5").PivotTable
pvtTable.RefreshTable
Sheets("Total Dealer (Trend)").Select
Cells.Select
Selection.Columns.AutoFit
Sheets("Data").Select
Range("S40:T" & nz + 39).Select
Selection.Copy
Range("A2").Select
Sheets("Total Dealer (Trend)").Select
Range("B40").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Data").Select
Range("U40:U" & nz + 39).Select
Selection.Copy
Range("A2").Select
Sheets("Total Dealer (Trend)").Select
Range("E40").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B40:E" & nz + 39).Select
Selection.Sort Key1:=Range("E40"), Order1:=xlDescending, Header:=xlNo _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
ActiveWindow.WindowState = xlMaximized
End Sub
I think you might have a format change taking place on your Worksheet after row 10,000. Say it was a date, now it's General or some other type conflict, and you are getting a data mismatch as a result of the Value of mu being set by
"L10000"
Check the format of the cells below 10,000. Especially Column "L"
this is an example error handler, hopefully you can just copy and paste this into your code as described and it should output the value of the failing cell when the error occurs, and then you can hopefully correct it. The following goes right at the top of your code
On Error GoTo MyProcedure_Error
Then the below goes above the end sub
MyProcedure_Exit:
On Error GoTo 0
Exit Sub
MyProcedure_Error:
Select Case Err.Number
'the "Case 9" statement below is left as an example to show how you could code a
'specific error message if a specifc module needed it
'Case 9
'MsgBox "The input file does not appear to be in the correct format, for importing into the " & _
'" Locations tab" & vbCrLf & "The expected format is " & Str(Import_Cols) & " columns, Pipe Delimited" & _
'vbCrLf, vbCritical, "Error in in procedure TrimColumn of Module DeveloperToolKit"
Case Else
MsgBox "An unexpected error has occured, the call value that has failed is." & _
vbCrLf & Cells(joker, 12) & _
vbCrLf & "Error Code = " & Str$(Err.Number) & _
vbCrLf & "Error Text = " & Err.Description, vbCritical, "Critical Error"
End Select
Resume MyProcedure_Exit

Resources