Solved

Excel Automation

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

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
 

Author Comment

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

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 28

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

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 17

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 28

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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

896 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

16 Experts available now in Live!

Get 1:1 Help Now