Excel精英培训网

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

[已解决]如何实现复制时将格式也复制过来 麻烦帮忙修改一下代码

[复制链接]
发表于 2013-5-29 14:28 | 显示全部楼层 |阅读模式
各位前辈,初学者,不懂代码。如何修改代码后,可以将外币格式复制过来。谢谢各位了
最佳答案
2013-5-30 09:21
  1. Sub hanzhen2()
  2. 'Sheets("客户信息表").Select
  3. '有ThisWorkbook中的工作簿事件,此处无须再选中工作表.否则必须保证"客户信息表"为活动工作表该程序才能执行.
  4.     Dim UserRange As Range
  5.     On Error GoTo Canceled
  6.     Set UserRange = Application.InputBox _
  7.                     (Prompt:="请选择客户:", _
  8.                      Title:="提示:", _
  9.                      Type:=8)
  10.     With Worksheets("客户信息表")

  11.         TEMPbianhao = .Cells(UserRange.Row, 1).Value
  12.         TEMPmingcheng1 = .Cells(UserRange.Row, 3).Value
  13.         TEMPqian = .Cells(UserRange.Row, 4).Value
  14.         TEMPshe = .Cells(UserRange.Row, 6).Value
  15.         TEMPshe2 = .Cells(UserRange.Row, 7).Value
  16.         TEMPshe3 = .Cells(UserRange.Row, 5).Value
  17.         TEMPUNIT = .Cells(UserRange.Row, 9).Value
  18.         str4 = .Cells(UserRange.Row, 4).NumberFormatLocal
  19.         str5 = .Cells(UserRange.Row, 5).NumberFormatLocal
  20.         str6 = .Cells(UserRange.Row, 6).NumberFormatLocal
  21.         str7 = .Cells(UserRange.Row, 7).NumberFormatLocal
  22.     End With

  23.     If Worksheets("客户信息表").Cells(UserRange.Row, 2).Value = "√" Then

  24.         On Error Resume Next
  25.         If Len(Worksheets(TEMPmingcheng1).Name) = 0 Then
  26.             Worksheets("企业询证函模板").Copy after:=Worksheets(Worksheets.Count)
  27.             Set newsheet = ActiveSheet
  28.             Err.Clear
  29.         Else
  30.             Set newsheet = Worksheets(TEMPmingcheng1)
  31.         End If
  32.         With mySheet
  33.             .Name = TEMPmingcheng1
  34.             .Range("A3").FormulaR1C1 = "致:" & TEMPmingcheng1
  35.             .Range("E2").FormulaR1C1 = "编号:" & "OOO" & TEMPbianhao
  36.             .Range("B13").FormulaR1C1 = "截止日期:" & "2012年12月31日"
  37.             .Range("C14").FormulaR1C1 = TEMPqian
  38.             .Range("C14").NumberFormatLocal = str4
  39.             .Range("E15").FormulaR1C1 = TEMPshe
  40.             .Range("E15").NumberFormatLocal = str6
  41.             .Range("E14").FormulaR1C1 = TEMPshe2
  42.             .Range("E14").NumberFormatLocal = str7
  43.             .Range("C15").FormulaR1C1 = TEMPshe3
  44.             .Range("C15").NumberFormatLocal = str5
  45.             .Range("D21").FormulaR1C1 = TEMPUNIT
  46.         End With
  47.     Else
  48.         MsgBox Prompt:="此客户无需函证!", Title:="友情提示:"
  49.     End If

  50.     mySheet.PrintPreview
  51.     Worksheets("客户信息表").Activate
  52. Canceled:
  53. End Sub
复制代码

复件 企业函证定稿.rar

24.49 KB, 下载次数: 8

发表于 2013-5-30 07:04 | 显示全部楼层
  1. Sub hanzhen2()
  2. 'Sheets("客户信息表").Select
  3. '有ThisWorkbook中的工作簿事件,此处无须再选中工作表.否则必须保证"客户信息表"为活动工作表该程序才能执行.
  4.     Dim UserRange As Range
  5.     On Error GoTo Canceled
  6.     Set UserRange = Application.InputBox _
  7.                     (Prompt:="请选择客户:", _
  8.                      Title:="提示:", _
  9.                      Type:=8)
  10.     With Sheets("客户信息表")

  11.         TEMPbianhao = .Cells(UserRange.Row, 1).Value
  12.         TEMPmingcheng1 = .Cells(UserRange.Row, 3).Value
  13.         TEMPqian = .Cells(UserRange.Row, 4).Value
  14.         TEMPshe = .Cells(UserRange.Row, 6).Value
  15.         TEMPshe2 = .Cells(UserRange.Row, 7).Value
  16.         TEMPshe3 = .Cells(UserRange.Row, 5).Value
  17.         TEMPUNIT = .Cells(UserRange.Row, 9).Value
  18.         str4 = .Cells(UserRange.Row, 4).NumberFormatLocal
  19.         str5 = .Cells(UserRange.Row, 5).NumberFormatLocal
  20.         str6 = .Cells(UserRange.Row, 6).NumberFormatLocal
  21.         str7 = .Cells(UserRange.Row, 7).NumberFormatLocal
  22.     End With
  23.     If Sheets("客户信息表").Cells(UserRange.Row, 2).Value = "√" Then


  24.         Set mySheet = Worksheets.Add(Type:=xlWorksheet, after:=Sheets(Sheets.Count))


  25.         Sheets("企业询证函模板").Select
  26.         Cells.Select
  27.         Selection.Copy
  28.         mySheet.Select
  29.         Cells.Select
  30.         Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
  31.                                False, Transpose:=False



  32.         With mySheet
  33.             .Name = TEMPmingcheng1
  34.             .Range("A3").FormulaR1C1 = "致:" & TEMPmingcheng1
  35.             .Range("E2").FormulaR1C1 = "编号:" & "OOO" & TEMPbianhao
  36.             .Range("B13").FormulaR1C1 = "截止日期:" & "2012年12月31日"
  37.             .Range("C14").FormulaR1C1 = TEMPqian
  38.             .Range("C14").NumberFormatLocal = str4
  39.             .Range("E15").FormulaR1C1 = TEMPshe
  40.             .Range("E15").NumberFormatLocal = str6
  41.             .Range("E14").FormulaR1C1 = TEMPshe2
  42.             .Range("E14").NumberFormatLocal = str7
  43.             .Range("C15").FormulaR1C1 = TEMPshe3
  44.             .Range("C15").NumberFormatLocal = str5
  45.             .Range("D21").FormulaR1C1 = TEMPUNIT
  46.         End With
  47.     Else: MsgBox Prompt:="此客户无需函证!", Title:="友情提示:"
  48.     End If

  49.     mySheet.PrintPreview
  50.     Sheets("客户信息表").Select
  51. Canceled:
  52. End Sub
复制代码
回复

使用道具 举报

发表于 2013-5-30 07:14 | 显示全部楼层
复件 企业函证定稿.rar (19.38 KB, 下载次数: 6)
回复

使用道具 举报

 楼主| 发表于 2013-5-30 08:09 | 显示全部楼层
hwc2ycy 发表于 2013-5-30 07:14

十分感谢您的帮助
回复

使用道具 举报

发表于 2013-5-30 08:23 | 显示全部楼层
其实自定义格式就是单元格的NumberFormatLocal

另外你的代码里,如果重复生成报告就出错了,这个没有做判断。
回复

使用道具 举报

 楼主| 发表于 2013-5-30 08:43 | 显示全部楼层
hwc2ycy 发表于 2013-5-30 07:14

麻烦问一下,在生成多个函证时,如何不复制模版
回复

使用道具 举报

 楼主| 发表于 2013-5-30 08:44 | 显示全部楼层
hwc2ycy 发表于 2013-5-30 08:23
其实自定义格式就是单元格的NumberFormatLocal

另外你的代码里,如果重复生成报告就出错了,这个没有做判 ...

就是要求不允许重复生成报告
回复

使用道具 举报

发表于 2013-5-30 09:21 | 显示全部楼层
你把原来的代码都换掉。
  1. Sub hanzheng1()

  2.     Dim Msg, Style, Title, Response
  3.     Dim TEMPA As String
  4.     Msg = "是否函证?"
  5.     Style = vbYesNo
  6.     Title = "请确认"
  7.     If MsgBox(Msg, Style, Title) = vbYes Then

  8.         For i = 4 To Worksheets("客户信息表").Range("a1").CurrentRegion.Rows.Count
  9.             With Worksheets("客户信息表")
  10.                 TEMPbianhao = .Cells(i, 1).Value
  11.                 TEMPmingcheng = .Cells(i, 3).Value
  12.                 TEMPqian = .Cells(i, 4).Value
  13.                 TEMPshe = .Cells(i, 6).Value
  14.                 TEMPshe2 = .Cells(i, 7).Value
  15.                 TEMPshe3 = .Cells(i, 5).Value
  16.                 TEMPUNIT = .Cells(i, 9).Value

  17.                 str4 = .Cells(i, 4).NumberFormatLocal
  18.                 str5 = .Cells(i, 5).NumberFormatLocal
  19.                 str6 = .Cells(i, 6).NumberFormatLocal
  20.                 str7 = .Cells(i, 7).NumberFormatLocal
  21.             End With
  22.             If Worksheets("客户信息表").Cells(i, 2).Value = "√" And Worksheets("客户信息表").Cells(i, 3).Value <> "" Then
  23.                 Dim newsheet As Worksheet
  24.                 On Error Resume Next
  25.                 If Len(Worksheets(TEMPmingcheng).Name) = 0 Then
  26.                     Worksheets("企业询证函模板").Copy after:=Worksheets(Worksheets.Count)
  27.                     Set newsheet = ActiveSheet
  28.                     Err.Clear
  29.                 Else
  30.                     Set newsheet = Worksheets(TEMPmingcheng)
  31.                 End If
  32.                 On Error GoTo 0
  33.                 With newsheet
  34.                     .Name = TEMPmingcheng
  35.                     .Range("A3").FormulaR1C1 = "致:" & TEMPmingcheng
  36.                     .Range("e2").FormulaR1C1 = "编号:" & "OOO" & TEMPbianhao
  37.                     .Range("b13").Formula = "截止日期:" & "2012年12月31日"
  38.                     .Range("c14").FormulaR1C1 = TEMPqian
  39.                     .Range("e15").FormulaR1C1 = TEMPshe
  40.                     .Range("e14").FormulaR1C1 = TEMPshe2
  41.                     .Range("c15").FormulaR1C1 = TEMPshe3
  42.                     .Range("d21").FormulaR1C1 = TEMPUNIT
  43.                     .Range("C14").NumberFormatLocal = str4
  44.                     .Range("E15").NumberFormatLocal = str6
  45.                     .Range("E14").NumberFormatLocal = str7
  46.                     .Range("C15").NumberFormatLocal = str5
  47.                 End With
  48.             End If
  49.         Next i
  50.     End If
  51. End Sub
复制代码
回复

使用道具 举报

发表于 2013-5-30 09:21 | 显示全部楼层    本楼为最佳答案   
  1. Sub hanzhen2()
  2. 'Sheets("客户信息表").Select
  3. '有ThisWorkbook中的工作簿事件,此处无须再选中工作表.否则必须保证"客户信息表"为活动工作表该程序才能执行.
  4.     Dim UserRange As Range
  5.     On Error GoTo Canceled
  6.     Set UserRange = Application.InputBox _
  7.                     (Prompt:="请选择客户:", _
  8.                      Title:="提示:", _
  9.                      Type:=8)
  10.     With Worksheets("客户信息表")

  11.         TEMPbianhao = .Cells(UserRange.Row, 1).Value
  12.         TEMPmingcheng1 = .Cells(UserRange.Row, 3).Value
  13.         TEMPqian = .Cells(UserRange.Row, 4).Value
  14.         TEMPshe = .Cells(UserRange.Row, 6).Value
  15.         TEMPshe2 = .Cells(UserRange.Row, 7).Value
  16.         TEMPshe3 = .Cells(UserRange.Row, 5).Value
  17.         TEMPUNIT = .Cells(UserRange.Row, 9).Value
  18.         str4 = .Cells(UserRange.Row, 4).NumberFormatLocal
  19.         str5 = .Cells(UserRange.Row, 5).NumberFormatLocal
  20.         str6 = .Cells(UserRange.Row, 6).NumberFormatLocal
  21.         str7 = .Cells(UserRange.Row, 7).NumberFormatLocal
  22.     End With

  23.     If Worksheets("客户信息表").Cells(UserRange.Row, 2).Value = "√" Then

  24.         On Error Resume Next
  25.         If Len(Worksheets(TEMPmingcheng1).Name) = 0 Then
  26.             Worksheets("企业询证函模板").Copy after:=Worksheets(Worksheets.Count)
  27.             Set newsheet = ActiveSheet
  28.             Err.Clear
  29.         Else
  30.             Set newsheet = Worksheets(TEMPmingcheng1)
  31.         End If
  32.         With mySheet
  33.             .Name = TEMPmingcheng1
  34.             .Range("A3").FormulaR1C1 = "致:" & TEMPmingcheng1
  35.             .Range("E2").FormulaR1C1 = "编号:" & "OOO" & TEMPbianhao
  36.             .Range("B13").FormulaR1C1 = "截止日期:" & "2012年12月31日"
  37.             .Range("C14").FormulaR1C1 = TEMPqian
  38.             .Range("C14").NumberFormatLocal = str4
  39.             .Range("E15").FormulaR1C1 = TEMPshe
  40.             .Range("E15").NumberFormatLocal = str6
  41.             .Range("E14").FormulaR1C1 = TEMPshe2
  42.             .Range("E14").NumberFormatLocal = str7
  43.             .Range("C15").FormulaR1C1 = TEMPshe3
  44.             .Range("C15").NumberFormatLocal = str5
  45.             .Range("D21").FormulaR1C1 = TEMPUNIT
  46.         End With
  47.     Else
  48.         MsgBox Prompt:="此客户无需函证!", Title:="友情提示:"
  49.     End If

  50.     mySheet.PrintPreview
  51.     Worksheets("客户信息表").Activate
  52. Canceled:
  53. End Sub
复制代码
回复

使用道具 举报

发表于 2013-5-30 09:22 | 显示全部楼层
修改了下,对于已经有存在的询证函,直接修改内容,不再重新生成。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 06:03 , Processed in 0.450662 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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