Using offset to add value to next blank cell - please help with code

Kellogg

New Member
Joined
Mar 30, 2013
Messages
41
Platform
  1. Windows
  2. MacOS
Code:
Dear Great Minds of Excel,

I have a user form on worksheet "PCODE8" that searches a worksheet called "StateInfo" to see if a county is listed in the column under the state abbreviation.  the code successfully finds the correct State abbreviation, listed in the first cell in the column for that state, checks to see if the county is listed but fails to add the county if not listed.  I have one column for each state and would like the columns to populate as users enter county names for the first time.   The table shows worksheet "StateInfo" and the bold letter/numbers are just the column designation.  They are not row 1, just for reference.  I'm using the column number designations, 2-6, because I need to have the formula dynamic to find the state column to check if the county is listed then add if not listed.


[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][B]B (2)[/B][/TD]
[TD][B]C (3)[/B][/TD]
[TD][B]D (4)[/B][/TD]
[TD][B]E (5)[/B][/TD]
[TD][B] (6)[/B][/TD]
[/TR]
[TR]
[TD]AK[/TD]
[TD]AL[/TD]
[TD]AR[/TD]
[TD]AZ[/TD]
[TD]CA[/TD]
[/TR]
[TR]
[TD]County 1[/TD]
[TD]County 1[/TD]
[TD]County 1[/TD]
[TD]County 1[/TD]
[TD]County 1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]County 2[/TD]
[TD]County 2[/TD]
[TD]County 2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]County 3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


I have successfully done this using an offset but the column has to be specifically designated and I need it to be dynamic.  The code below is what I have and included are notes of where it goes into problems.  Any suggestions would be outstanding help.

The two lines I'm struggling with are the following:

    Range("I" & Rows.count).End(xlUp).Offset(1).Select                  '****this works

