Whenever I run this code, it generates a sequential number.
I want to display the new sequential number in a MsgBox, but it prints the older sequential number.
Private Sub ToggleButton1_Click()
Dim reponse As VbMsgBoxResult
Dim REVISIONRNCAUTO As Workbook
Dim Sheet2 As Worksheet
Dim cell_value As String
Set REVISIONRNCAUTO = ActiveWorkbook
Set Sheet2 = REVISIONCRNAUTO.Worksheets(2)
cell_value = Sheet2.Cells(4, "A").Value & Sheet2.Cells(4, "B").Value
If CheckBox1.Value = True And CheckBox4.Value = True And CheckBox7.Value = True And CheckBox2.Value = False And CheckBox3.Value = False _
And CheckBox6.Value = False And CheckBox5.Value = False And CheckBox8.Value = False And CheckBox9.Value = False And CheckBox10.Value = False And CheckBox11.Value = False And CheckBox12.Value = False _
And CheckBox13.Value = False And CheckBox14.Value = False And CheckBox15.Value = False Then
Sheet2.Activate
reponse = MsgBox("Êtes-vous sûr de vouloir générer ce RNC?", vbYesNo + vbQuestion, "Enregistrement RNC")
If reponse = vbYes Then
Sheets("Sheet2").Range("B4").Select
ActiveCell.EntireRow.Insert shift:=xlDown
Sheets("Sheet2").Range("B4:E4").Select
Selection.Borders.Weight = xlThin
Sheets("Sheet2").Range("B4").Select
ActiveCell.Value = "=b5+1"
Sheets("Sheet2").Range("A4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "E"
Else
Exit Sub
End If
End If
MsgBox ("Le nouveau RNC enregistré est le : " & cell_value)
You aren't changing the value of cell_value after you set it.
They are not linked forever like an Excel formula. You have to set it again once you change the cells that it is based on.
Put the cell_value = line right before the Else in addition to where it currently is.
Related
I've been trying to program a special user login and Audit Trail, the login makes sheets display in custom order, then I still get
Error 1004: Method of Visible Object Failed.
Help me please :(
Macro 1 (I have 5 sheets in this book, each one has this macro in them):
Private Sub Worksheet_Deactivate()
Me.Name = "Información"
ThisWorkbook.Protect , True
Application.OnTime Now, "UnprotectBook"
End Sub
And Have this one in This Workbook:
Private Sub Workbook_Open()
Application.Visible = False
Sheet1.Visible = True
Sheet2.Visible = False
Sheet3.Visible = False
Sheet4.Visible = False
Sheet5.Visible = False
UF_log.Show
End Sub
Private Sub Workbook_Close()
Sheet1.Visible = False
Sheet2.Visible = False
Sheet3.Visible = False
Sheet4.Visible = False
Sheet5.Visible = False
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
ActiveSheet.Delete
MsgBox "No se tiene permitido insertar nuevas hojas de cálculo", vbExclamation, "Alerta"
Application.DisplayAlerts = True
End Sub
UF log code:
Private Sub EnterButton_Click()
Dim Fila, final As Integer
Dim password As Variant
Dim DatoEncontrado
UsuarioExistente = Application.WorksheetFunction.CountIf(Sheets("Users").Range("A:A"), _
Me.TxtUser.Value)
Set Rango = Sheets("Users").Range("A:A")
If Trim(TxtUser.Text) = "" Then
MsgBox "Ingreso Usuario", vbExclamation, "Alerta"
TxtUser.SetFocus
Exit Sub
End If
If Trim(TxtPassword.Text) = "" Then
MsgBox "Ingreso Contraseña", vbExclamation, "Alerta"
TxtPassword.SetFocus
Exit Sub
End If
If UsuarioExistente = 0 Then
MsgBox "El usuario '" & Me.TxtUser & "' no existe", vbExclamation, "Alerta"
Exit Sub
End If
If UsuarioExistente = 1 Then
DatoEncontrado = Rango.Find(What:=Me.TxtUser.Value, MatchCase:=True).Address
Contra = CStr(Sheets("Users").Range(DatoEncontrado).Offset(0, 1).Value)
Estatus = CStr(Sheets("Users").Range(DatoEncontrado).Offset(0, 2).Value)
If Sheets("Users").Range(DatoEncontrado).Value = Me.TxtUser.Value And Contra = Me.TxtPassword.Value Then
MsgBox "Acceso correcto", vbExclamation, "Ok"
If Estatus = "Administrador" Then
Application.Visible = True
Sheet1.Visible = True
Sheet2.Visible = True
Sheet3.Visible = True
Sheet4.Visible = True
Sheet5.Visible = True
Application.CommandBars("Ply").Enabled = True
Call Desproteger
For Fila = 1 To 1000
If Sheet4.Cells(Fila, 1) = "" Then
final = Fila
Exit For
End If
Next
Sheet4.Cells(final, 1) = Application.UserName & " accessed via log in " & " at " & Time & " " & Date
Call Proteger
End
Else
If Estatus = "Fabricación" Then
Application.Visible = True
Sheet1.Visible = True
Sheet2.Visible = xlSheetVeryHidden
Sheet3.Visible = True
Sheet4.Visible = True
Sheet5.Visible = xlSheetVeryHidden
Application.CommandBars("Ply").Enabled = True
Call Desproteger
For Fila = 1 To 1000
If Sheet4.Cells(Fila, 1) = "" Then
final = Fila
Exit For
End If
Next
Sheet4.Cells(final, 1) = Application.UserName & " accessed via log in " & " at " & Time & " " & Date
Call Proteger
End
Else
Application.Visible = True
Sheet1.Visible = True
Sheet2.Visible = True
Sheet3.Visible = xlSheetVeryHidden
Sheet4.Visible = True
Sheet5.Visible = xlSheetVeryHidden
Application.CommandBars("Ply").Enabled = False
Call Desproteger
For Fila = 1 To 1000
If Sheet4.Cells(Fila, 1) = "" Then
final = Fila
Exit For
End If
Next
Sheet4.Cells(final, 1) = Application.UserName & " accessed via log in " & " at " & Time & " " & Date
Call Proteger
End
End If
End If
Else
MsgBox "Datos incorrectos, vuelva a intentar", vbExclamation, "Error"
End If
End If
End Sub
Private Sub ExitButton_Click()
End
End Sub
Code that searches for duplicates works in test mode, but doesn't when I run it normally.
I have a subroutine called FindDuplicatesInColumn, placed in the section that Validates data entered into a userform.
' Check if all data has been entered on the userform
Function Data_Validation() As Boolean 'Declare Function with Bool as data type
'Default True. False if any conditions met. When a function is called, a new variable,
'with the function name and datatype given is created. You'll set the value in the
'function. When the function ends either in Exit Function or
'End Function, whatever is contained in this variable is returned as the Functions result
Data_Validation = True
' Check if all data has been entered on the userform
If ARLArea = False And KNBArea = False And LSQArea = False And RSQArea = False And RevenueControlInspectors = False And SpecialRequirementTeam = False Then
MsgBox "Select Area!", vbInformation, ("Area")
ARLArea.SetFocus
Data_Validation = False
Exit Function
End If
If EmployeeNo1 = "" Then
MsgBox "Enter Employee Number!", vbInformation, ("Employee Number")
EmployeeNo1.SetFocus
Data_Validation = False
Exit Function
End If
If FirstName1 = "" Then
MsgBox "Enter First Name!", vbInformation, ("First Name")
FirstName1.SetFocus
Data_Validation = False
Exit Function
End If
If LastName1 = "" Then
MsgBox "Enter Last Name!", vbInformation, ("Last Name")
LastName1.SetFocus
Data_Validation = False
Exit Function
End If
If CSA2 = False And CSA1 = False And CSS2 = False And CSS1 = False And CSM2 = False And CSM1 = False And AM = False And RCI = False And SRT = False Then
MsgBox "Select Grade!", vbInformation, ("Grade")
CSA2.SetFocus
Data_Validation = False
Exit Function
End If
BlnVal = 1
FindDuplicatesInColumn
End Function
The Subroutine I created
Sub FindDuplicatesInColumn()
Dim sAccNum As String
Dim rAccLst As Range
Dim res
Dim IdVal As Integer
Set rAccLst = Sheets("Data Input").Range("B5:B" & Range("B" & Rows.Count).End(xlUp).Row)
sAccNum = EmployeeNo1
If Not rAccLst.Find(What:=sAccNum, LookIn:=xlValues, Lookat:=xlPart) Is Nothing Then
MsgBox "Sorry, This person already exists in the Database!"
'Empty Area
ARLArea = False
LSQArea = False
KNBArea = False
RSQArea = False
RevenueControlInspectors = False
SpecialRequirementTeam = False
'Empty EmployeeNo1
EmployeeNo1.Value = ""
'Empty FirstName1
FirstName1.Value = ""
'Empty LastName1
LastName1.Value = ""
'Empty Grade
CSA2 = False
CSA1 = False
CSS2 = False
CSS1 = False
CSM2 = False
CSM1 = False
AM = False
RCI = False
SRT = False
sAccNum = ""
If Data_Validation() = False Then
Exit Sub
End If
Else
'Sheets("Data Input").Range("B" & Range("B" & Rows.Count).End(xlUp).Row).Offset(1, 0).Value = "ACC" & sAccNum
End If
End Sub
I am trying to check when the user enters the EmployeeNo1 in the userform. It is checked against the data in Column B of Data_Input to see if it is duplicated. If it is, then to stop the data from the form being added to the database.
Set rAccLst = Sheets("Data Input").Range("B5:B" & Range("B" & Rows.Count).End(xlUp).Row)
should be
With Sheets("Data Input")
Set rAccLst = .Range("B5:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
End with
...otherwise that second Range() defaults to the active sheet and not necessarily to the sheet you want.
I am trying to extract a SAP transaction into a .txt document through a SAP script that I recorded which finally then gets extracted into my excel sheet.
When I run the script in SAP then it works by copying the values into my .txt document. So it seems like my problem is my macro in excel not activating my SAP script.
When I run my VBA macro to do the same, it says
"Run-time error '7': out of memory
and does not copy anything into my .txt document.
I hope someone can tell me why?
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System
Const fpath = "S:\Supply\WChristian\SAP GUI"
Const ffilename = "script2.txt"
Sub OpenCSVFile()
'
' Load the CSV extract
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;S:\Supply\WChristian\SAP GUI\script2.txt", Destination:=Range( _
"$A$4:$I$24"))
.Name = "mb52"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub DeleteAll()
'
' DeleteAll Macro
On Error Resume Next
Cells.Select
Selection.QueryTable.Delete
Selection.ClearContents
Range("A1").Select
End Sub
Function Attach_Session() As Boolean
Dim il, it
Dim W_conn, W_Sess
If W_System = "" Then
Attach_Session = False
Exit Function
End If
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Attach_Session = True
Exit Function
End If
End If
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
For il = 0 To objGui.Children.Count - 1
Set W_conn = objGui.Children(il + 0)
For it = 0 To W_conn.Children.Count - 1
Set W_Sess = W_conn.Children(it + 0)
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
Set objConn = objGui.Children(il + 0)
Set objSess = objConn.Children(it + 0)
Exit For
End If
Next
Next
If objSess Is Nothing Then
MsgBox "No active session to system " + W_System + ", or scripting is not enabled.", vbCritical + vbOKOnly
Attach_Session = False
Exit Function
End If
If IsObject(WScript) Then
WScript.ConnectObject objSess, "on"
WScript.ConnectObject objGui, "on"
End If
Set objSBar = objSess.findById("wnd[0]/sbar")
objSess.findById("wnd[0]").maximize
Attach_Session = True
End Function
Public Sub RunGUIScript()
Dim W_Ret As Boolean
' Connect to SAP
W_Ret = Attach_Session
If Not W_Ret Then
Exit Sub
End If
On Error GoTo myerr
ojbSess.findById("wnd[0]").ResizeWorkingPane 174, 29, False
ojbSess.findById("wnd[0]/tbar[0]/okcd").Text = "mb52"
ojbSess.findById("wnd[0]").sendVKey 0
ojbSess.findById("wnd[0]/usr/ctxtWERKS-LOW").Text = "DO"
ojbSess.findById("wnd[0]/usr/ctxtLGORT-LOW").Text = "01"
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").Text = "2"
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").SetFocus
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").caretPosition = 3
ojbSess.findById("wnd[0]").sendVKey 8
ojbSess.findById("wnd[0]/tbar[1]/btn[45]").press
ojbSess.findById("wnd[1]/tbar[0]/btn[0]").press
ojbSess.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:\Supply\WChristian\SAP GUI\"
ojbSess.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "script2.txt"
ojbSess.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
ojbSess.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub
myerr:
MsgBox "Error occured while retrieving data", vbCritical + vbOKOnly
End Sub
Sub StartExtract()
' Set the sid and client to connect to
W_System = "DCG210"
' Run the GUI script
RunGUIScript
' End the GUI session
' Switch to the worksheet where the data is loaded to
Sheets("Extract").Select
' Delete the entire sheet before loading the file
DeleteAll
' Load the CSV file
OpenCSVFile
' Update the time and date on the control worksheet
Sheets("Control").Select
Cells(2, 2).Value = Now()
End Sub
As I promised #Christian I'm pasting here my approach to connect with SAP. It was always working. I based on that all of my macros for tons of reports from SAP :)
Sub SapConn()
Dim Appl As Object
Dim Connection As Object
Dim session As Object
Dim WshShell As Object
Dim SapGui As Object
'Of course change for your file directory
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
Set WshShell = CreateObject("WScript.Shell")
Do Until WshShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WshShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("01. PRD - ERP Production", True) 'here paste your name of module you are trying to connect/enter
Set session = Connection.Children(0)
'if You need to pass username and password - not connect automatically
'session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "900"
'session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"
'session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
'session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
If session.Children.Count > 1 Then
answer = MsgBox("You've got opened SAP already, please leave and try again", vbOKOnly, "Opened SAP")
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
Exit Sub
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]").sendVKey 0 'ENTER
'and there goes your code in SAP
Hope it will help You :)
I have a useform that will open another work book when opened and close the workbook when closed. This works well the first time, but when I go to open the userform again it shoots out the runtime error. I can use some help on this issue I have not been able to determine what the issue is. Below is the error line in italics:
Sub Auto_Open()
Application.OnKey "{F3}", "ShowForm"
End Sub
Sub ShowForm()
*Auto_Fill_Form.Show*
End Sub
The button I use to exit the userform is shown below:
Private Sub CommandButton4_Click()
Workbooks("WorkOrderDatabase.xlsm").Close SaveChanges:=True
Unload Me
End Sub
Userform Code:
Private Sub CommandButton2_Click()
Call UserForm_Initialize
End Sub
Private Sub CommandButton4_Click()
'ComboBox1.Value = ""
Workbooks("WorkOrderDatabase.xlsm").Close SaveChanges:=True
Unload Me
End Sub
Private Sub CommandButton5_Click()
Me.Width = 480
Me.Height = 520
Auto_Fill_Form.Label26.Visible = True
Auto_Fill_Form.TextBox15.Visible = True
Auto_Fill_Form.CommandButton7.Visible = True
Auto_Fill_Form.Label22.Visible = False
Auto_Fill_Form.TextBox16.Visible = False
Auto_Fill_Form.Label23.Visible = False
Auto_Fill_Form.TextBox17.Visible = False
Auto_Fill_Form.Label24.Visible = False
Auto_Fill_Form.TextBox18.Visible = False
Auto_Fill_Form.Label25.Visible = False
Auto_Fill_Form.TextBox19.Visible = False
Auto_Fill_Form.CommandButton8.Visible = False
TextBox15 = Translate(TextBox10.Value, "en", "es", True)
SortAsc2
End Sub
Private Sub CommandButton6_Click()
Me.Width = 480
Me.Height = 520
Auto_Fill_Form.Label22.Visible = True
Auto_Fill_Form.TextBox16.Visible = True
Auto_Fill_Form.Label23.Visible = True
Auto_Fill_Form.TextBox17.Visible = True
Auto_Fill_Form.Label24.Visible = True
Auto_Fill_Form.TextBox18.Visible = True
Auto_Fill_Form.Label25.Visible = True
Auto_Fill_Form.TextBox19.Visible = True
Auto_Fill_Form.CommandButton8.Visible = True
Auto_Fill_Form.Label26.Visible = False
Auto_Fill_Form.TextBox15.Visible = False
Auto_Fill_Form.CommandButton7.Visible = False
TextBox16 = Translate(TextBox11.Value, "en", "es", True)
TextBox17 = Translate(TextBox12.Value, "en", "es", True)
TextBox18 = Translate(TextBox13.Value, "en", "es", True)
TextBox19 = Translate(TextBox14.Value, "en", "es", True)
End Sub
Private Sub CommandButton7_Click()
Dim rng As Range
Set rng = Locations.Worksheets(Page).ListObjects("Table15").Range
Dim LastRow As Long
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = TextBox10.Value
rng.Parent.Cells(LastRow + 1, 2).Value = TextBox15.Value
SortAsc2
End Sub
Private Sub CommandButton8_Click()
Dim rng As Range
Set rng = Locations.Worksheets(Page).ListObjects("Table24").Range
Dim LastRow As Long
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox11.Value
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox12.Value
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox13.Value
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox14.Value
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox16.Value
rng.Parent.Cells(LastRow + 1, 9).Value = TextBox17.Value
rng.Parent.Cells(LastRow + 1, 10).Value = TextBox18.Value
rng.Parent.Cells(LastRow + 1, 11).Value = TextBox19.Value
SortAsc2
End Sub
Private Sub OkButton_Click()
ThisWorkbook.Sheets("WO_Cover").Cells(1, 9).Value = TextBox1.Value
ThisWorkbook.Sheets("First Piece Inspection").Cells(5, 7).Value = TextBox1.Value
ThisWorkbook.Sheets("Production Inspection").Cells(5, 7).Value = TextBox1.Value
ThisWorkbook.Sheets("WO_Cover").Cells(3, 2).Value = TextBox2.Value
ThisWorkbook.Sheets("WO_Cover").Cells(3, 8).Value = TextBox3.Value
ThisWorkbook.Sheets("WO_Cover").Cells(4, 8).Value = TextBox4.Value
ThisWorkbook.Sheets("WO_Cover").Cells(4, 5).Value = TextBox5.Value
ThisWorkbook.Sheets("WO_Cover").Cells(5, 2).Value = ComboBox1.Value
ThisWorkbook.Sheets("WO_Cover").Cells(10, 1).Value = TextBox7.Value
ThisWorkbook.Sheets("WO_Cover").Cells(10, 3).Value = TextBox8.Value
ThisWorkbook.Sheets("WO_Cover").Cells(20, 3).Value = TextBox9.Value
ThisWorkbook.Sheets("WO_Cover").Cells(21, 2).Value = ComboBox2.Value
ThisWorkbook.Sheets("WO_Cover").Cells(24, 2).Value = ComboBox3.Value
ThisWorkbook.Sheets("WO_Cover").Cells(27, 2).Value = ComboBox4.Value
ThisWorkbook.Sheets("WO_Cover").Cells(30, 2).Value = ComboBox5.Value
ThisWorkbook.Sheets("WO_Cover").Cells(33, 2).Value = ComboBox6.Value
ThisWorkbook.Sheets("WO_Cover").Cells(36, 2).Value = ComboBox7.Value
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value Then
Me.Width = 375
Me.Height = 520
Auto_Fill_Form.Label17.Visible = True
Auto_Fill_Form.TextBox10.Visible = True
Auto_Fill_Form.CommandButton5.Visible = True
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
Else
Me.Width = 250
Me.Height = 520
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value Then
Me.Width = 375
Me.Height = 520
Auto_Fill_Form.Label18.Visible = True
Auto_Fill_Form.TextBox11.Visible = True
Auto_Fill_Form.Label19.Visible = True
Auto_Fill_Form.TextBox12.Visible = True
Auto_Fill_Form.Label20.Visible = True
Auto_Fill_Form.TextBox13.Visible = True
Auto_Fill_Form.Label21.Visible = True
Auto_Fill_Form.TextBox14.Visible = True
Auto_Fill_Form.CommandButton6.Visible = True
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
Else
Me.Width = 250
Me.Height = 520
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ThisWorkbook.Sheets("WO_Cover").Cells(1, 9).Value
TextBox2.Value = ThisWorkbook.Sheets("WO_Cover").Cells(3, 2).Value
TextBox3.Value = ThisWorkbook.Sheets("WO_Cover").Cells(3, 8).Value
TextBox4.Value = ThisWorkbook.Sheets("WO_Cover").Cells(4, 8).Value
'TextBox5.Value = ThisWorkbook.Sheets("WO_Cover").Cells(4, 5).Value
ComboBox1.Value = ThisWorkbook.Sheets("WO_Cover").Cells(5, 2).Value
TextBox7.Value = ThisWorkbook.Sheets("WO_Cover").Cells(10, 1).Value
TextBox8.Value = ThisWorkbook.Sheets("WO_Cover").Cells(10, 3).Value
TextBox9.Value = ThisWorkbook.Sheets("WO_Cover").Cells(20, 3).Value
ComboBox2.Value = ThisWorkbook.Sheets("WO_Cover").Cells(21, 2).Value
ComboBox3.Value = ThisWorkbook.Sheets("WO_Cover").Cells(24, 2).Value
ComboBox4.Value = ThisWorkbook.Sheets("WO_Cover").Cells(27, 2).Value
ComboBox5.Value = ThisWorkbook.Sheets("WO_Cover").Cells(30, 2).Value
ComboBox6.Value = ThisWorkbook.Sheets("WO_Cover").Cells(33, 2).Value
ComboBox7.Value = ThisWorkbook.Sheets("WO_Cover").Cells(36, 2).Value
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
Auto_Fill_Form.Label26.Visible = False
Auto_Fill_Form.TextBox15.Visible = False
Auto_Fill_Form.CommandButton7.Visible = False
Auto_Fill_Form.Label22.Visible = False
Auto_Fill_Form.TextBox16.Visible = False
Auto_Fill_Form.Label23.Visible = False
Auto_Fill_Form.TextBox17.Visible = False
Auto_Fill_Form.Label24.Visible = False
Auto_Fill_Form.TextBox18.Visible = False
Auto_Fill_Form.Label25.Visible = False
Auto_Fill_Form.TextBox19.Visible = False
Auto_Fill_Form.CommandButton8.Visible = False
'TextBox10.Value =
End Sub
Private Sub UserForm_Initialize()
'Empty Form
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
ComboBox1.Value = Null
'Initialized value
TextBox5.Value = "A"
ComboBox2.Value = "N/A"
ComboBox3.Value = "N/A"
ComboBox4.Value = "N/A"
ComboBox5.Value = "N/A"
ComboBox6.Value = "N/A"
ComboBox7.Value = "N/A"
Me.ComboBox1.List = Locations.Worksheets(Page).ListObjects("Table15").ListColumns(1).DataBodyRange.Value
Me.ComboBox2.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox3.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox4.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox5.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox6.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox7.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
End Sub
Module 1:
Private wLocations As Workbook
Public Const Page = "Description - Processes"
Public Property Get Locations() As Workbook
Const sPath As String = "V:\My Stuff\Templates\Work Order Batch Creation Template\WorkOrderDatabase.xlsm"
Dim sFile As String
If wLocations Is Nothing Then
'extract file name from full path
sFile = Dir(sPath)
On Error Resume Next
'check if the file is already open
Set wLocations = Workbooks(sFile)
If wLocations Is Nothing Then
Set wLocations = Workbooks.Open(sPath)
End If
On Error GoTo 0
End If
Set Locations = wLocations
End Property
Module 2:
Sub Auto_Open()
Application.OnKey "{F3}", "ShowForm"
End Sub
Sub ShowForm()
Auto_Fill_Form.Show
End Sub
Module 3:
Sub SortAsc2()
Locations.Worksheets(Page).Range("A2:B16").Sort Key1:=Locations.Worksheets(Page).Range("A2"), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Locations.Worksheets(Page).Range("D2:K16").Sort Key1:=Locations.Worksheets(Page).Range("D2"), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
' Locations.Worksheets("Description - Processes").Range("A2:B16").Sort Key1:=Locations.Worksheets("Description - Processes").Range("A2"), _
' Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
' Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'
' Locations.Worksheets("Description - Processes").Range("D2:K16").Sort Key1:=Locations.Worksheets("Description - Processes").Range("D2"), _
' Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
' Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End Sub
Module 4:
Public Function Translate(strInput As String, strFromLanguageCode As String, strToLanguageCode As String, blnTargetAlphabet As Boolean) As String
Dim strURL As String
Dim objHTTP As Object
Dim objHTML As Object
Dim objDivs As Object, objDiv
Dim strTranslatedT0 As String
Dim strTranslatedO1 As String
' send query to web page
strURL = "https://translate.google.com/m?hl=" & strFromLanguageCode & _
"&sl=" & strFromLanguageCode & _
"&tl=" & strToLanguageCode & _
"&ie=UTF-8&prev=_m&q=" & strInput
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", strURL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ""
' create a html document
Set objHTML = CreateObject("htmlfile")
With objHTML
.Open
.Write objHTTP.responseText
.Close
End With
' o1 has Anglicised translation, t0 as tranlsation in target language
Set objDivs = objHTML.getElementsByTagName("div")
For Each objDiv In objDivs
If objDiv.className = "o1" Then
strTranslatedO1 = objDiv.innerText
End If
If objDiv.className = "t0" Then
strTranslatedT0 = objDiv.innerText
End If
Next objDiv
' choose which to return
If blnTargetAlphabet Then
Translate = strTranslatedT0
Else
Translate = strTranslatedO1
End If
CleanUp:
Set objHTML = Nothing
Set objHTTP = Nothing
End Function
Open the workbook to collect the data.
If you close the wb right away, you will have to get rid of the workbook.close line in the "Quit" Button
Private Sub UserForm_Initialize()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\dmorrison\Downloads\TestRemoveLater\WorkOrderDatabase-1.xlsm")
'Empty Form
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
ComboBox1.Value = Null
'Initialized value
TextBox5.Value = "A"
ComboBox2.Value = "N/A"
ComboBox3.Value = "N/A"
ComboBox4.Value = "N/A"
ComboBox5.Value = "N/A"
ComboBox6.Value = "N/A"
ComboBox7.Value = "N/A"
With wb
Me.ComboBox1.List = .Worksheets(Page).ListObjects("Table15").ListColumns(1).DataBodyRange.Value
Me.ComboBox2.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox3.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox4.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox5.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox6.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox7.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
'.close
End With
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
End Sub
I am using excel VBA to create a new sheet then copy data from another sheet to this new sheet I created. Then I will format the new sheet by deleting some columns and text wrapping. It does the job alright However it is not efficient: The screen flickers so much despite using Application.DisplayAlerts = False, Application.EnableEvents = False.
Any help?
Sub ProcessPostingData()
Dim MyDateTime As String
Dim szToday As String
Dim szTime As String
Dim TD, TM As String
Dim AfterFilterFinalRow As Long
Dim lLastRow3nd As Long
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Sheets("szTempNow").Delete
On Error GoTo 0
Sheets.Add().Name = "szTempNow"
Worksheets("DATA_PROCESSING").Select
lLastRow3nd = Cells(1, 6).EntireColumn.Find("*", SearchDirection:=xlPrevious).Row
'We sort,create sheet with DateTime stamp,copy data to new sheet and format
ActiveWorkbook.Worksheets("DATA_PROCESSING").Range(Cells(1, 1), Cells(lLastRow3nd, 10)).Sort _
Key1:=Range("A1"), Header:=xlYes
With Worksheets("DATA_PROCESSING")
AfterFilterFinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Sheets("DATA_PROCESSING").Range("A1:J1").Copy Destination:=Sheets("szTempNow").Range("A1")
Sheets("szTempNow").Range("A2:J" & AfterFilterFinalRow).Value = Sheets("DATA_PROCESSING").Range("A2:J" & AfterFilterFinalRow).Value
Sheets("DATA_PROCESSING").Range(Cells(2, 1), Cells(AfterFilterFinalRow, 10)).EntireRow.Delete
'Removing columns not needed and formating
Sheets("szTempNow").Select
'With Sheets("szTempNow")
.Columns("G:G").Delete Shift:=xlToLeft
.Columns("D:E").Delete Shift:=xlToLeft
End With
'With Range(Cells(1, 1), Cells(AfterFilterFinalRow, 10))
'.HorizontalAlignment = xlGeneral
'.VerticalAlignment = xlCenter
'.WrapText = True
'.ReadingOrder = xlContext
'End With
'Range("E2:E" & AfterFilterFinalRow).Columns("E:E").ColumnWidth = 70
'Rename Sheet with Todays date and Time
szTime = Format(Time, "h-mm AM/PM")
szToday = Format(Now(), "dd-mmm-yyyy")
TD = "D"
TM = "T"
MyDateTime = TD & szToday & TD & "_" & TM & szTime & TM
ActiveSheet.Name = MyDateTime
Range("K1").Value = ActiveSheet.Name
Range("K1").Font.Bold = True
With Range("K1")
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.ReadingOrder = xlContext
End With
Application.EnableEvents = False
Application.DisplayAlerts = True
End Sub
You are looking for:
Application.ScreenUpdating = False
That's the one that helps stop screen flickering, and can also speed up processing. Application.DisplayAlerts suppresses dialogs along the lines of "This Workbook is Unsaved."