简体   繁体   English

EXCEL VBA动态数据输入

[英]EXCEL VBA Dynamic Data Entry

在此处输入图片说明

  1. RED colour box in Range("D:D") is the value refer from the Me.ComboBox1.Value Range(“ D:D”)中的红色颜色框是从Me.ComboBox1.Value引用的值
  2. Yellow and green colour are the data from Me.TextBox1 and Me.TextBox2 respectively. 黄色和绿色分别是来自Me.TextBox1Me.TextBox2的数据。 In Me, value in Me.TextBox1 and Me.TextBox2 will be insert into this sheet according to the value selected in the Me.ComboBox1 . 在Me中,将根据Me.ComboBox1选择的值将Me.TextBox1Me.TextBox2值插入此工作表中。
  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 另一件事是我为黄色和绿色数据的最后一行放置了.OffSet(1,0).EntireRow.Insert

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: 请注意,这取决于每个数据块中的E列中始终有一个值,以使其起作用:

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? 关于第4点,如果“ D”是列表中的最后一个值,那么为什么还要插入其他空白行,想必它下面的所有行还是空白?

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 . 另外,看起来好像不需要在“ With”块中偏移函数的“ rfound”部分。 . . that is what the "With" is there for. 这就是“ With”的用途。 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. 我尚未将其加载到VBA IDE中,因此请仔细检查拼写错误。 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. 似乎您正在尝试将textbox1的值(无论如何...如果稍稍解释一下,将会有所帮助)放置到与用户在combobox1中定义的搜索结果位置相对应的单元格偏移中。 The nextrow variable is inside the row offset parameter, but the location is already where you want it to be. nextrow变量位于行偏移量参数内部,但是该位置已经是您想要的位置。 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. 您可能会注意到,按照我以前的建议,我也删除了多余的“ rfound”。 See if this works and if so you may want to remove the newrow variable all together. 查看这是否可行,如果可以,则可能要一起删除newrow变量。

Good luck and let us know how it goes. 祝你好运,让我们知道如何进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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