I have cells that are supposed to be 0 I believe. 7.45058059692383E-12.
How do I make these 0 in my code?
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i)
I thought converting NumberFormat to 0 may help but it did not work.
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Instead the result is FALSE
Please Help!
Full code:
Sub CopyData()
Worksheets("MainData").Rows("2:" & Rows.Count).ClearContents
'Copy data from the CM Commentary File to Template
Application.ScreenUpdating = False
Set MainDataCM = Workbooks.Open(Sheets("Input").Range("B3") & Sheets("Input").Range("B6"))
MainDataCM.Sheets("Main Data").Copy After:=ThisWorkbook.Sheets(1)
MainDataCM.Close savechanges:=False
Application.ScreenUpdating = True
Sheets("Main Data").Name = "CM_MainData"
Worksheets("CM_MainData").Visible = False
'Read the CM_MainData tab and copy the required columns in the MainData tab
Dim k As Long
k = Sheets("CM_MainData").Range("A1", Sheets("CM_MainData").Range("A1").End(xlDown)).Rows.Count
Debug.Print (k)
i = 2
While i <= k
Sheets("MainData").Range("A" & i) = Sheets("CM_MainData").Range("A" & i)
Sheets("MainData").Range("B" & i) = Sheets("CM_MainData").Range("B" & i)
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Sheets("MainData").Range("D" & i) = Sheets("CM_MainData").Range("D" & i)
Sheets("MainData").Range("E" & i) = Sheets("CM_MainData").Range("C" & i) * 1000
Sheets("MainData").Range("F" & i) = Sheets("CM_MainData").Range("H" & i)
'Sheets("MainData").Range("E" & i).NumberFormat = "0.00"
If Sheets("MainData").Range("F" & i) = "" Then
Sheets("MainData").Range("F" & i) = "RBC INVESTOR SERV O/H & MISC"
End If
i = i + 1
Wend
Worksheets("Macro").Activate
Worksheets("Macro").Select
MsgBox "Step 1 Completed"
End Sub
Value transfer:
Sheets("MainData").Range("C" & i).Value = Sheets("CM_MainData").Range("C" & i).Value
Number format (a separate step, and does not change the underlying value):
Sheets("MainData").Range("C" & i).NumberFormat = "0.00"
Or use WorksheetFunction.Round (does change the underlying value).
Sheets("MainData").Range("C" & i).Value = WorksheetFunction.Round(Sheets("CM_MainData").Range("C" & i).Value, 2)
Related
If Column 2 is equal ChosenDate when the button is clicked, the Label Box in VBA will display all the row data of Column 6. But I can only display one data. and if the value ChosenDate is changed the data diplayed in Label Box will also be changed.
Dim DateChosen As Date
Dim i As Integer
DateChosen = "2020/07/20"
For i = 10 To 5000
If Cells(i, 2).Value = DateChosen Then
EventLabel.Caption = Range("F" & i)
End If
Next i
Try replacing of
EventLabel.Caption = Range("F" & i)
with
EventLabel.Caption = EventLabel.Caption & Range("F" & i) & VbCrLf 'to be displayed one on top of the other
or
EventLabel.Caption = EventLabel.Caption & Range("F" & i) & ", "' to be dsplayed one after the other
Edited:
In order to keep only the last iteration/processing result, your code must previously clear the caption:
EventLabel.Caption = ""
For i = 10 To 5000
If Cells(i, 2).Value = DateChosen Then
EventLabel.Caption = EventLabel.Caption & Range("F" & i) & VbCrLf
End If
Next i
Try below sub.
Sub MultiLineLabel()
Dim DateChosen As Date
Dim i As Integer
Dim myCaption
DateChosen = "2020/07/20"
For i = 10 To 5000
If Cells(i, 2).Value = DateChosen Then
myCaption = myCaption & Range("F" & i) & vbNewLine
End If
Next i
EventLabel.Caption = myCaption
End Sub
I'm trying to modify the below function to include logic where if the variables PPD_1_Date, PPD_2_Date and TSpot_Date are all empty (blank) then output to my "Error" worksheet.
I have rows that should fall under this logic, however they are falling under the Else condition instead.
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim TSpot_Date As Variant
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
Entity = Worksheets("Data").Range("J" & i)
Dept = Worksheets("Data").Range("M" & i)
TSpot_Date = Worksheets("Data").Range("AS" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
j = j + 1
Else
'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
'GoTo EmptyRange
'Else
If (InStr(1, Entity, "CNG Hospital") Or InStr(1, Entity, "Home Health") Or InStr(1, Entity, "Hospice") Or InStr(1, Dept, "Volunteers") Or ((IsEmpty(PPD_1_Date) = True) And (IsEmpty(PPD_2_Date) = True))) And IsEmpty(TSpot_Date) = True Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
k = k + 1
Else
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = TSpot_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AY" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = "NO PPD DATES BUT HAS TSPOT DATE1"
j = j + 1
End If
End If
End If
'EmptyRange:
'k = k + 1
Next i
End Function
Here is the code I added to the other OR logic;
Or ((IsEmpty(PPD_1_Date) = True) And (IsEmpty(PPD_2_Date) = True))
Example row has empty cells in columns AW, BA, and AS, so it should write to my Error worksheet. Is there a syntax or logic issue? I did initially have TSPOT_Date defined as a Date variable, however I was getting a '1004' runtime error (I think because some column rows are empty) so I changed to Variant, however logic still doesn't work as I expect.
The problem you're running into is that you can't check if Date variables are "empty" using isEmpty() or even with Len() because the default value for a date is 30-Dec-1899 00:00:00, so there is always a value in a Date variable.
Instead, you should check to see that a Date variable is empty/has not been filled like this
If PPD_2_Date = 0 Then
...
I'm trying to get an output in column U for multiple worksheets in a workbook. The return string will either be "Yes" or "No" depending on which column is not blank and if the difference between two dates are > 150. This is the code I have written, but nothing shows up in column U. Could anyone help me figure out why this isn't working?
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Integer
Dim listLength
listLength = ws.Cells(Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub
When you start your loop with For Each ws In ThisWorkbook.Worksheets then Excel starts processing from Sheet1 and I'm sure your list in another sheet.
So, the reason nothing changes in your U column is, because you are
looking at wrong Worksheet.
As long as you would like to loop through all of your worksheets, start to follow your lists from Sheet1. If you would like to loop in different order, you should define this in your code.
And below I edited some part of your code and also added msgbox ws.name to show you in which sheet Excel is working now.
Why Use Integer Instead of Long?
Option Explicit
Sub Compliance()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i As Long
Dim listLength
MsgBox ws.Name
listLength = ws.Cells(ws.Rows.Count, "M").End(xlUp).Row - 1
For i = 2 To listLength + 2
If IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And IsEmpty(ws.Range("N" & i)) = True And DateDiff("d", ws.Range("M" & i), ws.Range("K" & i)) > 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And IsEmpty(ws.Range("O" & i)) = True And DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf IsEmpty(ws.Range("P" & i)) = True And DateDiff("d", ws.Range("O" & i), ws.Range("N" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
ElseIf DateDiff("d", ws.Range("N" & i), ws.Range("M" & i)) < 150 Then
ws.Range("U" & i) = "Yes"
Else
ws.Range("U" & i) = "No"
End If
Next
Next ws
End Sub
I have this code and I need textbox 9 and 10 to be number vlaues when entering in my excel table any idea how I can fix this?
Sub FillRanges(ws As Worksheet, L As Long)
With ws
.Range("C" & L).Value = (Now)
.Range("D" & L).Value = Me.TextBox2
.Range("E" & L).Value = Me.TextBox3
.Range("F" & L).Value = Me.TextBox4
.Range("G" & L).Value = Me.TextBox5
.Range("K" & L).Value = Me.ComboBox1
.Range("L" & L).Value = Me.ComboBox2
.Range("M" & L).Value = Me.ComboBox3
.Range("N" & L).Value = Me.TextBox9
.Range("O" & L).Value = Me.TextBox10
.Range("R" & L).Value = Me.TextBox39
.Range("P" & L).Value = Me.TextBox40
End With
End Sub
You can use a convert function like CDbl(). It would be something like:
Sub FillRanges(ws As Worksheet, L As Long)
With ws
.Range("C" & L).Value = (Now)
.Range("D" & L).Value = Me.TextBox2
.Range("E" & L).Value = Me.TextBox3
.Range("F" & L).Value = Me.TextBox4
.Range("G" & L).Value = Me.TextBox5
.Range("K" & L).Value = Me.ComboBox1
.Range("L" & L).Value = Me.ComboBox2
.Range("M" & L).Value = Me.ComboBox3
.Range("N" & L).Value = CDbl(Me.TextBox9)
.Range("O" & L).Value = CDbl(Me.TextBox10)
.Range("R" & L).Value = Me.TextBox39
.Range("P" & L).Value = Me.TextBox40
End With
There are also other convert functions. CInt()(integer), CLng()(long) and CDec()(decimal).
I think it's preferable to validate user input before using (writing) it
so you may want to write some quite simple user input validation subs and call them from within controls change event handler, like follows:
Option Explicit
Private Sub TextBox9_Change()
ValidateNumericInput Me.TextBox9, 0, 10.4 '<--| as soon as this control text changes, call 'ValidateNumericInput' to validate it
End Sub
Private Sub ValidateNumericInput(tb As MSForms.TextBox, minVal As Double, maxVal As Double)
Dim errMsg As String
With tb
If Len(.Text) > 0 Then '<-- proceed only if there's some text to validate!
Select Case True
Case Not IsNumeric(.value) '<--| if not a "numeric" input
errMsg = "please enter a number"
Case CDbl(.Text) < minVal Or CDbl(.Text) > maxVal '<--| if "numeric" input exceeds passed range
errMsg = "please enter a number within " & minVal & " and " & maxVal
End Select
If errMsg <> "" Then '<--| if error message has been written
MsgBox "invalid input in " & tb.name & vbCrLf & vbCrLf & errMsg, vbCritical + vbExclamation + vbOKOnly, "Invalid input" '<--| infrm the user
.Text = "" '<--| delete textbox input
End If
End If
End With
End Sub
where I assumed a Double type input would be needed, but you can easily adapt it to other types
so, you may then add such other subs as:
ValidateStringInput(tb As MSForms.TextBox, validStrings() as String)
and the likes...
I would like to change the data values of textboxes by using a spinbutton
The data are written in an Excel table ..... could you explain to me which
syntax I should use or perhaps could you show me an example?
Private Sub SpinButton1_Change()
'Range("G15").Value = SpinButton1.Value
Dim I As Integer
For I = 2 To 10 Step 1
TextBox2.Value = Ws.Range("A"& I)
Next
Using the following code i gives a good result but but why do i need an msgbox ? Without msgbox it doesn't function ... strange ...
Private Sub SpinButton1_Change()
Dim I As Integer
For I = 2 To 10 Step 1
MsgBox ("La valeur de la Textbox" & I & " est de " & TextBox2)
ComboBox1.Value = Ws.Range("A" & I)
ActiveCell = Me.ComboBox1.Value
TextBox2.Value = Ws.Range("B" & I)
TextBox3.Value = Ws.Range("C" & I)
TextBox4.Value = Ws.Range("D" & I)
TextBox5.Value = Ws.Range("E" & I)
TextBox6.Value = Ws.Range("F" & I)
TextBox7.Value = Ws.Range("G" & I)
TextBox8.Value = Ws.Range("H" & I)
TextBox9.Value = Ws.Range("I" & I)
'Range("G15").Value = SpinButton1.Value
Next