changes of colums randomly of excel file randomly using libra office - excel

I have an excel file consist of 1000x3 words. I wrote 5 rows here as an example.
Target choice1 choice2 choice3
booklet criquet nutmeg luciole
argoman border phobie liom
mouche libellule taplet luciole
abrot guêpe terrier greeca
However I know currently all the words in choice2 are correct answer, I want to randomly mix the place of choice 1 and choice 2 and choice 3 using libra office such as below:
Target choice1 choice2 choice3
booklet nutmeg criquet luciole
argoman phobie liom border
mouche taplet libellule luciole
abrot terrier guêpe greeca
I am new to excel and I don't know how to work with it. Please advice me how to do it and in advance I appreciate all the comments.

Sorry for the late reply.
You can use something like this:
Sub MixTheValues()
Dim x, y, r
Dim Words As New Collection
For x = 2 To Cells(Rows.CountLarge, "B").End(xlUp).Row
For y = 2 To 4
Words.Add (Cells(x, y).Value)
Next y
For y = 2 To 4
r = Int(Words.Count * Rnd + 1)
Cells(x, y).Value = Words(r)
Words.Remove (r)
Next y
Next x
End Sub
x starts on row 2 and goes to the last row.
y defines the columns you want to use (in this case, 2 to 4 or B to D
Note that this shuffles the answers, but because it is completely random, some of them may not move at all.
Results:

Related

How do I check if a cell is blank in VBA

I am trying to go through an array and any element with a value I want to print in a list going down. However, I am having problems with identifying blank cells. Right now it recognizes every cell as not be empty. I am also including a screenshot of the data to help. (Some of the columns are hidden in the screen shot)
I have tried:
Evaluate("isblank(" & Cells(i, 8).Address & ")")
IsEmpty(list(i, 1))
IsNull(list(i, 1))
None of the options above work and this is what I currently have
Dim list As Variant
Dim i As Integer
Dim Y As String
Dim X As Integer
Y = "A"
X = 2
list = Range("H12:H158").Value
For i = 1 To 147
If Not IsEmpty(list(i, 1)) Then
Cells(X, Y) = list(i, 1)
X = X + 1
End If
Next i
Any advice is greatly appreciated
I have just tried out your macro and as a result, in the column "A", starting from row 2, I have the values:
HRT-2-11
HRT-2-13
HRT-2-14
HRT-2-15
HRT-2-17
HRT-2-18
This, I believe, is what you are looking for, isn't it? I believe that there has been a value in the cells which appear blank, but now there should be some invisible character (like a space or something). I'd advise you to check the value of such an "empty" cell and go on from there (the "immediate" window is perfect for investigations like this).
Len() never missed a char.
If Len(list(i, 1))>0 Then
....

Removing duplicate lines of text but have exceptions - Excel

I have some lines of text that look as below. As you can see there is a duplicate of "x1". What I want to do in Excel is remove duplicates (eg. x1) but keep the word "Title" as it counts as a duplicate and it will be removed which is something I don't want.
Title
x1
Title
x2
Title
x3
Title
x1
Title
x4
I searched all over the internet about methods some of them including "data validation" or even formulas like the one below but none of these worked. So do you have any ideas on how can this be done?
=IFERROR(LOOKUP(2, 1/((COUNTIF($A$2:$A$20, $A$2:$A$20)>1)*(COUNTIF($E$1:E1, $A$2:$A$20)=0)*(COUNTIF($C$2:$C$3, $A$2:$A$20)<>1)), $A$2:$A$20), "")
There are a few ways to deal with this I suppose but if the "Title" is always the every other row, you could simply use a do loop with a "+ 2" to determine the dups. So for example, let's assume all this data is in column A starting in row 1:
x = 2
Range("C1").Formula = "=Count(A:A)"
y = range("C1")
Cells(x,2).Formula = "=Countifs(A:A, A1)"
Range(Cells(x,2)).Copy
Range(Cells(x + 1, 2), cells(y, 2)).PasteSpecial xlPasteFormulas
Do While x <= y
If Cells(x, 2) > 1 Then
Rows(x).Delete
x = x + 1 'Add 1 since you already deleted 1 row and only need to move to the next row at this point.
Else
x = x + 2 'Add 2 to ensure you don't delete the title row
End If
Loop
I realized after searching a little further that there is no easy way to do this in Excel. And in some cases it is impossible. So I found a workaround with Sublime text which is much easier to do this.
Copy-paste the content from column A to Sublime text.
Then select the repeated content that you don't want to be removed and then ALT + F3. This will select all instances of that content.(HINT: Add one "space" before that text to avoid line merging when proceeding to deleting duplicates.
Inverse the selection to now select all the rest text/content.
Then go Edit > Permute Lines > Unique....
5 . DONE!!

Excel VB: how do I call VLOOKUP if condition is met?

I've got a data set that comes out of Oracle, but a number of the records are missing the GEO data (AMER, EMEA, APAC) in column E and instead show "-". I am formatting and processing this data using VB and I have everything working save for this one piece.
What I want to do is to call a VLOOKUP formula to replace those "-" values with the correct GEO based on the country name shown in column J. All records with GEOs already present should be bypassed. The code I built for this overwrites every record, unfortunately:
For z = 1 To LR
x = Application.VLookup(Cells(z, "J"), Sheets("MasterTerritoryList").Columns("A:F"), 6, False)
If Not (IsError(x)) Then
Cells(z, "E").Value = x
End If
Next z
Can someone help me out by showing me how to read the data in column E and replace only those values that equal "-"?
Many thanks!!
For z = 1 To LR
If Cells(z, "E").text = "-" Then ' <-------- Add this test
X = Application.VLookup(Cells(z, "J"), Sheets("MasterTerritoryList").Columns("A:F"), 6, False)
If Not (IsError(X)) Then Cells(z, "E").Value = X
End If
Next z

Set predefined values in a column when an item is selected from a list

I am an amateur Android developer and I don't have any knowledge in VBA. If you have some good learning resources please post them too.
The scenario is like this :
I have 2 columns the first will be populated in list and the second will be the value i want to set to a cell in the same row in a different column
Example
Names | grade
Kim | 45
Joe | 90
If I selected "Kim", 45 will be the value of the next column's cell in the same row as the list
From what I remember Android apps are programmed in Java unless you run the NDK (which is too confusing for a beginner to mess with). Since you asked for the solution in Excel-VBA here it is:
Dim x As Integer
Dim y As Integer
For x = 1 To 6
If Range("A" + x).Value == "Kim" Then
y = Range("B" + x).Value
End If
Next x
I don't code in Visual Basic but this is the best from what I can remember of it.

Get maximum value of columns in Excel with macro

First of all I have no idea of writing macros in excel, but now I have to write a code for a friend. So here we go.
In my excel sheet I have a table which holds some producers as columns and 12 months of the year as rows. In their intersecting cell it's written the amount of products produced by the producer during that month. Now I need to find maximum and minimum values of produced goods within each month and output the producers of that goods.
I found a code for a similar problem, but I don't understand it clearly and it has errors.
Here is the code:
Sub my()
Dim Rng As Range, Dn As Range, Mx As Double, Col As String
Set Rng = Range(Range("A1"), Range("A6").End(xlUp))
ReDim ray(1 To Rng.Count)
For Each Dn In Rng
Mx = Application.Max(Dn)
Select Case Mx
Case Is = Dn.Offset(, 0): Col = "A"
Case Is = Dn.Offset(, 1): Col = "B"
Case Is = Dn.Offset(, 2): Col = "C"
Case Is = Dn.Offset(, 3): Col = "D"
End Select
ray(Dn.Row - 1) = Col
Next Dn
Sheets("Sheet2").Range("A2").Resize(Rng.Count) = Application.Transpose(ray)
End Sub
I get the following error:
Run-time error'9': Subscript out of range.
So my question is, what does this error mean and what do I need to change in this code to work?
EDIT1:
OK, now the error is gone. But where do I get the results?
EDIT2
I know this line is responsible for inserting the results in specified place, but I cant see them after execution. What's wrong with that?
Error means the array you are trying to access has not been defined to contain the ordinal you're looking for: For example Array 10 has positions 0-9 so if I try and access array(10) it would throw that error or array(-1) it would throw that error.
I can't remember if excel is 0 or 1 based arrays.
Possibly change
ray(Dn.Row - 1) = Col
to
if dn.row-1<> 0 then ' or set it to <0 if zero based.
ray(Dn.Row - 1) = Col
end if
You don't need VBA (a macro) to do this. It can be done using a worksheet formula.
E.g.
If your producers are P1,P2,P3,P4 and your sheet looks like this:-
A B C D E F
+-------------------------------------------
1 | Month P1 P2 P3 P4 Top Producer
2 | Jan 5 4 3 2
3 | Feb 2 3 5 1
4 | Mar 6 4 4 3
...
...
The following formula placed in cells F2,F3,F4,... will pick out the top producer in each month.
=INDEX($B$1:$E$1,MATCH(MAX(B2:E2),B2:E2,0))
Generally it's better to try and use built in Excel functionality where possible. Resort to VBA only if you really need to. Even if you were to use the top producer/month data for some other operation which is only possible in VBA, at least the top producer/month data derivation is done for you by the worksheet, which will simplify the VBA required for the whole process.
Transposing a range can also be done using a worksheet formula by using the TRANSPOSE() function.
BTW - I'm not sure what you want to do if two producers have the same output value. In the VBA example in your question, the logic seems to be:- if two producers are joint top in a month, pick the first one encountered. The formula I've given above should replicate this logic.
I have used these functions quite extensively and they are very reliable and fast:
Public Function CountRows(ByRef r As Range) As Integer
CountRows = r.Worksheet.Range(r, r.End(xlDown)).Rows.Count
End Function
Public Function CountColumns(ByRef r As Range) As Integer
CountColumns = r.Worksheet.Range(r.End(xlToRight), r).Columns.Count
End Function
Give it a reference (ex. "A2") and it will return the filled cells down, or the the right until and empty cell is found.
To select multiple sells I usually do something like
Set r = Range("A2")
N = CountRows(r)
Set r = r.Resize(N,1)

Resources