Avatar of gdunn59
gdunn59
 asked on

How can I add to my code to have it check 2 cells to see if they contain data if so proceed with rest of code

I have the following code that loops through a spreadsheet and gathers data from 4 different columns.  I need to have it check to see if Columns 12 OR 13 contain data, if so then collect that data and put in Sheet2, if those 2 cells are empty, then skip over them and go to the next row and check.

Everything is working right now, except it is putting all rows 4 different columns in Sheet2 and I just need it to put them in Sheet2 if there is data in Columns 12 OR 13.

How can I revise my existing code to do this?  Thanks!

Here is my code:
Private Sub cmdSubmitChangesTo3E_Click()
' create an XML file from an Excel table
    Dim varInvIndex As String
    Dim varInvElectHistID As String
    Dim varDateSubmitted As String
    Dim varComments2 As String
    Dim i As Long
    Dim wb1 As Excel.Workbook
    Dim k As Long

    ActiveWorkbook.Worksheets("Sheet1").Activate

    i = 8
    k = 2

    Do Until i > Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        ' varInvIndex
        Worksheets("Sheet2").Range("A" & k) = Cells(i, 5).Value
        'varInvElectHistID
        Worksheets("Sheet2").Range("b" & k) = Cells(i, 6).Value
        ' varDateSubmitted
        Worksheets("Sheet2").Range("c" & k) = Cells(i, 12).Value
        'varComments2
        Worksheets("Sheet2").Range("d" & k) = Cells(i, 13).Value

        k = k + 1
        i = i + 1
    Loop

Worksheets("Sheet2").Range("A1").Value = "InvIndex"
Worksheets("Sheet2").Range("B1").Value = "InvElectHistID"
Worksheets("Sheet2").Range("C1").Value = "Date Submitted"
Worksheets("Sheet2").Range("D1").Value = "Comments"

Cells.Select
Cells.EntireColumn.AutoFit
Columns("A:A").Select
Selection.ColumnWidth = 11.57
Cells.Select
Cells.EntireRow.AutoFit
Rows("2:2").Select
'With ActiveWindow
'    .SplitColumn = 0
'    .SplitRow = 1
'End With

Worksheets("Sheet2").Select
ActiveWindow.FreezePanes = False
Worksheets("Sheet2").Range("A2").Select
ActiveWindow.FreezePanes = True

Worksheets("Sheet2").Range("A2").Select

ActiveWorkbook.Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A8").Select

End Sub

Open in new window

Microsoft ExcelVisual Basic ClassicSpreadsheets

Avatar of undefined
Last Comment
SStory

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
SStory

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck