Create new column in Excel based on previous column data - sql

Just a disclaimer: I have limited experience with Excel and sql... I'm basically a noob, so bear with me.
I have a big Excel spreadsheet that is sent to me daily that I would like to manipulate.
I would like to add a couple columns that create values based on their respective rows.
ID Color Brand Indicator
1 Green Vizio TRUE
2 Yellow Samsung FALSE
3 Blue Samsung TRUE
4 Red Sony FALSE
5 Orange Vizio TRUE
In the example above, the Indicator column is the one that I'd like to be created based on the values in the previous columns. The Indicator should be true if Brand has the word Vizio in it, OR if the color is Blue. I mention it has to have the word vizio in it because there are cases where it won't be simply "vizio", but maybe "vizio tv".
I would like to automate this process as much as possible, so do you think it would be best to use an Excel VBA macro or SQL for this?
Any help would be much appreciated, thank you.

You can use the following Excel macro.
Public Sub AddIndicators()
Const INDICATOR_1_COLUMN = 4
Const INDICATOR_1_FORMULA = "=OR(IFERROR(SEARCH(""vizio"",C2),0),(B2=""blue""))"
[a2:index(a:a,counta(a:a))].Offset(, INDICATOR_1_COLUMN - 1) = INDICATOR_1_FORMULA
End Sub
This can easily be expanded for additional indicator columns.
To have this macro available to run whenever you receive your daily worksbooks (and without the need to add the macro to the incoming workbook) simply add this macro to your Personal Workbook.
After that is done, just make sure your daily workbook is open and on the sheet where you want the indicators before executing the macro.
If perchance you do not wish for the formulas to remain in the indicator column, you can use this version of the macro instead:
Public Sub AddIndicators()
Const INDICATOR_1_COLUMN = 4
Const INDICATOR_1_FORMULA = "=OR(IFERROR(SEARCH(""vizio"",C2),0),(B2=""blue""))"
With [a2:index(a:a,counta(a:a))].Offset(, INDICATOR_1_COLUMN - 1)
.Formula = INDICATOR_1_FORMULA
.Value = .Value
End With
End Sub

Your requirement is very simple. You don't have to go for VBscript, macros etc. It can be simply done with excel functions.
=OR(ISNUMBER(SEARCH("blue",B2)),(ISNUMBER(SEARCH("vizio",C2))))
Assumption: Color is in column B and Brand is in column c and data starts from row 2. First row is for headers.

Related

Programming in VBA the selection of multiple rows in a Word 2014 table

I want to change the font of all even rows in a large table in Microsoft Word (most versions, I use 2014) to red
I tried a simple loop :
For ii=2 to ActiveDocument.Tables(1).Rows.Count step 2
ActiveDocument.Tables(1).Rows(ii).Select
Selection.Font.ColorIndex = wdRed
Next
This sometimes hangs, sometimes it works, but takes hours (my table has 14000 rows...)
Then I had the idea : Manually, I can select a row by left-clicking on its left, then add additional rows to the selection by Ctrl-left-click on their left.
And I can then modify the font of all rows selected at once.
So let's see if doing the same programmatically is faster ! I tried something like
ActiveDocument.Tables(1).Rows(2).Select
For ii=4 to ActiveDocument.Tables(1).Rows.Count step 2
Selection.Add (ActiveDocument.Tables(1).Rows(ii))
Next
Selection.Font.ColorIndex = wdRed
but Add is not accepted as a valid Selection object member
Can someone help there ?
define a new style and apply it to the table ... no vba needed
this is a macro recording of an example style change ... if you wish to use vba
Selection.Tables(1).Style = "Grid Table 5 Dark - Accent 2"
also, record a macro of doing a new style definition .... lots of good stuff in it

clear cell value if condition is met vba

I am trying to use pivot table to generate charts but then I wanted to incorporate vba codes to clear values if certain string is selected, so I don't want to generate charts if certain string is selected.
Now, my vba codes uses a lot of lookup functions which I should incorporate the iferror statement back then but again I feel like its too late for me to go back and will just take more time to fix each vlookup function I used. Now, since I am only getting stuck on this portion, I am just going to post the codes for this portion.
How pivot table and charts work is that the user selects zip code and/or county and it will calculate vlaues and graph will appear so I can monitor monthly data. Each of the 3 selection button has N/A which is supposed to not return any value so chart won't graph, similar to reset button. I just tested if I select zipcode as N/A but the codes failed so I haven't expanded my test codes to county and territory. Also (All) is different from N/A as for (All), I am calculating everything, as the whole book of business.
I tried to copy and paste the pivot table as values and see if it will work, and it just won't execute, and when I pressed F8, it looks like it skips the range selection and selection.clearcontents part and jumps to the end.
Sub test()
'take out n/a
Dim find As String
find = "N/A"
Select Case find
Case Cells(2, 2).Value = find
Range("E14:R14").Select
Range("E43:R43").Select
Range("E73:R73").Select
Selection.ClearContents
End Select
End Sub

