My code is checking if cell value from 2 sheets are different, if no it continue check for next row, if different it copy from sheet2 to sheet1 the cell value where I need and add to Cell in sheet1(where the value copied too) a comment with the old value.
every time when the value changed again it remove the comment and put new one.
I need to do a comment check if exist and append the old value to comment.
I want that the comment will contain all the old values that changed in the cell.
this is my piece of code:
If Not IsEmpty(datasheet.Cells(iData, j).Value) Then
comm = user & vbNewLine & "Old Date:" & vbNewLine & ActiveCell.Value
datasheet.Cells(iData, j).Copy Destination:=ActiveCell
With ActiveCell
ActiveCell.Interior.ColorIndex = 0
With ActiveCell.Borders
.LineStyle = xlContinuous 'Setting style of border line
.Weight = xlThin 'Setting weight of border line
.ColorIndex = xlAutomatic 'Setting colour of border line
End With
If Not .Comment Is Nothing Then .Comment.Delete
.ClearComments
.AddComment
.Comment.Text Text:=comm
.Comment.Visible = False
End With
Related
Good afternoon,
I have another problem related to IF statement.
I have the following code:
Sub BOMNewRow()
If Range("D38").Value <> "" Or Range("C38") <> "x" Or Range("C38") <> "0" Then
'Inserting new Core Drill row + formula autofill from the previous one
Range("A38").EntireRow.Insert
Range("A38").Value = "First Core drill into building or existing chamber (per event)"
Range("B38").Value = "Sitec"
If Range("D39").Value Like "*2*" Or Range("C39").Value = 2 Then
Range("C38").Value = 2
Else
Range("C38").Value = 1
End If
Range("E38").Value = 100.39
Range("F37").Select
Selection.AutoFill Destination:=Range("F37:F38"), Type:=xlFillDefault
'customizing new row
With Range("A38:B38,D38:F38")
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).Weight = xlThin
End With
With Range("C38")
.Borders(xlEdgeBottom).Weight = xlMedium
End With
Else
Call AuditCheck 'go straight away to the BoM audit control
'Taking a look on one of the audit cell
End If
If Range("C39").Value <> "x" Or Range("C39").Value <> "0" Then
Range("C39").Value = "x"
Range("D38").Value = Range("D39")
Call New_version2
Sheets("BoM").Activate
Else
Call AuditCheck
End If
Range("A43").Select
End Sub
What I intend to do is:
insert the new row 38 and customize it - this is done by my first IF statement.
run the second IF statement based on the cell C39, which doesn't work.
If previously I had the X value in the cell C38, now when I inserted a new row the second IF statement still executes the process for value C39, which is X.
.
It looks like I don't understand the if statement order within the macro.
How can I solve this problem?
In my userform, users can enter numbers in a textbox (2 maximum).
The number then gets copied in a worksheet.
I'm trying to have any number entered in this textbox to get copied but with the number 6 at the end.
If the user enter '.14'; then in the worksheet, I need to see '.16'.
If the user enter '.45'; then in the worksheet, I need to see '.46'.
And so on..
The only exception, is If the user enter '6' in the textbox, then in the worksheet, I need to ONLY see '.6', and not '.66'. (this problem was fixed thanks to an answer received earlier : VBA to ignore cell formating when adding a specific number using a userform)
I've used cell formating and entered ''.06'' In 'Type'. I thought It worked, but then when I enter let's say '.43' in the userform, the number that gets copied in the cell is not '.46' like expected, but ''.56''.
I dont know why.
EDIT :
I tried this but it's not working :
Sub ReplaceLastDigit()
Dim r As Range
Application.ScreenUpdating = False
For Each r In Range("C4", Range("C" & Rows.Count).End(xlUp))
If IsNumeric(r) And Len(r) > 1 Then
r = Left(r, Len(r) - 1) & "6"
End If
Next r
Application.ScreenUpdating = True
End Sub
EDIT 2:
Everything works, except one thing. When it's only ".6", it doesn't add ".66" (great), but it adds a zero. So I see ".60". Is it the formating?
This is the code :
If Left(TextBox3.Text, 1) <> 6 Then
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "." & Left(TextBox3.Text, 1) & 6
Else
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = ".6"
End If
Here is the correction to my code that made it work :
If Left(TextBox3.Text, 1) <> 6 Then
Sheets("Sheet3").Range("C4").NumberFormat = "#"
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "." & Left(TextBox3.Text, 1) & 6
Else
Sheets("Sheet3").Range("C4").NumberFormat = "#"
Sheets("Sheet3").Range("C4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = ".6"
End If
I am new to VBA and looking to run a code to colour cells in rows in a specific colour. I have been using DO UNTIL and always end with an extra cell coloured. What is the best way to overcome this.
The table I am working with looks like this,
Number/Name
1/test_01
2/test_02
3/test_03
4/test_04
5/test_05
and continues on and the end will change each time i run the code.
I have set up a test sheet to get the basic idea running so I can expand upon it once I have it running properly. This specific test is dividing column A (Number) by 2 and if there is a remainder of 1 then it will be coloured one way and if not it will be coloured another.
Sub Button2_Click()
Dim row_cnt As Integer
row_cnt = 1
Do Until Sheets("sheet1").Range("A" & row_cnt).Value = ""
row_cnt = row_cnt + 1
If Sheets("sheet1").Range("A" & row_cnt).Value Mod 2 <> 0 Then
Range("A" & row_cnt & ":B" & row_cnt).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
Else
Range("A" & row_cnt & ":B" & row_cnt).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
End If
Loop
End Sub
I expect the cells to be coloured until the last cell with a value in. However, this code goes past that and colours an extra cell. I am looking for a way to improve what I have.
you can dynamically find the last row, such that:
lr = cells(rows.count,1).end(xlup).row
row_cnt = 1
Do until row_cnt = lr+1 'so you get actions on your last row
'do stuff
row_cnt = row_cnt + 1
loop
if you can avoid vba for this, bigben's suggestion for conditional formatting would be solid
To answer the specific Q "Why doesn't my Do Untilwork":
It's because you test based on a value of row_cnt , then immediately increment it inside the loop, so process the next row.
To fix that, move the increment to just before Loop and adjust the initialisation of row_cnt
On a side note, you should use Long rather than Integer as the counter data type
This might be a trivial question for you experts out there:
Based on the input (Week) the table is initially filtered on that specific week(WK) and the next one(WK+1).
I'm then formatting all WK+1 cells to be greyed out.
So far so good. Now the question. How can I change the code below so that the entire row containing a cell with the WK+1 value to be greyed out?
ActiveSheet.Range("B5").AutoFilter Field:=1, Criteria1:=WK, Operator:=xlOr, _
Criteria2:=(WK + 1)
With ActiveSheet.Range("$B:$B").FormatConditions _
.Add(xlCellValue, xlEqual, "=" & WK + 1)
With .Font
.Bold = True
.ColorIndex = 15
End With
End With
Thanks in advance!
Mac
This is possible with FormatCondition of type xlExpression.
Example:
ActiveSheet.Range("$A:$Z").FormatConditions.Delete
ActiveSheet.Range("A1").Activate
With ActiveSheet.Range("$A:$Z").FormatConditions _
.Add(Type:=xlExpression, Formula1:="=($B1=" & WK + 1 & ")")
With .Font
.Bold = True
.ColorIndex = 15
End With
End With
I delete the FormatConditions before I add the new one. Because if not, multiple calls of this code would result in multiple FormatConditions ever with the same condition but possible other values of WK. This is because the code adds a new FormatCondition everytime it runs.
Greetings
Axel
I am using a loop to put a conditional format in "every 4th row" in "column D". However, I am not able to get the code correct.
I had two seperate things happen. First, I have had the formula show up in the spreadsheet with " " around the formula and the conditional format did not work. So now I am trying to rewrite it using the following code and it tells me compile error expected end of statement.
Any help on how I can get the conditional format of D(i-1)>sum(D(i):D(i+2) shade cell red to work is appreciated.
This is the middle of the For/Next loop where I am trying to shade.
Range("D" & (i - 1)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="D" & (i-1) & "> Sum(D" & i & "D" & (i + 2)")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
i = i + 4
This worked for me:
Sub Tester()
Dim i As Long, fc As FormatCondition
For i = 2 To 10 Step 4
Set fc = ActiveSheet.Range("D" & (i - 1)).FormatConditions. _
Add(Type:=xlExpression, _
Formula1:="=D" & (i - 1) & "> Sum(D" & i & ":D" & (i + 2) & ")")
fc.SetFirstPriority
With fc.Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
fc.StopIfTrue = False
Next i
End Sub
Is it a requirement to do this in VBA? I'm asking because, through regular conditional formatting, you could also easily apply it to every 4th row.
To do so, you simply need a formula like this one in your conditional formatting:
=AND(MOD(ROW($D5),4)=0, $D5 > SUM($D$2:$D4))
The MOD-function divides the row number by 4 and checks if the remainder is 4.
It's not exactly what you asked for, but it might solve your situation...