Excel精英培训网

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

[已解决]如何复制并清除 内容

[复制链接]
发表于 2011-3-28 01:02 | 显示全部楼层 |阅读模式
写一代码要实现下面的功能:
                 将费用类别下的空格用左面一列的数值填充,然后将费用类别下的数据剪切粘到左面一列相应的位置上,此时数据为数值,
最后清除费用类别下的数据
不知我说明白了吗
最佳答案
2011-3-28 21:30
那就进行了下修改。
这个代码放在SHEET1的模块里
  1. Option Explicit
  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3. Dim iCurRow As Integer
  4. Dim iReturn As Integer
  5. Dim iLeibie As Integer
  6. Dim iRows As Integer
  7. iCurRow = Target.Row
  8. If Target.Count = 1 And Cells(1, Target.Column) = "费用类别" Then
  9. iReturn = MsgBox("是否选择进行数据转换", 36, "需要转换数据么?")
  10.     If iReturn = 6 Then
  11.         iRows = Range("A65536").End(xlUp).Row
  12.         iLeibie = Target.Column
  13.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[-1]"
  14.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).Copy
  15.         Range(Cells(iCurRow, iLeibie - 1), Cells(iRows, iLeibie - 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  16.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).Delete xlToLeft
  17.     End If
  18. End If
  19. End Sub
复制代码

当你点的在“费用类别”那行的时候提示你需要数据转换否,如果是,则进行数据转换。
复制.gif
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2011-3-28 01:06 | 显示全部楼层
本帖最后由 ls 于 2011-3-28 01:07 编辑

复制.rar

12.56 KB, 下载次数: 11

回复

使用道具 举报

发表于 2011-3-28 01:39 | 显示全部楼层
我来回答吧:

  1. Sub DeletClass()
  2. Dim iLeiBie As Integer
  3. Dim iRows As Integer
  4. Dim i As Integer
  5. iRows = Range("A65536").End(xlUp).Row
  6. '查找“费用类别”所在的列
  7. For i = 1 To 255
  8.     If Cells(1, i).Value = "费用类别" Then
  9.         iLeiBie = i
  10.         Exit For
  11.     End If
  12. Next
  13. '选择“费用类别”里的空值
  14. Columns(i).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[-1]"
  15. Range(Cells(2, iLeiBie), Cells(iRows, iLeiBie)).Copy
  16. Range(Cells(2, iLeiBie - 1), Cells(2, iLeiBie - 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  17. Range(Cells(2, iLeiBie), Cells(iRows, iLeiBie)).Delete xlToLeft
  18. End Sub
复制代码
我很偷懒地录制了下宏。
回复

使用道具 举报

 楼主| 发表于 2011-3-28 18:32 | 显示全部楼层
放浪形骸 发表于 2011-3-28 01:39
我来回答吧:
我很偷懒地录制了下宏。

我也录制了宏,没会修改,没有达到我的目的
回复

使用道具 举报

 楼主| 发表于 2011-3-28 18:45 | 显示全部楼层
Range(Cells(2, iLeiBie), Cells(iRows, iLeiBie)).Copy
顶点能不能我来选择,不一定是Cells(2, iLeiBie)
回复

使用道具 举报

发表于 2011-3-28 20:05 | 显示全部楼层
那一列的名字是叫金额么?
回复

使用道具 举报

 楼主| 发表于 2011-3-28 20:54 | 显示全部楼层
放浪形骸 发表于 2011-3-28 20:05
那一列的名字是叫金额么?

我要达到这样的要求:
                我在费用类别列选择range(中间的一个点到iROW),
然后将这一区域的空值等于左一列的值,复制range(中间的一个点到iROW),粘贴到左一列的位置
注意:中间一点到费用类别的区域不变
回复

使用道具 举报

 楼主| 发表于 2011-3-28 20:55 | 显示全部楼层
也就是说,费用类别列选择range(中间的一个点到iROW),是我所选择的区域
回复

使用道具 举报

 楼主| 发表于 2011-3-28 21:04 | 显示全部楼层
或者可以做个选择区域的 对话框,如下面的样式
2011-03-27_220900.gif
回复

使用道具 举报

发表于 2011-3-28 21:30 | 显示全部楼层    本楼为最佳答案   
那就进行了下修改。
这个代码放在SHEET1的模块里
  1. Option Explicit
  2. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  3. Dim iCurRow As Integer
  4. Dim iReturn As Integer
  5. Dim iLeibie As Integer
  6. Dim iRows As Integer
  7. iCurRow = Target.Row
  8. If Target.Count = 1 And Cells(1, Target.Column) = "费用类别" Then
  9. iReturn = MsgBox("是否选择进行数据转换", 36, "需要转换数据么?")
  10.     If iReturn = 6 Then
  11.         iRows = Range("A65536").End(xlUp).Row
  12.         iLeibie = Target.Column
  13.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[-1]"
  14.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).Copy
  15.         Range(Cells(iCurRow, iLeibie - 1), Cells(iRows, iLeibie - 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  16.         Range(Cells(iCurRow, iLeibie), Cells(iRows, iLeibie)).Delete xlToLeft
  17.     End If
  18. End If
  19. End Sub
复制代码

当你点的在“费用类别”那行的时候提示你需要数据转换否,如果是,则进行数据转换。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-23 11:27 , Processed in 0.268539 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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