VBA Excel Line Break in Cell - vba

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 .

Related

How to combine the values of different cells into strings of our own format and store it in the third cell?

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

Excel VBA: Fill in empty cells with adjacent cell values

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

Highlight specific rows in table according to condition

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.

Excel VBA - Finding Last Non-Empty Row Based on Column Header

I am currently creating a reporting sheet for equipment for our company. This sheet will grab data from multiple Excel sheets and populate it based on our custom report template. So far I have managed to find on how to merge multiple sheets and find the column header. But now I'm stucked on how to find the last non empty value based on column header.
Attached in the picture above is the sample data. As you can see, the last non empty cell in each row is the average of the row. What I am trying to do is to find the header column(eg SVC525) and take the last non empty value of the column, which is average.
for the example given, column B, this should do the trick
Dim lastrow as Integer, val
lastrow = Range("B" & Rows.Count).End(xlUp).Row
val = range("B" & lastrow + 2).Value
to iterate through your rows(header) that is not empty is another story that you can easily search for.
you may be after something like this
Dim svVal As String
svVal = "SVC525" '<--| set your header value to be searched for in row 1
With Worksheets("averages") '<--| change "Averages" tou your actual sheet name
MsgBox .Cells(.Rows.count, .Range("A1", .Cells(1, .Columns.count).End(xlToLeft)).Find(what:=svVal, LookIn:=xlValues, lookat:=xlWhole).column).End(xlUp)
End With

Copy a value from one sheet to another and then copy result back to same sheet in different column

I apologise for the ambiguity in the question/statement but its easier to explain my issue.
I have a tool that I use to process some data in one sheet of my workbook (sheet1). And a data sheet that provides that data (sheet2)
Basically I want to take each value from Y2:LastRow of the datasheet (sheet2) run that value through the tool in sheet1 then copy that result and paste it into the same row but a different column (AQ to be exact).
Here is what I have tried so far:
With Worksheets("DataSheet")
LastRow = .Range("Y" & .Rows.Count).End(xlUp).row
For Each cell In Worksheets("DataSheet").Range(Cells(LastRow, 25), Cells(1, 25))
ThisWorkbook.Worksheets("Conversion Tool").Range("A1").Value = cell
Sheet1.Range("F14").Copy
Sheet2.Range(Cells(LastRow, 43), Cells(2, 43)).PasteSpecial (xlPasteValues)
Next
End With
However, with this is pastes the value into all cells of that column. I know that there is probably some basic fix but I'm still getting to grips with this.
Thanks in advance.
This should work for you
With Worksheets("DataSheet")
LastRow = .Range("Y" & .Rows.Count).End(xlUp).Row
For Each Cell In .Range(Cells(LastRow, 25), Cells(1, 25))
ThisWorkbook.Worksheets("Conversion Tool").Range("A1").Value = Cell
Sheet1.Range("F14").Copy
.Range("AQ" & Cell.Row).PasteSpecial xlPasteValues
Next
End With
It takes the row from the cell you copied and enters the new value in same row but column AQ.