98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA syntax improvement
Excel VBA syntax improvement
Resolved · Urgent Priority · Version 2010
Bin has attended:
Excel VBA Intro Intermediate course
Access Introduction course
Access Intermediate course
Access Advanced course
Excel VBA Advanced course
Excel VBA syntax improvement
Hi, I created a macro to sort some raw data. It works very slow and sorts the data one row at a time. Is there anyway can make it run faster?
In order to remove the specific rows I inserted an extra column with “x”at Column A first. Is there any other way that I don’t need the extra column while it can do the same work?
I want to remove the last three rows of the raw data, every time i run the macro. I created a Dim function which seems not working in the macro, but if I test it separately it works well. Could you please have a look and see why.
How can I rename the field titles as listed below? The field title in the raw data is actually split into two rows. You see if I run the macro the top part will be removed and then there will be two columns with same field title. Don’t know if it matters but is there any way I can replace the old title to the new title as listed below?
I planed to make the macro acts in the order as listed below:
1.Remove the top 10 rows
2.Remove column M “Remarks”
3.Remove any row that start with blank cells or “__” or “Cy” or, has blank cells in column D
4.Rename fields name as per my email
5.Adjust the column with
For any questions please let me know. I can send the raw data over if it is needed.
Many thanks
Bin
VBA code i made have listed below:
Option Explicit
Sub RemoveTop10Rows()
Dim iRows As Integer
'remove the top 10 rows
Rows("1:10").Delete
'insert a column in column A and fill with data "x"
Range("a1").EntireColumn.Insert
ActiveCell.EntireColumn.Value = "x"
'go to a2 and use do loop to remove all rows that start with " ","Cy","__" in column B. _
Do until the row starts with "To"
Range("a2").Select
Do Until ActiveCell.Offset(0, 1) = "To"
If ActiveCell.Offset(0, 1) = "" Or _
ActiveCell.Offset(0, 4) = "" Or _
ActiveCell.Offset(0, 1) = "Cy" Or _
ActiveCell.Offset(0, 1) = "__" Then
'remove any row starts with ""
'remove any row starts with figures
'remove any row srates with "Cy"
'remove any row starts with "__"
ActiveCell.EntireRow.Delete
ElseIf ActiveCell.Offset(1, 0).Select Then
End If
Loop
'Remove the inserted column with "x"
Range("a1").EntireColumn.Delete
'go to A1 cell
Range("a1").Select
'use dim to count the number of rows after removed blank rows
iRows = Range("a1").CurrentRegion.Rows.Count
'go to the last row in the data area and remove the (the dim seems not working, but works if i run it separately to only this small session)
Selection.Offset(iRows - 1, 0).EntireRow.Delete
Selection.Offset(1, 0).EntireRow.Delete
Selection.Offset(1, 0).EntireRow.Delete
End Sub
RE: excel VBA syntax improvement
Hi Bin,
Here are two bits of code which may help:
1. To dynamically count and delete the last 10 rows of data:
Dim LastRows As Range
Set LastRows = Cells(Rows.Count, "A").End(xlUp)
LastRows.Offset(-9).Resize(10).EntireRow.Delete
This code counts the number of rows with data in column A, then deletes the last ten rows across all columns.
2. To find a certain column header (e.g. Remarks) regardless of which column it's in, then delete the entire column.
Dim rng As Range
With Worksheets("sheet1").Range("A1:z1")
Set rng = Worksheets("sheet1").Range("A1:z1") _
.Find(What:="Remarks", _
LookAt:=xlWhole, MatchCase:=False)
Do While Not rng Is Nothing
rng.EntireColumn.Delete
Set rng = .FindNext
Loop
End With
I hope this helps.
Kind regards
Marius Barnard
Best STL
RE: excel VBA syntax improvement
Thanks Marius for your reply.
I will try your above codes and see how it goes.
About my codes, is there any way can make it run faster? It currently runs one row at a time which is quite cime consuming if i run larger amount of data. Is there any other Dim function which can run the actions of removing rows in computer memory?
How can i rename the row header?
Many thanks
Bin
RE: excel VBA syntax improvement
Hi Bin,
To make code run faster and clear memory when you're running a loop, you could use
Application.Screenupdating = False
and
Application.CutCopyMode = False
near the start of your code.
Which row header do you need to rename?
Kind regards
Marius
RE: excel VBA syntax improvement
Hi Marius,
1) Rename Headers
After i removed the tope 10 rows, i will get the header the same as below: RawData Title line. I want to replace all of them with below "New Title" line.
Column title: A, B, C, D, E, F, G, H, I, J, K, L, M
RawData title: Cy, City, Supplier, City, Date, Site/Bkg, Agent, Nat, Tyrpe Tpye, Resp., Reason, /Loss ST, Remarks
New Title: Country Code, Supp City, Supplier, Svc.City, Arri Date, Tour Ref.Site/Bkg, Agent, Nat, Comp Type, Resp., Reason, Profit/Loss Status, Remarks
2) Make the macro runs faster
To use below two codes, should i put them together like below and place them right under "Do Until" statement as the below example indicates?
Application.Screenupdating = False
Application.CutCopyMode = False
E.g.:
Do Until ActiveCell.Offset(0, 1) = "To"
Application.Screenupdating = False
Application.CutCopyMode = False
If ActiveCell.Offset(0, 1) = "" Or _
ActiveCell.Offset(0, 4) = "" Or _
ActiveCell.Offset(0, 1) = "Cy" Or _
ActiveCell.Offset(0, 1) = "__" Then
'remove any row starts with ""
'remove any row starts with figures
'remove any row srates with "Cy"
'remove any row starts with "__"
ActiveCell.EntireRow.Delete
ElseIf ActiveCell.Offset(1, 0).Select Then
End If
Loop
Many thanks
Bin
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Shortcut fill a cell with contents from adjacent cellsUse Control + D to fill a cell with the data from an adjacent cell. This speeds up data entry and is a cool tip to share! |