Solved

Excel Automation

Posted on 2016-09-24
17
25 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 18

Expert Comment

by:Roy_Cox
ID: 41813757
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
ID: 41813816
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 18

Expert Comment

by:Roy_Cox
ID: 41813821
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:kingcastle
ID: 41813826
how would i do that, sorry about all questions.
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41813849
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 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41813860
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
ID: 41813891
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
ID: 41813893
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
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41813901
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 18

Expert Comment

by:Roy_Cox
ID: 41813908
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
ID: 41813985
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 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41813990
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
ID: 41813992
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 29

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points (awarded by participants)
ID: 41813998
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
ID: 41814036
outstanding!
0
 

Author Comment

by:kingcastle
ID: 41814037
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 29

Expert Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

813 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

11 Experts available now in Live!

Get 1:1 Help Now