Solved

Excel Automation

Posted on 2016-09-24
17
20 Views
Last Modified: 2016-10-13
Hi
I get an orders excel workbook in a set format everyday and before i can import this sheet into my production system i have to make some changes.

Like change a few column header names, replace any blank cells with the word None, things like that.

Is there anyway to automate this type of thing in excel.

Thanks
0
Comment
Question by:kingcastle
  • 8
  • 5
  • 4
17 Comments
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
You can start by recording a macro. Click on the Developer Tab and you will see Record a macro. Click this then Excel will record the steps to produce the end result. When finished click Stop Recording. The generated code will need cleaning up but post back for help with that process.

Here's a video showing what I mean

If you cannot see the Developer Tab then read this
0
 

Author Comment

by:kingcastle
Comment Utility
oh yeah i see how that could work.
 lets say i have two columns side by side, with data in both cells.
lets say there is 10 rows and each column either has data or is blank and the column beside has the data. is there anyway to automate merging those columns to the left column and then deleting the other column?

e.g.

                               Column 1       Column2
Row 1                     large
Row 2                                             Med
Row 3                                             Large
Row 4                      Med
Row 5                     Large
Row 6                                            Small
Row 7                                            Small
Row 8                                            Large
Row 9                                            Med
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
You really do not want to merge cells. Merged cells will inevitably cause problems. Why do you want to merge cells, just resize the column.
0
 

Author Comment

by:kingcastle
Comment Utility
how would i do that, sorry about all questions.
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Hover the cursor over the right of the Column Header until it changes to a cross (+) then just drag to the required width.

Not to appear rude, but it seems that you need to hone up on basic Excel skills before tackling VBA. So many people try to use VBA for features that Excel already has.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
Comment Utility
If I understood your requirement correctly and assuming that your data is in columns A and B and you want to merge these two columns and delete the column B in the end, try this.....

Sub MergeData()
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error Resume Next
Range("A1:A" & lr).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[1]"
Range("A1:A" & lr).Value = Range("A1:A" & lr).Value
Columns("B").Delete
Application.ScreenUpdating = True
End Sub

Open in new window

In the attached, click the button on Sheet1 to get the desired output and let me know if this is what you were trying to achieve.
MergeData.xlsm
0
 

Author Comment

by:kingcastle
Comment Utility
sorry but on the drag column width that doesnt actually move the data from one column to the other.

i will try the script option now.
0
 

Author Comment

by:kingcastle
Comment Utility
yip that script is a good one.

if my columns are D and E and F is their a variable in that script that i can add to change columns letters?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
Comment Utility
The code deals with only two columns e.g. column A and B in the attached.
Just change the columns letters in the column and range references used in the code.

e.g. Range("A1:A" & lr) would become Range("D1:D" & lr) and Columns("B") would become Columns("E").
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Why do you want to drag data from one column to the other? You said that you wanted to merge cells(columns), as I said that is not a good idea. Most experts would agree that MergedCells will inevitably cause problems, especially for a beginner.
0
 

Author Comment

by:kingcastle
Comment Utility
think i made a mess there, so lets say i wanted to do this script for column D E and F and then delete E and F?
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
Comment Utility
Okay try this......
Sub MergeData()
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error Resume Next
Range("D1:E" & lr).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[1]"
Range("D1:D" & lr).Value = Range("D1:D" & lr).Value
Columns("E:F").Delete
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:kingcastle
Comment Utility
thats perfect, it puts a 0 in the cells without and data this is fine but anyway to change the 0 to the word None
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points (awarded by participants)
Comment Utility
Okay try this.....

Sub MergeData()
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error Resume Next
Range("D1:E" & lr).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=IF(RC[1]<>"""",RC[1],NA())"
Range("D1:D" & lr).SpecialCells(xlCellTypeFormulas, 16).Value = "None"
Range("D1:D" & lr).Value = Range("D1:D" & lr).Value
Columns("E:F").Delete
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:kingcastle
Comment Utility
outstanding!
0
 

Author Comment

by:kingcastle
Comment Utility
just need to do something similar now to the column headers and we are set.
goin try that tomorrow, if i get stuck ok to come back you?
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
Comment Utility
The chosen answer resolved the question as per the Asker's comment in Post ID: 41814036.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now