简体   繁体   中英

EXCEL VBA Dynamic Data Entry

在此处输入图片说明

  1. RED colour box in Range("D:D") is the value refer from the Me.ComboBox1.Value
  2. Yellow and green colour are the data from Me.TextBox1 and Me.TextBox2 respectively. In Me, value in Me.TextBox1 and Me.TextBox2 will be insert into this sheet according to the value selected in the Me.ComboBox1 .
  3. Therefore, in this case, I wanted the yellow and green colour to be entered accordingly to the red selected by user.
  4. Another extra thing is that I put a .OffSet(1,0).EntireRow.Insert for the last row of yellow and green data

This should do it:

nextrow = Sheets("DB Cust").Range("C" & Sheets("DB Cust").Rows.Count).End(xlUp).Row + 1

UPDATE Following your edit, I think something similar to this could be what you need. Please note that this depends on there always being a value in column E for each block of data in order for it to work:

Dim lngNewRow As Long
Dim strLookupValue As String

strLookupValue = "A" ' or B/C/D etc.

lngNewRow = Sheets("DB Cust").Range("D:D").Find(strLookupValue).Offset(, 1).End(xlDown).Row + 1
Sheets("DB Cust").Rows(lngNewRow).Insert
Sheets("DB Cust").Cells(lngNewRow, "E").Value = "Data for column E"
Sheets("DB Cust").Cells(lngNewRow, "F").Value = "Data for column F"

With regards to your point 4, if "D" is the last value in the list then why do you need to insert additional blank rows, presumably all rows beneath it are blank anyway?

I'm not sure but the way you are calling your range seems strange to me. Try a different way of defining your count range.

Change this:

nextrow = WorksheetFunction.CountA(Sheets("DB Cust").Range("C:C")) + 2

To this:

Dim myWB as Workbook, DBcust as Worksheet
Set myWB = Excel.ActiveWorkbook
Set DBcust = myWB.Worksheets("DB Cust")

nextrow = Excel.WorksheetFunction.CountA(DBcust.Range(DBcust.Cells(1,3),DBcust.Cells(DBcust.UsedRange.Rows.Count,3)) + 2

I assigned the book and sheet to a variable for more reliability, but you can explicitly state them again if you wanted to. This code assumes the workbook is the currently active workbook, if not you will have to set the variable using the workbook name.

Also, it doesn't look like you need the "rfound" portion of the offset function within the "With" block . . . that is what the "With" is there for. It's just a little thing but meaningless code like that will only cause you extra headache so my advice would be to take it out.

I haven't loaded this into the VBA IDE so please double check for spelling errors. Thanks!

UPDATE:

After reading your comment, I took a closer look at your code and what it is you're trying to do. It looks like you are trying to place the value of textbox1 (whatever that may be...it would help if you explained this part a little) into a cell offset from the location of a search result defined by the user in combobox1. The nextrow variable is inside the row offset parameter, but the location is already where you want it to be. Try changing this:

With rfound

rfound.Offset(nextrow, 1).Value = TextBox1.Value
rfound.Offset(nextrow, 2).Value = TextBox1.Value
rfound.Offset(nextrow, 3).Value = TextBox1.Value
rfound.Offset(nextrow, 4).Value = TextBox1.Value
rfound.Offset(nextrow, 5).Value = TextBox1.Value
rfound.Offset(nextrow, 6).Value = TextBox1.Value
rfound.Offset(nextrow, 7).Value = TextBox1.Value
rfound.Offset(nextrow, 8).Value = TextBox1.Value
MsgBox ("Data entry success")

End With

To this:

With rfound

.Offset(0, 1).Value = TextBox1.Value
.Offset(0, 2).Value = TextBox1.Value
.Offset(0, 3).Value = TextBox1.Value
.Offset(0, 4).Value = TextBox1.Value
.Offset(0, 5).Value = TextBox1.Value
.Offset(0, 6).Value = TextBox1.Value
.Offset(0, 7).Value = TextBox1.Value
.Offset(0, 8).Value = TextBox1.Value
MsgBox ("Data entry success")

End With

You may notice I also removed the redundant "rfound" as per my previous advice. See if this works and if so you may want to remove the newrow variable all together.

Good luck and let us know how it goes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM