Speed up VBA execution - performance

So I have the below VBA macros setup and when CompHide runs it takes several minutes to update. I feel like this is due to the line that says C.EntireRow.Columns(43).Value = ""
I tried making a new "helper" column that would check if both of the columns were empty and had it return "Y" or "N" and then had the macro look at that for "Y" and hide those. This sped it up some but I am wanting to get even faster if I could.
Orginal code:
Sub CompHide()
Dim sht As Worksheet, C As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Set sht = Sheets("Comparison")
sht.Rows.Hidden = False
CSetRowVis "C9", "CMarket1"
CSetRowVis "C115", "CMarket2"
CSetRowVis "C221", "CMarket3"
CSetRowVis "C329", "CMarket4"
CSetRowVis "C437", "CMarket5"
CSetRowVis "C545", "CMarket6"
CSetRowVis "C653", "CMarket7"
CSetRowVis "C761", "CMarket8"
CSetRowVis "C869", "CMarket9"
CSetRowVis "C977", "CMarket10"
For Each C In sht.Range("CNonTest")
If C.Value = "" And C.EntireRow.Columns(43).Value = "" Then
C.EntireRow.Hidden = True
End If
Next
sht.Range("CBlank").EntireRow.Hidden = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub CSetRowVis(addr As String, rngName As String)
With Sheets("Comparison")
If .Range(addr).Value = "Unused" Then
.Range(rngName).EntireRow.Hidden = True
End If
End With
End Sub
New Code:
Sub CompHide()
Dim sht As Worksheet, C As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Set sht = Sheets("Comparison")
sht.Rows.Hidden = False
CSetRowVis "C9", "CMarket1"
CSetRowVis "C115", "CMarket2"
CSetRowVis "C221", "CMarket3"
CSetRowVis "C329", "CMarket4"
CSetRowVis "C437", "CMarket5"
CSetRowVis "C545", "CMarket6"
CSetRowVis "C653", "CMarket7"
CSetRowVis "C761", "CMarket8"
CSetRowVis "C869", "CMarket9"
CSetRowVis "C977", "CMarket10"
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
C.EntireRow.Hidden = True
End If
Next
sht.Range("CBlank").EntireRow.Hidden = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub CSetRowVis(addr As String, rngName As String)
With Sheets("Comparison")
If .Range(addr).Value = "Unused" Then
.Range(rngName).EntireRow.Hidden = True
End If
End With
End Sub
This is using Excel 2013 Standard 64 bit. Number of rows is just under 1200. Number of Columns is 150. All of those cells are formulas
If there is any extra info you need let me know.

Instead of this:
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
C.EntireRow.Hidden = True
End If
Next
consider something like this:
Dim rng As Range 'for collecting rows to be hidden
For Each C In sht.Range("CHideTest")
If C.Value = "Y" Then
if rng Is Nothing Then
set rng = C
Else
set rng = application.union(rng, C)
end if
End If
Next
'hide all accumulated rows (if any found)
if not rng is nothing then rng.EntireRow.Hidden = True

Related

Run time error 5:invalid procedure call or argument

Here is the code which I have written in vb6...it is showing error in text1.setfocus
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
End If
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub
Do not have this code in LostFocus, instead try to have it in the Validate event, there would be an cancel parameter to the event, if you set Cancel = True (means the cursor will not exit the control) you need not do setfocus
Try the following:
Private Sub Text1_Validate(Cancel As Boolean)
If IsNumeric(Text1.Text) = False Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Cancel = True
End If
End Sub
Try this if you have an empty string and you are trying to work with in in your loop you can get an invalid procedure call. skip over it all togehter don't run the loop if the text is empty.
Private Sub Text1_Lostfocus()
s1 = Text1.Text
flag = 0
If Text1.Text = "" Then
flag = 1
else
For i = 1 To Len(s1)
l = Mid(s1, i, 1)
If IsNumeric(l) = True Then
flag = 1
Exit For
End If
Next i
endif
If flag = 1 Then
MsgBox "Enter valid input"
Text1.ForeColor = vbRed
Text1.SetFocus
End If
End Sub

Expand VBA code to multiple shapes

Private Sub Worksheet_Calculate()
Dim cell As Range
Set cell = Range("E9")
If IsNumeric(cell) Then
If cell.Value < 0 Then
ActiveSheet.Shapes("Rectangle 2").Fill.ForeColor.RGB = vbRed
Else
ActiveSheet.Shapes("Rectangle 2").Fill.ForeColor.RGB = vbGreen
End If
End If
End Sub
Hi, how can I expand this code to work with multiple shapes? I tried to just copy everything from Set cell to End if and changed up the Set cell value and shape but this didn't work.
Private Sub Worksheet_Calculate()
Dim x As Range
Set x = Range("E9")
If IsNumeric(x) Then
If x.Value < 0 Then
Sheets("DIA").Shapes("Rectangle 2").Fill.ForeColor.RGB = vbRed
Else
Sheets("DIA").Shapes("Rectangle 2").Fill.ForeColor.RGB = vbGreen
End If
End If
Dim y As Range
Set y = Range("T9")
If IsNumeric(y) Then
If y.Value < 0 Then
Sheets("DIA").Shapes("Rectangle 19").Fill.ForeColor.RGB = vbGreen
Else
Sheets("DIA").Shapes("Rectangle 19").Fill.ForeColor.RGB = vbRed
End If
End If
End Sub
This works, any smarter ways?

Run a word vba macro from vb script

How can I call a word vba macro code from a VB Script:
the word vba macro code is under:
Sub find_replace_vik_42216()
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "abc"
.Replacement.Text = "def"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "pqr"
.Replacement.Text = "xyz"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
Application.ScreenUpdating = True
End Sub
could dear members create a vb script file which contains the above code, so that I call the vb script , in order to run the code.
I have wrecked my brains and troubled google, to no avail. Please help.
Thank you.
Vik
There is an example of VBScript code which opens the document and make two replacements with given options, more compact form of the .Find.Execute method used:
Const wdFindContinue = 1
Const wdReplaceOne = 1
Dim objWord, objDocument
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDocument = objWord.Documents.Open("C:\test.docx")
With objWord
.ScreenUpdating = False
With .Selection
.Collapse
With .Find
' .Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
' "abc" -> "def"
.Execute "abc", False, False, False, False, False, True, wdFindContinue, False, "def", wdReplaceOne
' "pqr" -> "xyz"
.Execute "pqr", False, False, False, False, False, True, wdFindContinue, False, "xyz", wdReplaceOne
End With
End With
.ScreenUpdating = True
End With

How must I write this code without error

I want to write code that writes something into a file but it says it can't. How can I fix this? Please help.
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
yes.Visible = False
no.Visible = False
Label1.Visible = False
ProgressBar1.Visible = False
Label2.Visible = False
Label3.Visible = False
TextBox1.Visible = False
TextBox2.Visible = False
apply.Visible = False
back.Visible = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
yes.Visible = True
no.Visible = True
Label1.Visible = True
setings.Visible = False
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles yes.Click
Label1.Text = "dowloading"
no.Visible = False
yes.Visible = False
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles no.Click
yes.Visible = False
no.Visible = False
Label1.Visible = False
Button1.Visible = True
setings.Visible = True
End Sub
Private Sub setings_Click(sender As Object, e As EventArgs) Handles setings.Click
Label2.Visible = True
Label3.Visible = True
TextBox1.Visible = True
TextBox2.Visible = True
apply.Visible = True
back.Visible = True
Button1.Visible = False
setings.Visible = False
End Sub
Private Sub back_Click(sender As Object, e As EventArgs) Handles back.Click
Label2.Visible = False
Label3.Visible = False
TextBox1.Visible = False
TextBox2.Visible = False
apply.Visible = False
back.Visible = False
Button1.Visible = True
setings.Visible = True
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub apply_Click(sender As Object, e As EventArgs) Handles apply.Click
Dim forgepath = TextBox1.Text
Dim savefolder = Path.Combine(TextBox2.Text, "crazydolphininstaller")
Directory.CreateDirectory(savefolder)
Dim configfolder = Path.Combine(savefolder, "config")
Directory.CreateDirectory(configfolder)
Dim configfile = Path.Combine(configfolder, "config.txt")
File.Create(configfile)
Using writer = New StreamWriter(configfile)
writer.WriteLine(forgepath)
writer.WriteLine(savefolder)
End Using
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
End Class
File.Create creates the file and returns a FileStream opened for you. Thus the following StreamWriter finds the file opened and cannot write in it. Just remove the File.Create line and use the StreamWriter constructor that allows the overwriting of the file if it exists
Private Sub apply_Click(sender As Object, e As EventArgs) Handles apply.Click
Dim forgepath = TextBox1.Text
Dim savefolder = Path.Combine(TextBox2.Text, "crazydolphininstaller")
Dim configfolder = Path.Combine(savefolder, "config")
' Called just one time. All the folder missing will be created
Directory.CreateDirectory(configfolder)
Dim configfile = Path.Combine(configfolder, "config.txt")
' Not needed
' File.Create(configfile)
' Pass False as second parameter to overwrite the file if it exists
Using writer = New StreamWriter(configfile, False)
writer.WriteLine(forgepath)
writer.WriteLine(savefolder)
End Using
End Sub

Picture changing in vb6

