Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

Excel Automation

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
kingcastle
Asked:
kingcastle
  • 8
  • 5
  • 4
1 Solution
 
Roy CoxGroup Finance ManagerCommented:
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
 
kingcastleAuthor Commented:
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
 
Roy CoxGroup Finance ManagerCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
kingcastleAuthor Commented:
how would i do that, sorry about all questions.
0
 
Roy CoxGroup Finance ManagerCommented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
kingcastleAuthor Commented:
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
 
kingcastleAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
Roy CoxGroup Finance ManagerCommented:
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
 
kingcastleAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
kingcastleAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
kingcastleAuthor Commented:
outstanding!
0
 
kingcastleAuthor Commented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
The chosen answer resolved the question as per the Asker's comment in Post ID: 41814036.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now