Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

increment numbers in columns based on entering of adjacent cell

0

Hi Guys,

I  have  combobox1  contains sheets names  and  textbox1,2,3,4

what  I look  for  it  when  select  sheet  from combobox 1  and   copy data  are  filled in all of  textboxes  to  sheet  where select  from combobox  should increment  number in column A  1,2,3.... based on filling of adjacent cells for  column B  . it   gives error "object  doesn't support this property " in this  line 

.Columns(1).Value = .Parent.Evaluate("Row(1:" & .Rows.Count & ")")
Private Sub CommandButton1_Click()
Dim lr As Long, sh As Worksheet
Set sh = Sheets(ComboBox1.value)
lr = sh.Range("A" & Rows.Count).End(xlUp).Row
With sh
.Range("B" & lr + 1) = TextBox1.Value
.Range("C" & lr + 1) = TextBox2.Value
.Range("D" & lr + 1) = TextBox3.Value
.Range("E" & lr + 1) = TextBox4.Value
.Columns(1).Value = .Parent.Evaluate("Row(1:" & .Rows.Count & ")")
.Columns(1).NumberFormat = "General"
End With
End Sub

I look forward anybody  tell  me  what's  the  right  way  to  work  the  code  correctly .

thanks 

Answer
Discuss

Answers

0
Selected Answer

Alaa  (nice to see you back).

Using the file you posted after help from Willie, I'd suggest you just simply number each new row added to the chosen sheet, assuming previous rows have been numbered by your macro (changes in bold):

Private Sub CommandButton1_Click()
Dim lr As Long, sh As Worksheet

Set sh = Sheets(ComboBox1.Value)

With sh
    ' display that sheet
    .Activate
    lr = .Range("A" & .Rows.Count).End(xlUp).Row

    .Range("B" & lr + 1) = TextBox1.Value
    .Range("C" & lr + 1) = TextBox2.Value
    .Range("D" & lr + 1) = TextBox3.Value
    .Range("E" & lr + 1) = TextBox4.Value
    ' commented out controls not present yet
    '.Range("F" & lr + 1) = TextBox5.Value
    '.Range("G" & lr + 1) = TextBox6.Value

    '.Range("A2:A" & lr).Value = .Evaluate("Row(1:" & .Rows.Count & ")")
    .Range("A" & lr + 1) = lr

    .Columns(1).NumberFormat = "General"
End With



End Sub

I've done that in the attached file (with some test data you can erase).

If you really want to renumber all rows (starting from row 2), you could use the simple loop (within the With... End With) instead of your Evaluate statement:

Dim Cl As Range


    For Each Cl In .Range("A2:A" & lr + 1)
        Cl.Value = Cl.Row - 1
    Next Cl

You might want to start your macro with this:

' test a sheet was chosen
If ComboBox1.Value = "" Then Exit Sub

(before the Set line)

Hope this fixes things for you.

Discuss

Discussion

Hi John 
you fixed  somethings  starting  from  if  the  combobox  is   empty  will pops  error
and   make  the  selected  sheet is  active  . your  comment  helped me  
thanks  for  fix  somethings  and  your answering .
Alaa (rep: 28) Jan 25, '23 at 10:35 am
Glad that helped you. Thanks for selecting my Answer, Alaa.
John_Ru (rep: 6142) Jan 25, '23 at 10:37 am
Add to Discussion
0

Hello Alaa and welcome,

You haven't included a sample file so I can only guess at what is going wrong.

The problem lies in your syntax.

First: you have a period in front of "Parent" and the last "Rows" - remove these:  .Columns(1).Value = Parent.Evaluate("Row(1:" & Rows.Count & ")")

Second: Columns(1) refers to the entire column "A" - change this to reference the single cell where you want the result to be entered.

I made these changes in a test file and got no errors.

Hope this helps

Cheers   :-)

Discuss

Discussion

thanks 
I  change  to  this
.Range("A2:A" & lr).Value = .Evaluate("Row(1:" & .Rows.Count & ")")

but  start  from  the  row1 , should  be from  row2 .
Alaa (rep: 28) Jan 24, '23 at 4:11 pm
Without a sample file to show what the worksheet layout is, it is difficult to provide further help. Edit your original post using the Add Files button to upload a sample file.
WillieD24 (rep: 547) Jan 25, '23 at 12:24 am
I attached file 
Alaa (rep: 28) Jan 25, '23 at 3:00 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login