VBA Counter Trouble

YoungOne

New Member
Joined
Mar 28, 2011
Messages
6
This question has to have a simple solution..

In this situation, the task is to search each row given column F, looking for cells that are blank. In the blank cell the words "No Comment" should be entered.

With the macro as it stands now, the VBA Counter is checking ever other row, i.e. 2,4,6,8; for the existence of blanks.

Where am I going wrong in getting this code to go through every line?

Code:
Sub FillBlanks()
 
Dim xlSurveySheet As Excel.Worksheet
Dim xlSurveyBook As Excel.Workbook
 
Dim SurveyLastRow As Integer
Dim SurveyQuestionFive As String
Dim SurveyRowCount As Long
 
fn = Application.GetOpenFilename("Excel-files,*.xlsx;*.csv", _
1, "Plateau Survey to Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
 
Set xlSurveyBook = Workbooks.Open(fn)
Set xlSurveySheet = xlSurveyBook.Worksheets(1)
 
'Set xlSurveySheet = ThisWorkbook.Worksheets("SurveySheet")
' xlSurveySheet.Activate
' xlSurveySheet.Cells.Select
 
SurveyLastRow = LastCell(xlSurveySheet).Row
SurveyRowCount = 1
 
For SurveyRowCount = 1 To SurveyLastRow
     SurveyRowCount = SurveyRowCount + 1
     SurveyQuestionFive = xlSurveySheet.Cells(SurveyRowCount, 6).Value
 
     If (xlSurveySheet.Cells(SurveyRowCount, 6).Value = "") Then
          xlSurveySheet.Cells(SurveyRowCount, 6).Value = "No Comments"
 
     Else
          If (xlSurveySheet.Cells(SurveyRowCount, 6).Value > 0) Then
 
          End If
     End If
 
Next SurveyRowCount
 
End Sub

Thanks in Advance.
-Young
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Have you tried stepping through to see exactly what it's doing when it skips every other cell?
 
Upvote 0
Seems like I have much to learn regarding VBA..

In some situations the counter is necessary, in others it isn't?

This was one of the times where deleting the counter allowed the macro to step through each row without incrementing SurveyRowCount.

Trial & Error is a Teacher to all.

Thanks, Glory, for the suggestion.
 
Upvote 0
That's the purpose of this line

For SurveyRowCount = 1 To SurveyLastRow

It automatically incriments the variable SurveyRowCount by 1 each loop

so by adding this line
SurveyRowCount = SurveyRowCount + 1

You've created a double incriment...
 
Upvote 0
This question has to have a simple solution..

In this situation, the task is to search each row given column F, looking for cells that are blank. In the blank cell the words "No Comment" should be entered.

With the macro as it stands now, the VBA Counter is checking ever other row, i.e. 2,4,6,8; for the existence of blanks.

Where am I going wrong in getting this code to go through every line?

Rich (BB code):
Sub FillBlanks()
 
Dim xlSurveySheet As Excel.Worksheet
Dim xlSurveyBook As Excel.Workbook
 
Dim SurveyLastRow As Integer
Dim SurveyQuestionFive As String
Dim SurveyRowCount As Long
 
fn = Application.GetOpenFilename("Excel-files,*.xlsx;*.csv", _
1, "Plateau Survey to Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
 
Set xlSurveyBook = Workbooks.Open(fn)
Set xlSurveySheet = xlSurveyBook.Worksheets(1)
 
'Set xlSurveySheet = ThisWorkbook.Worksheets("SurveySheet")
' xlSurveySheet.Activate
' xlSurveySheet.Cells.Select
 
SurveyLastRow = LastCell(xlSurveySheet).Row
SurveyRowCount = 1
 
For SurveyRowCount = 1 To SurveyLastRow Step 2 'only if you are skipping
     SurveyRowCount = SurveyRowCount + 1 'This destructive - remove or use step 2 - if you want it to skip a row
     SurveyQuestionFive = xlSurveySheet.Cells(SurveyRowCount, 6).Value
 
     If (xlSurveySheet.Cells(SurveyRowCount, 6).Value = "") Then
          xlSurveySheet.Cells(SurveyRowCount, 6).Value = "No Comments"
 
     Else
          If (xlSurveySheet.Cells(SurveyRowCount, 6).Value > 0) Then
 This does nothing or do you have other code?
          End If
     End If
 
Next SurveyRowCount
 
End Sub

Thanks in Advance.
-Young

Try suggestions above
 
Upvote 0
Don't increment, or do anything really, with a loop control variable within the loop.

By adding 1 to it you are basically turning into a loop which steps by 2.
 
Upvote 0
Thanks Everyone,
I understand more now.

After deleting "SurveyRowCount = SurveyRowCount + 1" I see how the Counter handles the stepping down of rows.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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
Back
Top