if-then returns the last true condition

kathytrahan

New Member
Joined
Feb 15, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
My code only writes the last true condition to my log. I understand it is writing over any prior true conditions in my selection. I'm not sure how to write the selection so that is saves each true condition on the next line of the log. How do I write the selection so that it will record each true condition ? I am new to VBA. This is the vba I'm having trouble with:
LogRow = Sheet3.Range("C99999").End(xlUp).Row + 1 'First Available Log Row
Dim Cll As Range
For Each Cll In .Range("D3,D4,D5,D6,D7,D8,H3,H4,H5,H6")
If Cll.Value <> Cll.Offset(0, 1).Value Then
Sheet3.Range("C" & LogRow).Value = Now 'Add Current Date & Time
Sheet3.Range("D" & LogRow).Value = Application.UserName 'Excel Application User Name
Sheet3.Range("E" & LogRow).Value = .Range("H3").Value 'USER INITIALS
Sheet3.Range("F" & LogRow).Value = .Range("D3").Value 'SITE NAME
Sheet3.Range("G" & LogRow).Value = Cll.Offset(0, -1).Value 'Cell header category
Sheet3.Range("H" & LogRow).Value = Cll.Offset(0, 1).Value 'Previous Value
Sheet3.Range("I" & LogRow).Value = Cll.Value 'New Value
End If
Next Cll

test with Log.xlsm
ABCDEFGHI
1load custFALSE
2cust id2AccUMAX
3cust row6Site Name AccUMAXAccUMAXUser InitialsHYHY
4next id7Address2354 Springdale Road, Suite 1502354 Springdale Road, Suite 150Date10/10/202110/10/2021
5srch cust6City PrincetonPrincetonSales Repmarymary
6new custFALSEStateNJNJSite ContactJohn Walters John Walters
7curr row1Zip code3358733587
8curr valload custCountryUSAUSA
9curr cell$I$6
form
Cell Formulas
RangeFormula
B3B3=IFERROR(MATCH(B2,CustID,0)+4,"")
B4B4=IFERROR(MAX(CustID)+1,1)
B5B5=IFERROR(MATCH(D2,SiteName,0)+4,"")
Cells with Data Validation
CellAllowCriteria
D2List=SiteName
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Welcome to the forum

Try moving one line
Rich (BB code):
Dim Cll As Range
For Each Cll In .Range("D3,D4,D5,D6,D7,D8,H3,H4,H5,H6")
    If Cll.Value <> Cll.Offset(0, 1).Value Then
        LogRow = Sheet3.Range("C99999").End(xlUp).Row + 1 'First Available Log Row
        Sheet3.Range("C" & LogRow).Value = Now 'Add Current Date & Time
        Sheet3.Range("D" & LogRow).Value = Application.UserName 'Excel Application User Name
        Sheet3.Range("E" & LogRow).Value = .Range("H3").Value 'USER INITIALS
        Sheet3.Range("F" & LogRow).Value = .Range("D3").Value 'SITE NAME
        Sheet3.Range("G" & LogRow).Value = Cll.Offset(0, -1).Value 'Cell header category
        Sheet3.Range("H" & LogRow).Value = Cll.Offset(0, 1).Value 'Previous Value
        Sheet3.Range("I" & LogRow).Value = Cll.Value 'New Value
    End If
Next Cll
 
Solution

kathytrahan

New Member
Joined
Feb 15, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Welcome to the forum

Try moving one line
Rich (BB code):
Dim Cll As Range
For Each Cll In .Range("D3,D4,D5,D6,D7,D8,H3,H4,H5,H6")
    If Cll.Value <> Cll.Offset(0, 1).Value Then
        LogRow = Sheet3.Range("C99999").End(xlUp).Row + 1 'First Available Log Row
        Sheet3.Range("C" & LogRow).Value = Now 'Add Current Date & Time
        Sheet3.Range("D" & LogRow).Value = Application.UserName 'Excel Application User Name
        Sheet3.Range("E" & LogRow).Value = .Range("H3").Value 'USER INITIALS
        Sheet3.Range("F" & LogRow).Value = .Range("D3").Value 'SITE NAME
        Sheet3.Range("G" & LogRow).Value = Cll.Offset(0, -1).Value 'Cell header category
        Sheet3.Range("H" & LogRow).Value = Cll.Offset(0, 1).Value 'Previous Value
        Sheet3.Range("I" & LogRow).Value = Cll.Value 'New Value
    End If
Next Cll
Thank you so much!! That was it. I forgot about the sequence of command order. Perfect.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,489
Messages
5,636,626
Members
416,932
Latest member
mm07

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top