You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- Altering VBA

Advertisement

QUESTION: Hi Tom

The following piece of code, when executed, will look into column “B” and check to see if there is an instance of the numbers: 7, 8, 9, 10 or 11. If any of these numbers is missing the code will place it at the bottom of the list.

Could you alter the code so that the word “blank” is placed in column “A” alongside the newly inserted number in column “B”?

Thanks in advance.

Chris

Sub Sheet2rng7891011()

'

' Sheet2rng7891011 Macro

'

'

Dim icol As Long, lastrow As Long, i As Long

Dim scol As String

Dim sh As Worksheet

Set sh = Worksheets("Sheet2rng")

scol = "B"

icol = sh.Range(scol & ":" & scol).Column

lastrow = sh.Cells(sh.Rows.Count, icol).End(xlUp).Row

For i = 7 To 11

If Application.CountIf(sh.Cells(1, icol).Resize(lastrow, 1), i) = 0 Then

lastrow = lastrow + 1

sh.Cells(lastrow, icol).Value = i

End If

Next

Calculate

End Sub

ANSWER: Christopher Mitchell,

Sub Sheet2rng7891011()

'

' Sheet2rng7891011 Macro

'

'

Dim icol As Long, lastrow As Long, i As Long

Dim scol As String

Dim sh As Worksheet

Set sh = Worksheets("Sheet2rng")

scol = "B"

icol = sh.Range(scol & ":" & scol).Column

lastrow = sh.Cells(sh.Rows.Count, icol).End(xlUp).Row

For i = 7 To 11

If Application.CountIf(sh.Cells(1, icol).Resize(lastrow, 1), i) = 0 Then

lastrow = lastrow + 1

sh.Cells(lastrow, icol).Value = i

sh.Cells(lastrow, "A").Value = "blank" '<== added line

End If

Next

Calculate

End Sub

--

Regards,

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Hi Tom

Could this code be altered so that each new instance of the word "blank" becomes "blank1", blank2" etc. So that if the word "blank" is placed in column "A" five times instead of:

Column A Column B

blank 7

blank 8

blank 9

blank 10

blank 11

I will see

Column A Column B

blank 7

blank1 8

blank2 9

blank3 10

blank4 11

Thanks in advance

Chris

Christopher Mitchell,

Sub Sheet2rng7891011()

'

' Sheet2rng7891011 Macro

'

'

Dim icol As Long, lastrow As Long, i As Long

Dim scol As String

Dim sh As Worksheet

Dim seq as Long

Set sh = Worksheets("Sheet2rng")

scol = "B"

icol = sh.Range(scol & ":" & scol).Column

lastrow = sh.Cells(sh.Rows.Count, icol).End(xlUp).Row

seq = 1 '<== line added

For i = 7 To 11

If Application.CountIf(sh.Cells(1, icol).Resize(lastrow, 1), i) = 0 Then

lastrow = lastrow + 1

sh.Cells(lastrow, icol).Value = i

sh.Cells(lastrow, "A").Value = "blank" & seq '<== modified line

seq = seq + 1 '<== line added

End If

Next

Calculate

End Sub

--

Regards,

Tom Ogilvy

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Fantastic. Thank you very much. Chris |

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

Answers by Expert:

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Extensive experience. **Education/Credentials**

Master of Science (MS) degree Operations Research (ORSA)**Awards and Honors**

Microsoft MVP in Excel.