Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2620|回复: 12

[已解决]怎么样添加客户信息是自动插入到指定行

[复制链接]
发表于 2012-9-9 16:54 | 显示全部楼层 |阅读模式
怎么样添加客户信息是自动插入到指定行,谢谢 添加插入.zip (39.08 KB, 下载次数: 16)
发表于 2012-9-9 18:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hwc2ycy 于 2012-9-9 18:35 编辑
  1. Private Sub CommandButton1_Click()
  2.    
  3.     Dim Mr As Integer
  4.     Dim rng As Range
  5.     Dim what As String
  6.     Dim rw As Long
  7.     Dim frg As Range
  8.    
  9.     If Application.CountIf(Sheets("客户资料").Range("B:B"), TextBox2.Value) >= 1 Then
  10.         MsgBox "该客户号已经被使用,请重新输入"
  11.         Exit Sub
  12.     End If
  13.    
  14.     With Sheets("客户资料")
  15.         Mr = .Range("B65536").End(xlUp).Row + 1
  16.         
  17.         If Len(TextBox9.Value) > 0 Then
  18.             Set frg = Range("b:b").Find(TextBox9.Value, Lookat:=xlWhole)
  19.             If frg Is Nothing Then
  20.                 MsgBox "客户号有误,请重新输入"
  21.                 Exit Sub
  22.             End If
  23.            frg.Offset(1).EntireRow.Insert
  24.            Mr = frg.Row + 1
  25.         End If
  26.         Cells(Mr, 1).Formula = "=row()-1"
  27.         Cells(Mr, 2) = TextBox2.Value
  28.         Cells(Mr, 3) = TextBox3.Value
  29.         Cells(Mr, 4) = TextBox4.Value
  30.         Cells(Mr, 5) = TextBox5.Value
  31.         Cells(Mr, 6) = TextBox6.Value
  32.         Cells(Mr, 7) = TextBox7.Value
  33.         Cells(Mr, 8) = TextBox8.Value
  34.         Cells(Mr, 9).Formula = "=PINY(C" & Mr & ")"
  35.         MsgBox "添加完成"
  36.     End With
  37. End Sub
复制代码
回复

使用道具 举报

发表于 2012-9-9 18:35 | 显示全部楼层
刚刚的代码漏了第9列拼音缩写了,已经补上了。
回复

使用道具 举报

 楼主| 发表于 2012-9-9 18:56 | 显示全部楼层
hwc2ycy 发表于 2012-9-9 18:25

还有一个Cells(Mr, 9) =     I列的公式是=PINY(I)
Cells(Mr, 9) = "=piny()“
回复

使用道具 举报

发表于 2012-9-9 19:17 | 显示全部楼层
9910343 发表于 2012-9-9 18:56
还有一个Cells(Mr, 9) =     I列的公式是=PINY(I)
Cells(Mr, 9) = "=piny()“

后来给补了,
回复

使用道具 举报

 楼主| 发表于 2012-9-9 20:07 | 显示全部楼层
hwc2ycy 发表于 2012-9-9 19:17
后来给补了,

还有一个问题,就是TextBox2的值等于单元格j1的值,这句代码怎么写,放在哪儿
TextBox2.Value = Range("j1")
回复

使用道具 举报

发表于 2012-9-9 20:11 | 显示全部楼层
9910343 发表于 2012-9-9 20:07
还有一个问题,就是TextBox2的值等于单元格j1的值,这句代码怎么写,放在哪儿
TextBox2.Value = Range(" ...

你想实现什么功能?
回复

使用道具 举报

 楼主| 发表于 2012-9-9 20:15 | 显示全部楼层
hwc2ycy 发表于 2012-9-9 20:11
你想实现什么功能?

我的J1的值是客户代码这一行的最大值,这样在添加时就不必输入客户代码了
回复

使用道具 举报

发表于 2012-9-9 20:20 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-9-9 20:24 编辑
9910343 发表于 2012-9-9 20:15
我的J1的值是客户代码这一行的最大值,这样在添加时就不必输入客户代码了
  1. Private Sub UserForm_Initialize()
  2.     TextBox2.Value = Sheets("客户资料").Range("j1")
  3. End Sub
复制代码
这样窗体每次初始化的时候就会自动读取J1单元格并给TEXTBOX2赋值。
对于客户名称应该还得做条件限制,对于这两项录入不全的,应该不予添加。
回复

使用道具 举报

发表于 2012-9-9 20:32 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-9-9 20:34 编辑
  1. Private Sub CommandButton1_Click()
  2.     Dim Mr As Integer
  3.     Dim rng As Range
  4.     Dim what As String
  5.     Dim rw As Long
  6.     Dim frg As Range
  7.     If Application.CountIf(Sheets("客户资料").Range("B:B"), TextBox2.Value) >= 1 Then
  8.         MsgBox "该客户号已经被使用,请重新输入"
  9.         Exit Sub
  10.     End If
  11.    
  12.     If Len(TextBox3.Value) = 0 Then
  13.         MsgBox "客户名称未输入,请重新输入"
  14.         Exit Sub
  15.     End If
  16.    
  17.     With Sheets("客户资料")
  18.         Mr = .Range("B65536").End(xlUp).Row + 1
  19.         If Len(TextBox9.Value) > 0 Then
  20.             Set frg = Range("b:b").Find(TextBox9.Value, Lookat:=xlWhole)
  21.             If frg Is Nothing Then
  22.                 MsgBox "客户号有误,请重新输入"
  23.                 Exit Sub
  24.             End If
  25.            frg.Offset(1).EntireRow.Insert
  26.            Mr = frg.Row + 1
  27.         End If
  28.         Cells(Mr, 1).Formula = "=row()-1"
  29.         Cells(Mr, 2) = TextBox2.Value
  30.         Cells(Mr, 3) = TextBox3.Value
  31.         Cells(Mr, 4) = TextBox4.Value
  32.         Cells(Mr, 5) = TextBox5.Value
  33.         Cells(Mr, 6) = TextBox6.Value
  34.         Cells(Mr, 7) = TextBox7.Value
  35.         Cells(Mr, 8) = TextBox8.Value
  36.         Cells(Mr, 9).Formula = "=PINY(C" & Mr & ")"
  37.         MsgBox "添加完成"
  38.     End With
  39. End Sub

  40. Private Sub UserForm_Initialize()
  41.     TextBox2.Value = Sheets("客户资料").Range("j1")
  42. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
9910343 + 3

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 13:46 , Processed in 1.187593 second(s), 18 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表