jachym
New Member
- Joined
- Jul 28, 2015
- Messages
- 3
- Office Version
- 2016
- Platform
- Windows
Hi guys,
I am quite new to VBA programming, but I have some experience with other computer languages so it is not that complicated for me.
I created several macros and I ran into a problem. I created several useful macros, which should speed up my work. However, this turned out to be only theoretical.
I will include just one example, but I struggle with the same issue in the other macros too. Theoretically they work fine and do what I want them to do, but the problem is speed. I tested them using a sheet with just a few cells. It worked and I was happy about it, but only until I opened a real file and tried to use it.
Example:
This macro was supposed to select all cells larger than user selected value. Very useful for me. It works, but when I tested it with a large document, i. e. document of a size I normally work with (approximately 20k rows and 25 columns) it was so slow that in the end the entire Excel froze. Trying for the second time it worked and selected the cells after about 3 minutes.... and I am using a relatively fast PC with i5 processor and 32GB of RAM.
The above code theoretically works for small number of cells, but in practice is unusable. My question is, is there a way to make this more efficient? Make it select the cells in say a few seconds? Are loops the only way of doing this and in general, is it normal for loops to take this long to execute?
Thanks in advance for any tips and info
I am quite new to VBA programming, but I have some experience with other computer languages so it is not that complicated for me.
I created several macros and I ran into a problem. I created several useful macros, which should speed up my work. However, this turned out to be only theoretical.
I will include just one example, but I struggle with the same issue in the other macros too. Theoretically they work fine and do what I want them to do, but the problem is speed. I tested them using a sheet with just a few cells. It worked and I was happy about it, but only until I opened a real file and tried to use it.
Example:
This macro was supposed to select all cells larger than user selected value. Very useful for me. It works, but when I tested it with a large document, i. e. document of a size I normally work with (approximately 20k rows and 25 columns) it was so slow that in the end the entire Excel froze. Trying for the second time it worked and selected the cells after about 3 minutes.... and I am using a relatively fast PC with i5 processor and 32GB of RAM.
VBA Code:
Sub SelectCellsLarger()
Dim ws As Worksheet
Dim SelectCells As Range
Dim xcell As Object
Set ws = ActiveSheet
Dim value As String
Dim n As Integer
n = 0
value = InputBox("Insert a value", "")
If IsNumeric(value) = False Then
MsgBox "Insert a numeric value"
Exit Sub
End If
If StrPtr(value) = 0 Then
Exit Sub
End If
For Each xcell In ws.UsedRange.Cells
If xcell.value > value And IsNumeric(xcell.value) = True And IsEmpty(xcell.value) = False Then
n = n + 1
If SelectCells Is Nothing Then
Set SelectCells = Range(xcell.Address)
Else
Set SelectCells = Union(SelectCells, Range(xcell.Address))
End If
End If
Next
On Error Resume Next
SelectCells.Select
MsgBox "Selected" & n & " cells."
End Sub
The above code theoretically works for small number of cells, but in practice is unusable. My question is, is there a way to make this more efficient? Make it select the cells in say a few seconds? Are loops the only way of doing this and in general, is it normal for loops to take this long to execute?
Thanks in advance for any tips and info