Solved

Excel Automation

Posted on 2016-09-24
17
28 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 30

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 30

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 30

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 30

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 30

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
This article will show you how to use shortcut menus in the Access run-time environment.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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