How to format/color cells based on contents of other cells

I am trying to automate an Excel workbook to help save time in making employee work schedules. I have two sheets. Sheet1 contains a simple table used to create the intended schedule for the month (employee name in first column, dates on the top row, and every day is marked with "WORKING", "ON CALL", "OFF", etc.), and the other sheet contains a table with color codes to keep track of how well the schedule was actually kept as the weeks pass. I need Sheet2 to update the color codes based on the schedule manually planned on Sheet1 so I can waste less company time manually color coding cells.
I don't want to use Conditional Formatting, since I believe you can't overwrite the format it gives a cell (in the case an employee called out rather than coming in on his scheduled day to work and on Sheet2 this needs to be recorded - with a different color code). I just need help figuring out some macro that can help speed things up and waste less company time manually color coding cells.
I don't want to record a macro this time, since names and days working can/will be different every time the schedule is updated/made.
Here is something you can work on.
Right click on the sheet 1 tab and select "View Code" copy and paste this code there.
Change a cell in Column A in sheet 1 to A,B or C then go to sheet 2 and see what happened.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
If Target = "A" Then Sheets("Sheet2").Range(Target.Address).Interior.ColorIndex = 3
If Target = "B" Then Sheets("Sheet2").Range(Target.Address).Interior.ColorIndex = 4
If Target = "C" Then Sheets("Sheet2").Range(Target.Address).Interior.ColorIndex = 5
End If
End Sub

How do I suppress Excel message for any pivot table update?

I have created a pivot table to summarize some information and have added some formulas adjacent to the pivot table to do calculations on the numbers included in it. I have created a macro that re-enters the formulas whenever the user changes the size of the pivot table (in the PivotTableUpdate event) by showing or hiding various rows/columns of data.
My problem is that whenever columns of data are added to the pivot table, it asks me "Do you want to replace the contents of the destination cells?" I always click yes, because although the cells will be overwritten when the pivot table expands, the formulas will be re-entered in their correct cell and everything is fixed and formatted properly by the macro.
Therefore, I would like to know where I should put application.displayalerts = false so that it is effective to suppress the message box whenever the user expands the pivot table.
Hi tlewis3348 i think this is what you are looking for
Sub UpdateIt()
Dim iP As Integer
Application.DisplayAlerts = False
For iP = 1 To ActiveSheet.PivotTables.Count
ActiveSheet.PivotTables(iP).RefreshTable
Next
Application.DisplayAlerts = True
End Sub
FWIW This solution worked great for me. I just activated each sheet individually and called the UpdateIt function before doing anything else, e.g.,
Worksheets("RFS Monthly Summary").Activate
UpdateIt
Worksheets("RFS Daily Activity").Activate
UpdateIt

Excel macro for automatic data entry

I need a macro to help me with data entry in Excel. Basically I want sheet 1 for data entry, and sheet 2 for the data that are entered. Sheet 1 will only have one row for data entry, and once that row is filled you hit enter. The row is automatically added to the table in sheet 2, and the row is cleared on sheet 1. Now you are ready to enter another entry in sheet 2.
So to summarize sheet 2 will have multiple rows for the data entered, and sheet 1 will only have 1 row because it automatically clears it's row after each entry.
Is something like this possible?? If you guys can post some code for me it would really help, and keep in mind I have never programmed in VBA before. Thanks in advance!!
Place a button on your sheet 1, then put this macro into a regular code module and attach it to your button. Assuming you are entering values across row2 (row1 being titles or such), then this would transfer row2 data to the next empty row on sheet2:
Sub Transfer()
Rows(2).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
Rows(2).ClearContents
End Sub
I'm not totally sure of the use-case, but have you considered using Excel UserForms instead.
The input would be a form that actually appears in front of the user which would collect input. When they hit the enter button, you could have underlying code to update the main sheet, Sheet 2.
http://www.excel-vba-easy.com/vba-userform-excel-vba.html
You could also use the database form. set up your database sheet then Data > Form (in 2003)
In order not to have to "click" on a button, I would suggest to look into this function:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
This macro will run every time something in your sheet has changed.
It might be that you have to check if there is data in cell A1, as this VBA might be triggered both after hitting enter and after the data has been transported to the other sheet.