I was trying this script from a pdf file.I got stuck where the target image should change to exploding image if clicked but the target image does not change from the standing image.Please Help!
Option Explicit
Dim fiPlayersScore As Integer
Dim fiNumberofMisses As Integer
Dim fbTargetHit As Boolean
Private Sub Form_Load()
Randomize
imgTarget.Enabled = False
imgTarget.Visible = False
cmdStop.Enabled = False
lblGameOver.Visible = False
lblGameOver.Enabled = False
End Sub
Private Sub cmdStart_Click()
Dim lsUserResponse As String
Dim lbResponse As Boolean
lsUserResponse = InputBox("Enter a level from 1 to 3." & _
(Chr(13)) & "" & (Chr(13)) & "1 being the Easiest and 3 being the " & _
"Hardest.", "Level Select", "1")
lbResponse = False
If lsUserResponse = "1" Then
Timer1.Interval = 1500
lbResponse = True
ElseIf lsUserResponse = "2" Then
Timer1.Interval = 1000
lbResponse = True
ElseIf lsUserResponse = "3" Then
Timer1.Interval = 750
lbResponse = True
Else
MsgBox ("Game Not Started.")
lbResponse = False
End If
If lbResponse = True Then
cmdStart.Enabled = False
imgTarget.Picture = imgStanding.Picture
frmMain.MousePointer = 5
fbTargetHit = False
Load_Sounds
cmdStop.Enabled = True
fiPlayersScore = 0
fiNumberofMisses = 0
lblScore.Caption = fiPlayersScore
lblMisses.Caption = fiNumberofMisses
Timer1.Enabled = True
lblGameOver.Visible = False
lblGameOver.Enabled = False
End If
End Sub
Private Sub cmdStop_Click()
Unload_Sounds
frmMain.MousePointer = vbNormal
Timer1.Enabled = False
imgTarget.Enabled = False
imgTarget.Visible = False
cmdStart.Enabled = True
cmdStop.Enabled = False
cmdStart.SetFocus
lblGameOver.Visible = True
lblGameOver.Enabled = True
End Sub
Private Sub Form_Click()
MMControl1.Command = "Play"
MMControl1.Command = "Prev"
fiNumberofMisses = fiNumberofMisses + 1
lblMisses.Caption = fiNumberofMisses
If CheckForLoose = True Then
cmdStop_Click
lblMisses.Caption = fiNumberofMisses
Exit Sub
End If
End Sub
Private Sub imgTarget_Click()
MMControl2.Command = "Play"
MMControl2.Command = "Prev"
Timer1.Enabled = False
imgTarget.Picture = imgExplode.Picture '**I AM STUCK HERE**
pauseProgram
fiPlayersScore = fiPlayersScore + 1
Timer1.Enabled = True
If CheckForWin = True Then
cmdStop_Click
lblScore.Caption = fiPlayersScore
Exit Sub
End If
lblScore.Caption = fiPlayersScore
fbTargetHit = True
imgStanding.Enabled = False
imgTarget.Visible = False
imgTarget.Enabled = False
Timer1.Enabled = True
End Sub
Public Sub Load_Sounds()
'Set initial property values for blaster sound
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = _
"C:\Temp\Sounds\Blaster_1.wav"
'Open the media device
MMControl1.Command = "Open"
Private Sub Timer1_Timer()
Dim liRandomLeft As Integer
Dim liRandomTop As Integer
imgTarget.Visible = True
If fbTargetHit = True Then
fbTargetHit = False
imgTarget.Picture = imgStanding.Picture
End If
liRandomLeft = (6120 * Rnd)
liRandomTop = (4680 * Rnd)
imgTarget.Left = liRandomLeft
imgTarget.Top = liRandomTop
imgTarget.Enabled = True
imgTarget.Visible = True
End Sub
Public Function CheckForWin() As Boolean
CheckForWin = False
If fiPlayersScore = 5 Then
CheckForWin = True
lblGameOver.Caption = "You Win.Game Over"
End If
End Function
Public Function CheckForLoose() As Boolean
CheckForLoose = False
If fiNumberofMisses = 5 Then
CheckForLoose = True
lblGameOver.Caption = "You Loose.Game Over"
End If
End Function
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
Unload_Sounds
End Sub
Public Sub Unload_Sounds()
MMControl1.Command = "Close"
MMControl2.Command = "Close"
End Sub
Public Sub pauseProgram()
Dim currentTime
Dim newTime
currentTime = Second(Time)
newTime = Second(Time)
Do Until Abs(newTime - currentTime) >= 1
newTime = Second(Time)
Loop
End Sub
EDIT:
imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
Note:
Set imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
will be faster than
imgTarget.Picture = imgExplode.Picture
imgTarget.Refresh
if imgExplode is going to be around during the lifetime of imgTarget (the first command copies the image, the Set command references the image).

Resources