and I want to convert to 


    Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1).Select   '**** this is what I'm trying to get to but the formula continues to replace (.Cells(2, n) instead of        
                                                                                                   'adding to next empty cell.

The entire section is below with notes.


'Check if County is in list ***********************************************
Dim stNme As String           'State Abbreviation in first cell of column
Dim ctyNme As String          'State's county name
stNme = cbxEntryState        'cbxEntryState is the userform field holding the State two letter abbreviation
ctyNme = cbxEntryCounty     'cbxEntryCounty is the userform field holding the county name




'Find Column With Target State Abbreviation
Dim Found As Range, LastRow As Long, n As Integer
Set Found = Sheets("StateInfo").Rows(1).Find(what:=stNme, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
LastRow = Cells(Rows.count, Found.Column).End(xlUp).Row
n = Found.Column  'This is the column that matches the state's two letter abbreviation




'Select Range Of Target State to check if county is listed
Dim supMessage As String
supMessage = "This County is not listed.  It will be added to the list."


With Sheets("StateInfo")
If WorksheetFunction.CountIf(.Range(.Cells(2, n), .Cells(1000, n)), ctyNme) = 0 Then
Application.Speech.Speak (supMessage)
'MsgBox "This Pest, I mean Officer, is not listed.  It will be added to the list."
'''''''''''
    Sheets("PCODE8").Range("AA1") = cbxEntryCounty
    Sheets("PCODE8").Range("AA1").Select
    Selection.Copy
    
    'after copy go to destination page
    
    Sheets("StateInfo").Visible = True
    Sheets("StateInfo").Select
    '
    'Now paste to next empty row in column n between (2, n) and (1000, n) or use end(xlup)
    '
    Range("I" & Rows.count).End(xlUp).Offset(1).Select                'this works
    'Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1).Select  '**** this is what I'm trying to get to but the formula continues to replace (.Cells(2, n) only
    
'I have also tried the two below
    'Range(.Cells(65536, n)).End(xlUp).Offset(1, 0).Select
    'Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1, 0).Select
    
    
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets("StateInfo").Visible = False
    Sheets("PCODE8").Select
    
    
'''''''''''''
End If


End With


I think I'm pretty close to an answer.  I have searched through the forum and their are workable suggestions but cannot find how to make the column tied to a variable or make offset code dynamic.  Any help is greatly appreciated.  

Thank you.

Craig
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Dear Great Minds of Excel,

I have a user form on worksheet "PCODE8" that searches a worksheet called "StateInfo" to see if a county is listed in the column under the state abbreviation.  the code successfully finds the correct State abbreviation, listed in the first cell in the column for that state, checks to see if the county is listed but fails to add the county if not listed.  I have one column for each state and would like the columns to populate as users enter county names for the first time.   The table shows worksheet "StateInfo" and the bold letter/numbers are just the column designation.  They are not row 1, just for reference.  I'm using the column number designations, 2-6, because I need to have the formula dynamic to find the state column to check if the county is listed then add if not listed.


[TABLE="class: grid, width: 500, align: center"]
<TBODY>[TR]
[TD][B]B (2)[/B]
[/TD]
[TD][B]C (3)[/B]
[/TD]
[TD][B]D (4)[/B]
[/TD]
[TD][B]E (5)[/B]
[/TD]
[TD][B](6)[/B]
[/TD]
[/TR]
[TR]
[TD]AK
[/TD]
[TD]AL
[/TD]
[TD]AR
[/TD]
[TD]AZ
[/TD]
[TD]CA
[/TD]
[/TR]
[TR]
[TD]County 1
[/TD]
[TD]County 1
[/TD]
[TD]County 1
[/TD]
[TD]County 1
[/TD]
[TD]County 1
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]County 2
[/TD]
[TD]County 2
[/TD]
[TD]County 2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]County 3
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]


I have successfully done this using an offset but the column has to be specifically designated and I need it to be dynamic.  The code below is what I have and included are notes of where it goes into problems.  Any suggestions would be outstanding help.

The two lines I'm struggling with are the following:

    Range("I" & Rows.count).End(xlUp).Offset(1).Select                  '****this works

and I want to convert to 


    Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1).Select   '**** this is what I'm trying to get to but the formula continues to replace (.Cells(2, n) instead of        
                                                                                                   'adding to next empty cell.

The entire section is below with notes.


'Check if County is in list ***********************************************
Dim stNme As String           'State Abbreviation in first cell of column
Dim ctyNme As String          'State's county name
stNme = cbxEntryState        'cbxEntryState is the userform field holding the State two letter abbreviation
ctyNme = cbxEntryCounty     'cbxEntryCounty is the userform field holding the county name




'Find Column With Target State Abbreviation
Dim Found As Range, LastRow As Long, n As Integer
Set Found = Sheets("StateInfo").Rows(1).Find(what:=stNme, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
LastRow = Cells(Rows.count, Found.Column).End(xlUp).Row
n = Found.Column  'This is the column that matches the state's two letter abbreviation




'Select Range Of Target State to check if county is listed
Dim supMessage As String
supMessage = "This County is not listed.  It will be added to the list."


With Sheets("StateInfo")
If WorksheetFunction.CountIf(.Range(.Cells(2, n), .Cells(1000, n)), ctyNme) = 0 Then
Application.Speech.Speak (supMessage)
'MsgBox "This Pest, I mean Officer, is not listed.  It will be added to the list."
'''''''''''
    Sheets("PCODE8").Range("AA1") = cbxEntryCounty
    Sheets("PCODE8").Range("AA1").Select
    Selection.Copy
    
    'after copy go to destination page
    
    Sheets("StateInfo").Visible = True
    Sheets("StateInfo").Select
    '
    'Now paste to next empty row in column n between (2, n) and (1000, n) or use end(xlup)
    '
    Range("I" & Rows.count).End(xlUp).Offset(1).Select                'this works
    'Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1).Select  '**** this is what I'm trying to get to but the formula continues to replace (.Cells(2, n) only
    
'I have also tried the two below
    'Range(.Cells(65536, n)).End(xlUp).Offset(1, 0).Select
    'Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1, 0).Select
    
    
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets("StateInfo").Visible = False
    Sheets("PCODE8").Select
    
    
'''''''''''''
End If


End With


I think I'm pretty close to an answer.  I have searched through the forum and their are workable suggestions but cannot find how to make the column tied to a variable or make offset code dynamic.  Any help is greatly appreciated.  

Thank you.

Craig

Try replacing this:
Code:
 Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1, 0).Select
With this
Code:
 ActiveSheet.Cells(1000, n).End(xlUp).Offset(1, 0).Select

It would also help if you could write the code without using Select. For instance:

Instead of:
Code:
Sheets(1).Select
Range("A1").Select
Selection.Copy

Just write:

Code:
Sheets(1).Range("A1").Copy

The Select keyword was a tool for recording macros so the user's manual movements could be tracked. It is unnecessary if you are writing the code yourself.
 
Upvote 0
JLG,

You are a whiz. Thanks it works perfectly. I will change the select also. Thanks again.:cool:



Try replacing this:
Code:
 Range(.Cells(2, n), .Cells(1000, n)).End(xlUp).Offset(1, 0).Select
With this
Code:
 ActiveSheet.Cells(1000, n).End(xlUp).Offset(1, 0).Select

It would also help if you could write the code without using Select. For instance:

Instead of:
Code:
Sheets(1).Select
Range("A1").Select
Selection.Copy

Just write:

Code:
Sheets(1).Range("A1").Copy

The Select keyword was a tool for recording macros so the user's manual movements could be tracked. It is unnecessary if you are writing the code yourself.
 
Upvote 0

Forum statistics

Threads
1,216,111
Messages
6,128,899
Members
449,477
Latest member
panjongshing

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