Question:
(Microsoft VBA) Is it possible to run two functions simultaneously in one macro?
Br_T
2010-01-23 01:02:06 UTC
When we program a macro, it normally performs a string of long actions chronologically. Is it possible to group these actions and run them simultaneously, and perhaps loop them all in one macro.
Four answers:
AnalProgrammer
2010-01-23 01:40:18 UTC
Generally speaking the code that you write in VBA is structured and therefore one action follows another.

You may like to check the attached link for a possible way of making concurrent actions.



Have fun.
devilishblueyes
2010-01-28 06:05:23 UTC
I sort of get what you're saying and sort of don't. Because you could mean it probably 2 different ways. And I want to avoid confusing you.



Macros when they are programmed basically read from the top of the code and read to the bottom of the code. And the computer HAS to perform those actions in that order. You can't have the computer performing two or more operations at once. For example, I can't have it pasting data on sheet1 while it is coloring cells on sheet2. It has to do one or the other first. And which one it does first depends on which one is stated first in the code.



Having said that, there are some ways to speed up your code. And there are some ways to group your code to simplify it.



A simple way to speed up your code is to set the ScreenUpdating to false. Basically this avoids updating the screen everytime your macro makes a change. You want to turn the ScreenUpdating back on though at the end of your code so you can see the results of the changes that your macro made. This is great to use for For Next loops or Do While loops where thousands of changes can take place. Instead of making 1,000's of changes to the screen, it only makes one at the end. The other changes just happen in the background in the computer's memory and that happens a lot faster than the screen changes. Here's an example of how you can do it:



Application.ScreenUpdating = False



For x = 1 To 50001



Cells(x, 1).Value = "Hello"



Next x



Application.ScreenUpdating = True



By adding those two screenupdating lines you just eliminated 50,000 screen updates.





As far as grouping your code, you can do that with the Call procedure. You can write your Subs and Functions in a Module and then you can call that Sub in a different Sub. I like to use this for particularly long macros because it allows me to break the macro down into parts and some of those parts I can use multiple times if I want by passing data to them. It can save writing a lot of code that you normally would otherwise have to write.



Private Sub cmdSPI_Click()

'Checks for entries that would cause errors

CheckSPI = False

CheckNoICQ = False



Call SPI.MyChecks



If CheckSPI = True Then Exit Sub



Call SPI.CleaningProcedure

Call SPI.DateSPI

Call SPI.PartNumber

Call SPI.PreservationMethod

Call SPI.Instructions

Call SPI.DimsAndWeights

Call SPI.PackCube

Call SPI.PartNSN

Call SPI.SupplementalNSN

Call SPI.Change2UpperCase

Call SPI.CushionThickness

Call SPI.UnitContainerLevel

Call SPI.SpecialMarking



If CheckNoICQ = False Then

Call SPI.ICQ

End If



Call SPI.QUP

Call SPI.WSF

Call SPI.ContactPreservative

Call SPI.UnitContainer

Call SPI.IntermediateContainer

Call SPI.PreservationMaterial

Call SPI.WrappingMaterial

Call SPI.CushionMaterial

Call SPI.PackA

Call SPI.PackB

Call SPI.CageCode

Call SPI.MinPack

End Sub



Like the example above is a VERY large macro where I combine a bunch of smaller macros to make up the big one. SPI is the name of my module. And the names after the period are the names of the subroutines or each macro in that Module.



Then to save time, I do something even more like this:



Sub PartNSN()

Dim MyPartNSN As String



With frmPackagingCalc.txtPartNSN

MyPartNSN = Mid(.Value, 6, 8)

Call SPI.MyStringEntry(.Value, 16, 7, 15)

Call SPI.MyStringEntry(MyPartNSN, 45, 29, 8)

End With

End Sub



Notice how I used the MyStringEntry subroutine twice? I avoided having to write the entire code for that sub twice by doing that. Instead, all I had to do is pass data to it twice. To give you and Idea of how much extra I would have had to write. Here is the MyStringEntry macro that it calls twice.



Sub MyStringEntry(MyText As String, MyRow As Integer, MyFirstChar As Integer, MyLength As Integer)

Dim A As Integer, b As Integer

Dim MyUpperText As String

Dim MyString(1 To 1000) As String



MyUpperText = StrConv(MyText, vbUpperCase)



For A = 1 To MyLength

If A <= Len(MyUpperText) Then

MyString(A) = Mid(MyUpperText, A, 1)

Else

MyString(A) = ""

End If

Next A



MyFirstChar = MyFirstChar - 1

With Worksheets("CODE")

For b = 1 To MyLength

If MyString(b) = "0" Then

.Cells(MyRow, b + MyFirstChar) = Chr(216)

Else

.Cells(MyRow, b + MyFirstChar) = MyString(b)

End If

Next b

End With

End Sub
garbo7441
2010-01-23 08:26:53 UTC
There are times when the only way you can perform 'grouped' actions in VBA is to call a different macro. You can call macros from within a macro in VBA just by entering the macro name. For example, consider these two macros.



Sub SelectB32 ()

Range("B32").Select

SetInteriorColor

End Sub



Sub SetInteriorColor ()

ActiveCell.Interior.ColorIndex = 3

End Sub



The first macro selects a cell, then calls the second macro to set the color index. Now, of course, you could do that all in the first macro.



However, there are times in complex macros where trying to run a set of 'grouped actions' will fail when the macro is executed. Often, if you remove the problem code, insert it into a separate macro, and call that macro at the appropriate time in the first macro, it will process appropriately.
hukill
2016-12-08 13:47:10 UTC
sure, it fairly is a possibility, nevertheless equipment hyperlink, linking 2 360s on the comparable time. yet whilst your son lives with you yo can constantly play 3 on the comparable time on one 360, properly in case you have an HDTV it nevertheless seems good, yet relies upon on your television length. Yeah, like absolutely everyone has unhappy, maximum 360 video games you are able to play multi-participant even online with 2 - 4 debts on comparable 360, much greater in the experience that your playing Halo 3.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...