I have a spreadsheet named "Copy" sorted by numeric values (lowest to highest) in column H. The sheet has headers in row 1. The lowest value in column H is 0. I would like to find the last row that has column H equal 0 and then select the range of rows where column H equals 0 along with the header row.
Thanks!
This will do the job
Sub CustomSelect()
Dim i As Long: i = 2
Do While Range("H" & i).Value = 0
i = i + 1
Loop
Range("H1:H" & i - 1).EntireRow.Select
End Sub
Related
My data set column is H through BQ with 3000 rows.
I have column H with values in some rows and no value in others. First, I am trying to find the row with value. Once I do that I want to divide that value by the corresponding value in column I to get the answer X. Finally, I then want to repeat the value in column I – X times at the end of that row which has any value
For example:
Row Number Column H Column I Column J Column K Column L
1
2 1400 200 300 200 200
3
4 2000 1000
5 400
Expected results:
Row 2:
1400/200 = 7. I want to repeat the value in Column I(200) 7 times after Column L
Row 4:,
2000/1000 = 2. I want to repeat the value in Column I (1000) 2 times starting in Column J
Row 5: 400/0 = Error. In this case repeat the value in Column H (400) once in Column I.
This should meet your requirements:
Sub DoThisThing()
Dim rCell As Range, OrCell As Range, startCol As Integer, dividingIntger As Integer, _
answer As Double, searchRng As Range, WS As Worksheet
Set WS = Sheets(ActiveSheet.Name) 'Or use actual sheet name i.e. Set WS = Sheets("Sheet1")
Set searchRng = WS.Range("H1:H3000") 'sets the range to search through.
For Each rCell In searchRng.Cells
'If Column H is a numeric value, but not empty
If IsNumeric(rCell) And Not IsEmpty(rCell) Then
'Establishes offset cell being used
Set OrCell = rCell.Offset(0, 1)
'If column I has a numeric value that's not zero or blank
If IsNumeric(OrCell) And OrCell.Value2 <> 0 Then
'Number of columns to offset (rounded up)
dividingIntger = Application.WorksheetFunction.RoundUp(rCell.Value2 / OrCell.Value2, 0)
'Exact answer (decimals okay)
answer = rCell.Value2 / dividingIntger
'Finds which column to start entering values
startCol = WS.Cells(rCell.Row, Columns.Count).End(xlToLeft).Column + 1
'sets answer value into the starting column and over (less 1)
Range(WS.Cells(rCell.Row, startCol), WS.Cells(rCell.Row, startCol).Offset(0, dividingIntger - 1)).Value2 = answer
'If column I is zero or blank
ElseIf IsNumeric(OrCell) Then
'Sets Column I = to Column H
OrCell.Value2 = rCell.Value2
End If
End If
Next rCell
End Sub
'☠☠☠☠system tested!👍👍☄⚡✨💥
I have been trying to figure this problem out for a while but I cannot think of an answer. Some context here: I have two worksheets. In sheet one, I have a column that is full of names. In the second sheet, I have an outline that has 10 rows that display the names. However, if in sheet one there are 11 names, the 11th name would not appear in sheet two because there are only 10 rows. What I need to figure out is how to insert a row for the 11th names (or nth depending on how many names are added). Once I add the nth row, I would auto fill the formula down which is the easy part. The part I cannot figure out is how to insert the row.
What I was thinking was inserting a COUNT function that would count the different names and insert the rows that way. But is there a way I could do it so there would not be a random number (the number being the COUNT) in a cell in one of my worksheets?
From my understanding, you want to insert n-10 number of rows with n being total number of rows of names.
Try this
Sub test()
Dim startr, endr As Range
Set startr = Range("A7") 'Set this to be the first row of your list of names
If Not startr.Value = "" Then
With Sheets("Sheet1")
Do Until startr.Offset(i).Value = ""
i = i + 1
Loop
End With
Set endr = startr.Offset(i - 1) 'endr is now the last row of the names
totalr = Range(startr, endr).Rows.Count 'totalr is the total number of rows/names
If totalr > 10 Then
addr = totalr - 10 'addr is the number of rows you need to add
MsgBox "You need to insert " & addr & " more rows."
'The following finds the last row of column A and inserts addr number of rows
Dim lastr As Range
Set lastr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)
lastr.Offset(1).EntireRow.Resize(addr).Insert
Else
MsgBox "Number of rows/names is " & totalr & " which is less than 10, so no rows are being added."
End If
Else
MsgBox startr.Address(0, 0) & " is empty."
End If
End Sub
In Column W I have BALANCE2 values &
In Column X I have BALANCE3 values
I need to move the values from BALANCE3 (which are greater than zero) to overwrite the corresponding cell in BALANCE2 so that I have one total list of payments.
Can anyone help to work with this one? I don't know how to do it.
Do not use a macro for this
Just use excel's built in IF, for example, assuming BALANCE2 is column W and BALANCE3 is column X, write in cell Z1
=IF(X1>0,X1,W1)
And then drag it down to the entire column, cut and paste column Z into column W, and you got your "overwrite" without writing macro code
Try this one:
Public Sub overwriteValues()
Dim row As Integer
'Set start row
row = 1
With Sheets("sheetname")
'Loop until column X cell is blank
'You can modify column if you want other
Do While .Range("X" & row) <> ""
If .Range("X" & row) > 0 Then
.Range("W" & row) = .Range("X" & row)
End If
'Increase row
row = row + 1
Loop
End With
End Sub
My row data is as follows:
Column A
Datapoint1
Datapoint2
Datapoint3
Datapoint4
I can transpose, but the result will be like this.
Column A Column B Column C
Datapoint1 Datapoint2 Datapoint3
What I am trying to achieve is
Column A, Column B, Column C, Column D, Column E, Column F, Column G
Datapoint1, empty, empty, empty, empty, empty, Datapoint 2
Datapoint 3 would appear in Column M. There are 5 columns in between.
There are about 3,000 Datapoints that need to be transposed into columns (separated by 5 columns in between).
You can use this function below. I tested it with 3000 items and it ran almost instantly. The problem is after 2732 items, you run out of rows as the Excel maximum column number is 16,384 as of Office 2013 (see Office 2010 Excel Specifications and Office 2013 Excel Specifications). In the code below, once it passes that, it simply clears out the remaining rows and prints a statement to the debug window. Handle this how you want.
Sub TransposeIt()
Const maxColumns As Integer = 16384
On Error GoTo er
Application.ScreenUpdating = False '' shut off screen updating to speed up if possible
Dim x As Range
Set x = Range("A1", Range("A1").End(xlDown)) '' Selects all cells until there is a blank
'' if you might have blanks, then calculate the last row manually
Dim c As Range, col As Integer, val As Variant
For Each c In x.Cells
val = c.Value '' so as not to overwrite A1
c.Value = ""
col = (c.row - 1) * 6 + 1
If col > maxColumns Then '' max number of rows we can actually use is 2732
Debug.Print "Out of Columns, clearing Row " & c.row '' do what you want at this point
Else
Cells(1, col).Value = val
End If
Next
ex:
Application.ScreenUpdating = True
Exit Sub
er:
Debug.Print "An Error has occured: " & Err.Description
Resume ex
End Sub
I am looking to update the last column of one data table with the last column of another data table. This is part of a bigger vba code. The first table spreads from A2 to column K and row "lastrpivot1". The second goes from A1001 to column K and row "lastrpivot2". Beginning with the first row of table 2 (row1001) i have to find the equivalent row in table 1 based on the values in cells A to E.
So cells A to E or frow 1001 have to be compared to cells A to E of row 2, then row 3, then row 4... until a match if found or until row "lastrpivot1". When a match is found, the value in K must return to the K value of row 1001. EX: if AtoE of row 1001 match row AtoE of row 65, then copu K65 to K1001. there shound not be more than 1 match from each table. and if there is no match there is nothing to return.
Then we start this all over for row 1002 (second row of second chart), then 1003, 1004... to lastrpivot2.
I do use vba but i do not know all the functions. this is probably why i cant figure this out.
Thnka you
In Cell K1001, try this:
=IF((A1001&B1001&C1001&D1001&E1001)=(A1&B1&C1&D1&E1),K1,"")
Then drag the formula down.
This compares the entire row 1001 to the entire row 1, which is what you're asking for.
If you intend to find the matching row like a VLOOKUP (you kind of imply this, but it is not clear that this is your intention) then you will need to use VBA to do this.
Something like (untested):
Sub MatchTables()
Dim tbl1 as Range, tbl2 as Range
Dim var1() as Variant, var2() as Variant, v as Variant
Dim r as Long, matchRow as Long
Set tbl1 = Range("A1:K500") '## Modify as needed
Set tbl2 = Range("A1001:K15001") '## Modify as needed
ReDim var1(1 to tbl1.Rows.Count)
ReDim var2(1 to tbl2.Rows.Count)
'## store the range values, conctaenated, in array variables:
For r = 1 to tbl1.Rows.Count
var1(r) = tbl1(r,1) & tbl1(r,2) & tbl1(r,3) & tbl1(r,4) & tbl(r,5)
Next
For r = 1 to tbl2.Rows.Count
var2(r) = tbl2(r,1) & tbl2(r,2) & tbl2(r,3) & tbl2(r,4) & tbl2(r,5)
Next
r = 0
For each v in Var2
r = r+1
'## Check to see if there is a complete match:
If Not IsError(Application.Match(v, var1, False)) Then
matchRow = Application.Match(v, var1, False)
'## If there is a match, return the value from column K in the first table:
tbl2.Cells(r,11).Value = tbl1.Cells(matchRow,10).Value
Else:
tbl2.Cells(r,11).Value = vbNullString
End If
Next
End Sub