I have been searching, and the Offset only can be fixed (http://www.homeandlearn.org/the_offset_property.html)?
I would like to know how to do a variable offset. For example, I choose a fixed value from worksheet (1), based on the condition that you can see below it will do the offset, and use the offset value in a formula from code of worksheet (2).
Worksheet 1: I have the columns A to H and row 1
A B C D E F G H
1 5 10 15 20
Worksheet 2: I have column B and rows 14 to 18. In this worksheet, the values 2,3,4,5,6 are random and are used to determine the number of columns to offset in the worksheet 1. As follows, I expressed what I am trying to do, may be its easier to understand.
Row B
14 2 Sheets(1).Offset(A1,0,($B2-$B$2)*2,1,1)= Offset(5,0,(2-2)*2,1,1) =5 (A1)
15 3 Sheets(1).Offset(A1,0,($B3-$B$2)*2,1,1)= Offset(5,0,(3-2)*2,1,1) =10 (C1)
16 4 Sheets(1).Offset(A1,0,($B4-$B$2)*2,1,1)= Offset(5,0,(4-2)*2,1,1) =15 (E1)
17 5 Sheets(1).Offset(A1,0,($B5-$B$2)*2,1,1)= Offset(5,0,(5-2)*2,1,1) =20 (G1)
18 6 Sheets(1).Offset(A1,0,($B6-$B$2)*2,1,1)= Offset(5,0,(6-2)*2,1,1) =0 (I1)
During my search, I only found possible to do an offset by placing numbers of rows and columns desired to offset. However, it does not work for me.
Since the following code, returns time error '1004, I would like to know why? Is not supposed to return a value? If everything is defined, why does the offset not working? What's wrong?
Sheets(1).Cells(1,1).Offset(0,(Cells(i+countRows,3) - Cells(i,3))*2))
How can I solve this error?
I hope I made myself clear.
Thanks in advance!
Related
Select a Range
Iterate rows of selection. For each iteration, if value of first column of selection equals x, make current row colored red, else make current row colored green?
Not exactly what you are asking, but sounds to me like you could use conditional formatting for that. If needed, you could use an extra column where the formula would return True if the row would need to be red, False otherwise. Also the equation would need to know where the ranges are, so maybe an extra column for that.
So a not so genius halfworking example:
A B C D
1 q 0 0 0
2 i 1 2 0
3 i 0 2 1
4 d 0 2 0
https://help.libreoffice.org/Calc/Conditional_Formatting
B would hold 1 for range start, 0 otherwise.
C2: =IF(B2=1;ROW();B1)
D2: =IF(A2=OFFSET($B$1;0;D2-1);1;0)
And conditional formatting should apply red if D1 was 1.
Conditional Formatting Help
I am running into a problem in VBA in excel.
I am trying to create a participant registration program in excel for a sports tournament. One can add data like the weight, age and name of a participant. And then based on that, The participants are divided into poules named with letters A, B... until Z. I have a table which can sort these poules by letters and then for example only display the participants which are in poule A such as below
Example
Now i want to count the numbers with VBA macros of participants in the poule which is displayed by the sorted table. For example when i sort on poule A it has to return 3 and when i sort on B, it has to return 2.
Determined by the number of participants in a poule the program will print a different poule scheme, depending on the number of participants. (i have this part in VBA)
The problem is when i have a sorted table like below
Example 2
It counts all the participants, and not just the ones in B or any other sorted letter.
I have this:
Sub Count()
Dim nRows As Integer
nRows = Range(Range("A18"), Range("A18").End(xlDown)).Rows.Count
MsgBox (nRows)
End Sub
This works well if you sort A, but when you sort any other letter, it counts All the table until that letter. Eg. when you sort B, it displays 5 (Number of A + B).
I have looked on the internet for a really long time to find a solution but without succes.
I hope my question is clear and that somebody can help me.
I am assuming that you are using worksheet functions. Use Subtotal when working with filtered data.
These parameters evaluate all cells visible or hidden
1 AVERAGE
2 COUNT
3 COUNTA
4 MAX
5 MIN
6 PRODUCT
7 STDEV
8 STDEVP
9 SUM
10 VAR
11 VARP
These parameters evaluate only visible cells
101 AVERAGE
102 COUNT
103 COUNTA
104 MAX
105 MIN
106 PRODUCT
107 STDEV
108 STDEVP
109 SUM
110 VAR
111 VARP
The code does work now, except that it only counts the first letters it encounters.
So when the first column for the poules is for example A A A E A A B B E.
And i sort to A and use the count function, it only returns a value of 3 and not of 5 (because there are 5 A's)
When I sort the table to A, it looks like this (column number, poule value):
14 A
15 A
16 A
18 A
19 A
And it returns just a count of 3, have you maybe got any fixes for that problem as well?
Pictures:
sorted tabel to E
Table
Range.SpecialCells will return a range of only visible cells.
Dim rSource As Range
Dim rVisibleCells
Set rSource = Range(Range("A2"), Range("A2").End(xlDown))
Set rVisibleCells = rSource.SpecialCells(xlCellTypeVisible)
MsgBox rVisibleCells.Rows.Count
I'm a beginner to vba and hope to use it in order to automate a process. I have a computer program that provides me an excel spreadsheet, where the amount of data in the columns can change. For example, I could have a column of length 9 one time:
1
3
2
4
24
23
432
55
2
Or a column of length 5 (note: actual column sizes are usually in the thousands):
1
2
3
4
8
I want to write code that will grab this column, not knowing how long the column will be. Something like:
Dim list1 As Array
'I don't know how to find the upper bound
list1 = Range("A1:A" & Upper Bound).Value
Any help is appreciated. Thanks!
If you are working with constant values in these columns and are working with larger amounts of cells in these columns, it could be useful to use the count function:
dim x As Long
x = Worksheets(*sheet name*).Range(*column range*).Cells.SpecialCells(xlCellTypeConstants).Count
x is being passed the number of non-empty cells in your column. So you may want to be careful in your column range not to include any headers, if applicable.
I believe this will get you the last row. You might need to be careful if you're looking to count all values (e.g. the first couple rows are blank).
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
To get the data from column A into a 2d variant array, use the following:
Dim vA As Variant
vA = [a1:index(a:a,counta(a:a))]
This assumes that there are no blank cells within the data, and that the data starts in row 1.
I have two columns A and B. Range of A is A1:A6 and B is B1:B6. Now, I want to multiply these two columns and display result in column C using excel VBA. For example
A B C
2 3 6 (It is A1*B1)
3 8 24
9 2 18
7 3 21
2 4 8
5 4 20
I have tried this code but did not find it helpful:
Range("C1:C6").Value = Range("A1:A6") * Range("B1:B6")
Please guide me here as I am new to VBA. You help is really appreciated.
Alternatively,
for i = 1 to 6
cells(i,3) = cells(i,1) * cells(i,2)
next i
Try this. I have commented the code so that you will not have a problem understanding it. But if you still do then simply ask :)
'~~> This will enter the formula in the entire range in one go
Range("C1:C6").Formula = "=A1*B1"
'~~> This will convert the formula to values
Range("C1:C6").Value = Range("C1:C6").Value
So I have a chart that looks something like this. Assume that the top left value, 1, is in cell A1:
x= 1 2 3 4 5 6 7 8
4 3 2 1 2 3 4 5
9 8 7 6 7 8 9 10
8 7 6 5 4 3 2 1
Sum= 21 18 15 12 13 14 15 16
There are x values from 1 to 8, and a three columns of values resulting from using it an equation or something below it. The sum is the sum of the three values below their corresponding x-value.
I'm stuck trying to figure something out that will go through the row of sums, find the smallest value, and then assign it's corresponding x-value to a variable. I also need to assign the values to the left and right of that x-value to other variables.
For this particular chart, 12 is the smallest of the sums, so I would assign variable1 = 4, since that is that column's corresponding x-value. Then my second variable, which is called lowerbound, would equal 3, since it is to the left of x = 4, and my third variable, which is called upperbound, would equal 5, since it is to the right of x = 4.
If I could get the cell address returned of the x-value that corresponds to the smallest sum, then I could assign it to a variable, and then simply offset from that cell to assign the other variables. Even if I could make a program that will return me the cell of the minimum sum value, I could offset to the x-row, and go from there.
How would I do something like that?
TL:DR: To ask more clearly, since that's a lot of words: What would a program look like that detects the smallest value in the sum row, and returns the cell address of that value?
The length of the rows are an unknown, and vary a lot, but the length of the columns are given. They do change depending on the problem, but they will always be known. So I will always know how many rows are in a column, but I will not know how many columns are in a row.
This is the most confusingly-worded thing I've ever written in my entire life, but I hope I've explained it well enough to make some sense.
You guys really are amazing, by the way. I've gotten so far on this program, and it's all because of how helpful you are. I honestly think I would still be stuck at the beginning with you guys! You're willing to tolerate a newbie's incessant questions.
I am assuming that the sum is in A4:H4. Please change as applicable
You can use a formula like
=CELL("address",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))
If you want to use VBA then you can use this
Sub Sample()
MsgBox Application.Evaluate("=CELL(""address"",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))")
End Sub
Using your example, the following formula returns the cell address in row 1 whose value in row 5 is the lowest:
=ADDRESS(1,MATCH(MIN(A5:H5),A5:H5,0))
And if you want that cell's value, use INDIRECT. It takes the address as a string.
=INDIRECT(ADDRESS(1,MATCH(MIN(A5:H5),A5:H5,0)))
If you sum the columns by taking the sum of the array. Here is the VBA version:
For j = 1 To 8
For i = 1 To 3
sum(j) = sum(j) + Cells(i + 1, j + 1)
Next i
Cells(5, j + 1) = sum(j)
Next j