I was wondering if someone can help -
I have a column 'I' containing cells of different values; some of these cells are blank. I also have an adjacent complete column 'G' - no blank cells.
What I'm basically trying to do is use VBA to fill in the empty cells in column 'I' with the value of their adjacent cells in column 'G'.
I wrote the below code in a command button:
Sub CommandButton2_Click()
Lastrow = Cells(Rows.Count, "G").End(xlUp).Row
Range("I2:I" & Lastrow).SpecialCells(xlCellTypeBlanks).Value = Range("G2:G" & Lastrow).Value
End Sub
The above code populates the blank 'I' cells, but it always seems to start with the second 'G' cell when populating. I want it to populate the blank 'I' cell with it's adjacent 'G' cell, and not start counting from the beginning when it finds a blank cell.
Hope someone can help. Let me know if I need to clarify things further.
Regards,
Adrian
Dim blanks As Excel.Range
Set blanks = Range("I2:I" & Cells(Rows.Count, 7).End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
blanks.Value = blanks.Offset(0, -2).Value
Related
I have a table that has two columns namely Description and Amount. I want to write those details in a third cell in the form of Description1-Amount1, Description2-Amount2, and so on till there is an empty row. Please look at the image for better understanding.
So, now in the Sheet 2 in cell A1, I want the content as Dad-500, Chegg-123. Suppose if there are more items, I want them to be subsequently added.
I have designed a macro such that on click, whatever Values are present in the table at that point of time should be saved in Cell A1 of Sheet2.
My code is:
Sub database
Dim emptyRow As Long
Dim mix1 As String
mix1 = "=CONCATENATE(Sheet1!C5,""-"",Sheet1!D5)"
Sheet2.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = mix1
End Sub
In the above code, Sheet1!C5,""-"",Sheet1!D5 represents Description (C column) and Amount (D column) respectively.
This code copies the formula into the second sheet. But I want those details to be converted as a string and then store in the empty cell (A1), such that next time if I run the macro with the different set of values in the table, those details should be copied in Cell A2 (since that is the next empty cell) and the values in cell A1 should not be changed since it is converted to string.
Now the problem I am facing is if I run the macro for the second time, both the values in cell A1 and A2 are changing since both these cells contains formulas and not strings.
Please help me as to how to combine the values of different cells into strings of our own format and store it in the third string.
Transfer the values without creating a formula.
sub database
with sheet2
.cells(.rows.count, "A").end(xlup).offset(1, 0) = _
sheet1.cells(5, "C").value2 & chr(45) & sheet1.cells(5, "D").value2
end with
end sub
I am trying make this macro works. It supposed to highlight rows in a table according to the condition. When I try to offset the selected range, so only the cells within the table get highlighted. But when I do so, my condition doesnt work as it should be. I am completly newbie to this, many thanks for any advice.
Sub výklep()
'Find the last non-blank cell in column
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Set SelectedRange = Range("I6:I" & LastRow)
For Each cell In SelectedRange
If cell.Value > 10 Then cell.Range(Cells(1, 1), Cells(1, 9)).Offset(1, -7).Interior.ColorIndex = 3
Next
End Sub
You must be careful while offsetting with minus sign (-). In your case it is minus seven (-7). While looping it is crossing the left border of your sheet thus causing error.
I have almost no experience with VBA that being said.
I have a function in "B21" that I have copied over the same row up to "Z21"
I want the results from these cells to be shown on a different sheet all in one Column starting at "B2" and repeating until till it reaches the end.
This is what I have for that.
Sheets("Barlist").Range("B2").Value = "B21"
Alternatively the function I have in "B21" that I have copied over to repeat itself is:
=IF((COLUMN()-1)<$B$16,ROUND(TAN(RADIANS($B$5))*(($B$6)-($B$15*(COLUMN()-2))),2)+($B$11-0.33),IF((COLUMN()-1)=$B$16,$B$18,""))
So if I could just tell in to repeat this function down Column "B" until it returns a blank result that would should work to.
This code would cycle through all cells from B21 to the last column in row 21 with data and if the cell is blank you would need to add the code for what you wanted to do with that cell. Otherwise, the cells gets placed into "Barlist" starting at B2 and working down (using the next blank row each time)
Sub NAMEOFSUB()
Dim LastCol As Long
Dim CurCol As Long
Dim DestRow As Long
LastCol = Sheets("CURRENT SHEET NAME").Cells(21, Columns.Count).End(xlToLeft).Column
For CurCol = 2 to LastCol
If Sheets("CURRENT SHEET NAME").Cells(21, CurCol).Value = "" Then
'Do Something with your blank cell such as add a .Formula
Else
DestRow = Sheets("Barlist").Range("B" & Rows.Count).End(xlUp).Row + 1
Sheets("Barlist").Range("B" & DestRow).Value = Sheets("CURRENT SHEET NAME").Cells(2, CurCol).Value
End If
Next CurCol
End Sub
What exactly are you trying to do?
Right now your code would just enter the value "B21" in cell B2 on sheet Barlist.
If you want to add a reference to that cell, you could use Sheets("Barlist").Range("B2").FormulaR1C1="R21C2"
Although I really don't see any reason to use VBA at all for this. If you want a dynamic range, so it is linked to the original range, you could simply use the Transpose Function.
For your example, select the cells B2 to B26 on your Barlist sheet. Press F2 (should enter cell B2) and enter the formula =TRANSPOSE(SHEET!B21:Z21). This is an array function, so you don't just press enter, you have to press CTRL+SHIFT+ENTER at the same time to enter it. Excel will automatically append little curly brackets {} around the formula so you know it's an array formula.
If you want static values, select the range B21:Z21 you want to copy, then go to cell B2 on your Barlist sheet and right click>paste special and use the "trasnpsose" option
I have a project on excel macro, I need to highlight the next last row that has an empty value. example cell A1:A100 have data and the next cell is A101 is empty.
when user click a button it should highlight the cell A101...
If you are certain that you only need column A, then you can use an End function in VBA to get that result.
If all the cells A1:A100 are filled, then to select the next empty cell use:
Range("A1").End(xlDown).Offset(1, 0).Select
Here, End(xlDown) is the equivalent of selecting A1 and pressing Ctrl + Down Arrow.
If there are blank cells in A1:A100, then you need to start at the bottom and work your way up. You can do this by combining the use of Rows.Count and End(xlUp), like so:
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Going on even further, this can be generalized to selecting a range of cells, starting at a point of your choice (not just in column A). In the following code, assume you have values in cells C10:C100, with blank cells interspersed in between. You wish to select all the cells C10:C100, not knowing that the column ends at row 100, starting by manually selecting C10.
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlUp)).Select
The above line is perhaps one of the more important lines to know as a VBA programmer, as it allows you to dynamically select ranges based on very few criteria, and not be bothered with blank cells in the middle.
try this:
Sub test()
With Application.WorksheetFunction
Cells(.CountA(Columns("A:A")) + 1, 1).Select
End With
End Sub
Hope this works for you.
This does it:
Do
c = c + 1
Loop While Cells(c, "A").Value <> ""
'prints the last empty row
Debug.Print c
I've been searching for a solution to this problem for over a week now. I have a sheet with formatted text (colors and styling) with values an undetermined distance down the first column. There are some spaces in between but I believe I've handled that situation correctly by using IsEmpty. I want to copy each of these values from each cell all to one cell at the top of the second column. I've been successful in being able to copy the text from each of these cells into a specified concatenated cell with line breaks, however I've been unsuccessful at keeping the formatting. Any help anyone can provide on how to copy the formatting along with this text would be greatly appreciated. Thank you!
'set variable to find the last row of the sheet
Dim LastRow As Integer
'find the last row of the active sheet
LastRow = ActiveSheet.UsedRange.Rows.Count
'loop through each of the rows
For C = 1 To LastRow
'determine if a cell has a value in it - if so complete below commands
If (IsEmpty(Cells(C, 1).Value) = False) Then
'leave the contents of the first cell in the second column, insert a linebreak and copy the values from the current cell in the first column
Cells(1, 2).Value = Cells(1, 2).Value & Chr(10) & Cells(C, 1).Value
End If
Next C
You can make use of Range.Copy and Range.PasteSpecial properties that is easy to use and solved your problem of formatting cell .