I'm trying to automate excel modificaions.
The process works like this:
- The Excel list gets created.
- It needs to be manually processed by an employee (removing images, sorted alphabetically, etc.)
- The list gets converted into a csv file.
- CSV gets uploaded and processed.
Now I would like to automate this process as much as possible. I don't have any experience working with VBA or Excel macros.
So far i've been able to scramble a few different scripts together to get halfway, but I haven't been able to get these two functions working. I've been able to remove all the bloat at the top (not at the bottom yet), remove empty rows and remove unused columns.
I can't post the contents of the sheet itself because of privacy reasons, but the structure of the sheet looks like this:
| Name | Cost |
| Mark Renner (mare) | €200,- |
Question
I want to extract the 4 letter codes and replace them for the full names so only the 4 letter code remains in the cell.
Also I would like the list to be sorted alphabetically. The range of the sheet differs per day so there is no fixed ammount of cells.
There is nothing else on the sheet you need to worry about. I can provide more information if necessary.
It would be tremendous if someone is able to help me with this.
Thanks in advance!
Edit:
Here is some more requested information.
Table example after current script
This is the script I am currently using to remove all the bloat. I'm sure it's not perfect but it does the job for now.
Sub run()
Call testvba
Call DeleteRowWithContents
Call usedR
End Sub
Sub testvba()
Dim i As Integer
For i = 1 To 21
Rows(1).EntireRow.Delete
Next i
For i = 1 To 10
Columns(4).EntireColumn.Delete
Next i
Dim shape As Excel.shape
For Each shape In ActiveSheet.Shapes
shape.Delete
Next
End Sub
Sub DeleteRowWithContents()
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "User" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
Sub usedR()
ActiveSheet.UsedRange.Select
'Deletes the entire row within the selection if the ENTIRE row contains no data.
Dim i As Long
'Turn off calculation and screenupdating to speed up the macro.
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
'Work backwards because we are deleting rows.
For i = Selection.Rows.Count To 1 Step -1
If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
Selection.Rows(i).EntireRow.Delete
End If
Next i
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub `
Here is the table before the script:
SOLUTION:
I used Schalton's code for extracting the 4 letter code.
I ended up using this line of code to alphabetize the records:
Sub Alpha()
Dim fromRow As Integer
Dim toRow As Integer
fromRow = 1
toRow = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Rows(fromRow & ":" & toRow).Sort Key1:=ActiveSheet.Range("A:A"), _
Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End Sub