I am trying to write a VBA script on Excel 2011 For Mac and having limited success.
Depending on the value in cell A1, the script needs to unhide the rows below.
If A1 = 1, it needs to unhide row B.
If A1 = 2, it needs to unhide rows B and C.
If A1 = 3, it needs to unhide rows B, C and D.
...and so on, up to a maximum A1 value of 8.
The values in A1 use data validation to be looked up from a list elsewhere on the sheet.
Thank you!
If you say B, C, D, it seems you mean Columns, not Rows.
You can use this:
Range("B1").Resize(1, Range("A1")).EntireColumn.Hidden = False
Select Case Range("A1").Value
Case 1
Range("A2").EntireRow.hidden = false
Case 2
Range("A2,A3").EntireRow.hidden = false
'...
Case Else
MsgBox("Invalid number in cell A1")
End Select
in this case A2 would refer to row 2, A2,A3 would be 2 and 3 etc etc
EDIT:
per your comment maybe something like this would be better
Dim rng as Range
Dim val as Integer
val = Range("AE25").Value
if (val >= 1) then
set rng = Range("A26:A27").Resize(val,0)
rng.EntireRow.Hidden = false
end if
Related
So I'm pretty new to excel formulas and got almost no experience with VBA. But I've come across with a problem that I need to solve.
So the scenario goes like this.
I've got two workbooks and I need to retrieve data to one workbook from another if the condition for a cell value is met. Let me explain with an example.
(C for columns, R for rows, x for random numbers)
I've got Workbook A as shown below:
And Workbook B with the same structure
So what I'm trying to achieve here is:
When I change/insert values in Workbook A, C3Rx there will be a conditional mechanism that will check for the value.
Let's say if C3R1's value is "1" on Workbook A, it should fill C1R1, C2R1 and C3R1 on Workbook B accordingly.
If the value is not "1", it just should keep scanning the Workbook A, C3 and when it meets the conditional requirement (C3Rx having the value of "1"), it should write it in and go to the next row (C1R(x+1)). Follow the procedure again and again. Scanning all values in Workbook A.
I've tried to make it work using VLOOKUP and some other functions together but it doesn't suit really well with my case. It works with spaces when the value does not meet the condition and also, I need to fill all the cells on C1 with the formula till the end. (considering I don't know how long it may go, that's not really a solution for me)
I think it's achievable with Macros but like I've said, I don't have much experience with VBA.
Thanks for your help in advance.
Have a good one.
I'm not exactly get what you mean.
Anyway below I am guessing on what you mean.
Below is what contains in Workbook-A sheet1 column A to C
Below is what contains in Workbook-B sheet1 column A to C
With the first condition that Workbook-A and Workbook-B are arlready open.... below is Workbook-A sheet1 where cell C4 and C7 fill with 1 value,
and after the button is clicked :
1. Cell A4 to C4 value in Workbook-A Sheet1 become the value of cell A4 to C4 value in Workbook-B Sheet1.
2. Cell A7 to C7 value in Workbook-A Sheet1 become the value of cell A7 to C7 value in Workbook-B Sheet1
Button1 is assign to a macro like this :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
r = cell.Row
wbA.Range("A" & r, "C" & r).Value = wbB.Range("A" & r, "C" & r).Value
'wbA.Range("A" & r, "C" & r).Interior.Color = vbRed
End If
Next
End Sub
The code will look to each value in column C (starts from row 2) in Workbook-A sheet1.
If the code find the value is 1 in row X of column C, then it copy row X of column A to C in Workbook-B sheet1.
That's if I'm not mistaken on what you mean.
Assumed that both workbooks already open.
Below is the beginning look of wb-A right after it's open :
Below is the beginning look of wb-B right after it's open :
Back to wb-A, after you examined the data, you decided that id-003 branch location is not London, but Madrid. So you type number 1 in the third column id-003 row. WB-A now look like this :
And what you expect is, if the code find any row in the third column of wb-A with value "1", then the code have to copy all the three columns of id-003 row then paste it to the last blank row in the emp_id column of wb-B. So, wb-B look like this :
Here is the code which has to be in wb-A module :
Sub test()
Set wbA = Workbooks("Test-A.xlsm").Sheets("Sheet1")
Set wbB = Workbooks("Test-B.xlsm").Sheets("Sheet1")
Set Rng = wbA.Range("C2", Range("C2").End(xlDown))
For Each cell In Rng
If cell.Value = 1 Then
Range(cell, cell.Offset(0, -2)).Copy Destination:= _
wbB.Range("A1000000").End(xlUp).Offset(1, 0)
End If
Next
End Sub
Again, above is just my guessing because I'm still not clear what you want and how is the situation.
I have data in excel file for which filter has to be applied for each column independently but the filter condition is same. The reason for asking this is each column has that cell that meets the condition in a different row number.In table 1 I have 3 columns a,b and c.
I want to filter each columns independently with value=20 so that the result looks like table below
Try out this VBA code,
Sub matchvalues()
Dim i As Long, j As Long
Sheets.Add.Name = "newSheet"
j = InputBox("Enter the value to filter")
Rows("1:1").Copy Sheets("newSheet").Cells(1, 1)
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
If IsError(Application.Match(j, Columns(i), 0)) Then
Sheets("newSheet").Cells(2, i) = ""
Else
Sheets("newSheet").Cells(2, i) = j
End If
Next i
End Sub
This code will prompt the value that has to be filtered. Need to give that as input which will create a new sheet and output the values if present.
If you want to do this with just formulas, try the below. If the value that you are searching is in cell E1, enter the below formula in cell G2 and drag across.
=IF(ISNUMBER(MATCH($E$1,A:A,0)),$E$1,"")
You can change the values in E1 directly to see the updated result. Hope this helps.
I am trying to set conditional formatting in 18 cells in third column ("C"). I have merged each 6 cells in first column ("A"), and unmerged (normal) cells in second column ("B"). I am trying to check for each next cell in row of column "C" if there is a "yes" in first row of column "A" or whether there is a "no" in "A" column and "pass" in "B" column. The trick is, I want to check only first row of "A" column, seventh, thirteenth and nineteenth (so with the step = 6) and check every row in "B" column. I try something like this:
Sub try()
Dim i As Integer
Dim j As Integer
i = 1
For j = 1 To 12
With Range("C1:C18")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(Cells(i, 1) = ""Yes""; AND(Cells(i, 1) = ""No""; Cells(j, 2) = ""Pass""))"
End With
If j Mod 6 = 0 Then
i = i + 6
Next j
End Sub
But it does not work like that, I saw examples with specific Cells like "A1" or "A3" but I want a number to increase with every loop (so I tried it with Cells(row,column)).
You can do it in one statement on the whole range by using relative addresses, so what applies to C1 relatively to A1 and B1 will follow automatically in the subsequent rows of the range.
The only trick is to retrieve the value in column A, since this value is only available in cells A1, A7, etc. This is achieved by the expression OFFSET(A1,-MOD(ROW(C1)-1,6),0).
Sub doIt()
With Sheet1.Range("C1:C30").FormatConditions
.Delete
.Add(xlExpression, , _
"=OR(OFFSET(A1,-MOD(ROW(C1)-1,6),0)=""yes"",AND(OFFSET(A1,-MOD(ROW(A1)-1,6),0)=""no"",B1=""pass""))") _
.Interior.ColorIndex = 6
End With
End Sub
You can also do it from the GUI using the same formula; select cell C1 then select the whole range C1:C30, and click
Conditional Fomatting -> New rule -> Use a formula... and enter the same formula.
BTW, the expression can be further simplified if you dont care to check for "no", meaning if column A is assured to be either "yes" or "no".
Based on the value stored in a specific cell such as A1 of a worksheet I would like to hide a number of columns in the worksheet starting with column B.
Examples of what I am trying to do:
If the value of cell A1 = 10, then hide column B plus 10 columns after B
If the value of cell A2 = 11, then hide column B plus 11 columns after B
The difficulty is actually the way Excel (or least my Excel files) uses the alphabet (A, B, ...) for the name of the columns. I have done this on rows before using code like rows("2:" & range("A1").value) and set .hide = true
I wanted to add a comment to Glenn's answer above but don't have enough reputation. What I was going to add was that you don't need to activate a sheet or select the columns, you can simply go ahead and hide the columns:
Worksheets("TheSheet").Columns(2).Resize(, numColumnsToHide).EntireColumn.Hidden = True
You can reference columns by their index number as such: Columns(indexnumber) and you can use Resize() to set the number of columns you want to select like so:
Sub HideColumns()
Dim numColumnsToHide
numColumnsToHide = Cells(1, 1).Value
Columns(2).Resize(, numColumnsToHide).Select
Selection.EntireColumn.Hidden = True
End Sub
Obviously, this code doesn't have any validation of value in A1 so if someone runs HideColumns() without an integer in A1, bad things are going to happen. This also doesn't unhide any hidden columns.
Please to not complicate just do it
Sub ocultaColunas()
Range("D:E").EntireColumn.Hidden = True
End Sub
I have an excel file with 10,000 rows in column A some values are the same.
Example:
A1 - P7767
A2 - P3443
A3 - P7767
A4 - P8746
A5 - P9435
etc...
I then have another column with 100 rows which have some of the values found in column A,
B1 - P7767
B2 - P8746
etc...
I need to highlight all cells in column A where the value is found in any of the values in column B
So basically column B checks to see if it can find the same value anywhere in column A, if true highlight the cell leaving any cells white when the value is not found in column B
I hope I have explained this well, I have done some research and I believe I need to use conditional formatting to get this result but I am really stuck on the formula to use and cannot seem to find an example online (Maybe I am not searching the correct term as I'm not sure on what this is exactly called)
There may be a simpler option, but you can use VLOOKUP to check if a value appears in a list (and VLOOKUP is a powerful formula to get to grips with anyway).
So for A1, you can set a conditional format using the following formula:
=NOT(ISNA(VLOOKUP(A1,$B:$B,1,FALSE)))
Copy and Paste Special > Formats to copy that conditional format to the other cells in column A.
What the above formula is doing:
VLOOKUP is looking up the value of Cell A1 (first parameter) against the whole of column B ($B:$B), in the first column (that's the 3rd parameter, redundant here, but typically VLOOKUP looks up a table rather than a column). The last parameter, FALSE, specifies that the match must be exact rather than just the closest match.
VLOOKUP will return #ISNA if no match is found, so the NOT(ISNA(...)) returns true for all cells which have a match in column B.
A simple formula to use is
=COUNTIF($B:$B,A1)
Formula specified is for cell A1. Simply copy and paste special - format to the whole of column A
NOTE: You may want to remove duplicate items (eg duplicate entries in the same column) before doing these steps to prevent false positives.
Select both columns
click Conditional Formatting
click Highlight Cells Rules
click Duplicate Values (the defaults should be OK)
Duplicates are now highlighted in red:
The easiest way to do it, at least for me, is:
Conditional format-> Add new rule->Set your own formula:
=ISNA(MATCH(A2;$B:$B;0))
Where A2 is the first element in column A to be compared and B is the column where A's element will be searched.
Once you have set the formula and picked the format, apply this rule to all elements in the column.
Hope this helps
A1 --> conditional formatting --> cell value is B1 --> format: whatever you want
hope that helps
Suppose you want to compare a column A and column H in a same spreadsheet .
You need to go another column next to these 2 columns and paste this formula :
=(Sheet1!A:A=Sheet1!H:H)
this will display FALSE or TRUE in the column . So you can use this new column to color the non matching values using conditional color formatting feature .
I was trying to compare A-B columns and highlight equal text, but usinng the obove fomrulas some text did not match at all. So I used form (VBA macro to compare two columns and color highlight cell differences) codes and I modified few things to adapt it to my application and find any desired column (just by clicking it). In my case, I use large and different numbers of rows on each column. Hope this helps:
Sub ABTextCompare()
Dim Report As Worksheet
Dim i, j, colNum, vMatch As Integer
Dim lastRowA, lastRowB, lastRow, lastColumn As Integer
Dim ColumnUsage As String
Dim colA, colB, colC As String
Dim A, B, C As Variant
Set Report = Excel.ActiveSheet
vMatch = 1
'Select A and B Columns to compare
On Error Resume Next
Set A = Application.InputBox(Prompt:="Select column to compare", Title:="Column A", Type:=8)
If A Is Nothing Then Exit Sub
colA = Split(A(1).Address(1, 0), "$")(0)
Set B = Application.InputBox(Prompt:="Select column being searched", Title:="Column B", Type:=8)
If A Is Nothing Then Exit Sub
colB = Split(B(1).Address(1, 0), "$")(0)
'Select Column to show results
Set C = Application.InputBox("Select column to show results", "Results", Type:=8)
If C Is Nothing Then Exit Sub
colC = Split(C(1).Address(1, 0), "$")(0)
'Get Last Row
lastRowA = Report.Cells.Find("", Range(colA & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column A
lastRowB = Report.Cells.Find("", Range(colB & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column B
Application.ScreenUpdating = False
'***************************************************
For i = 2 To lastRowA
For j = 2 To lastRowB
If Report.Cells(i, A.Column).Value <> "" Then
If InStr(1, Report.Cells(j, B.Column).Value, Report.Cells(i, A.Column).Value, vbTextCompare) > 0 Then
vMatch = vMatch + 1
Report.Cells(i, A.Column).Interior.ColorIndex = 35 'Light green background
Range(colC & 1).Value = "Items Found"
Report.Cells(i, A.Column).Copy Destination:=Range(colC & vMatch)
Exit For
Else
'Do Nothing
End If
End If
Next j
Next i
If vMatch = 1 Then
MsgBox Prompt:="No Itmes Found", Buttons:=vbInformation
End If
'***************************************************
Application.ScreenUpdating = True
End Sub
Don't wana do soo much work guyss..
Just Press Ctr and select Colum one and Press Ctr and select colum two.
Then click conditional formatting -> Highlight Cell Rules -> Equel To.
and thats it. your done. :)